Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android Development for Beginners : Court Counter - Setting up the Methods
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:text="Team A" />
<TextView
android:id="@+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:text="0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="addThreeForTeamA"
android:text="+3 Points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="addTwoForTeamA"
android:text="+2 Points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="addOneForTeamA"
android:text="Free throw" />
</LinearLayout>
package com.example.android.courtcounter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Increase the score for Team A by 1 point.
*/
public void addOneForTeamA(View v) {
displayForTeamA(1);
}
/**
* Increase the score for Team A by 2 points.
*/
public void addTwoForTeamA(View v) {
displayForTeamA(2);
}
/**
* Increase the score for Team A by 3 points.
*/
public void addThreeForTeamA(View v) {
displayForTeamA(3);
}
/**
* Displays the given score for Team A.
*/
public void displayForTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
}
@enessoftic

This comment has been minimized.

Copy link

commented May 28, 2016

image

I'm getting these two things in the photo in red, displaying the message when I hover over it: "Cannot resolve symbol".
What's the deal, how to fix it?
Thanks.

@smebly

This comment has been minimized.

Copy link

commented May 30, 2016

I've got the same thing.
I was hopeing someone would have replied with a solution

@ghost

This comment has been minimized.

Copy link

commented May 31, 2016

This is an Android Studio build issue. There are multiple ways you may be able to fix this. Checkout this StackOverflow post to see how.

@enessoftic

This comment has been minimized.

Copy link

commented Jun 1, 2016

Hey Thomas, thank you so much, I have found the solution with the help of one udacity member and by googling - and Google took me to the link you shared, where I needed only the part with adding new string.

I needed to create folder menu and also menu_main.xml. Then, I needed <String name =... in menu/res...
They actually skipped to tell us these things. 🎯

If anyone has the same problem - you have the explanation here https://discussions.udacity.com/t/quiz-setting-up-the-methods-cannot-resolve-symbol-in-mainactivity-java/170714 (*screenshots, explanations in 6, 7, 8 comments 👍 *

Thank you all.

@VishwanthY

This comment has been minimized.

Copy link

commented Jun 6, 2016

Hey Softa, there seems to be a problem with the link you shared. Can you resend it again.

@smebly

This comment has been minimized.

Copy link

commented Jun 9, 2016

if u copy and paste it into the search bar then it comes up

@nassimjd

This comment has been minimized.

Copy link

commented Jun 12, 2016

I actually got it done before this gist. Here is my working code if anyone is interested
https://gist.github.com/nassimjd/30574d12fe59ba8d206c06236ab5ed2a

@fajaradibowo

This comment has been minimized.

Copy link

commented Jul 16, 2016

this is for android studio 2.12
package com.example.android.courtcounter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}
/**
 * Increase the score for Team A by 1 point.
 */
public void addOneForTeamA(View v) {
    displayForTeamA(1);
}

/**
 * Increase the score for Team A by 2 points.
 */
public void addTwoForTeamA(View v) {
    displayForTeamA(2);
}

/**
 * Increase the score for Team A by 3 points.
 */
public void addThreeForTeamA(View v) {
    displayForTeamA(3);
}

}

@Francis-Michael

This comment has been minimized.

Copy link

commented Jul 20, 2016

thanks @fajaradibowo

@martinweaver

This comment has been minimized.

Copy link

commented Jul 20, 2016

Googling wasn't helping me at all... tried cleaning projects/syncing project with gradle files/ turning it off and on again and several other things I don't understand.

In the end I just deleted both the methods with @override public boolean written at the beginning.

@override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;

}

@override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

Now it works but I don't know what I've deleted or whether it'll come back to bite me on the arse later. :)

@aldeecura

This comment has been minimized.

Copy link

commented Aug 2, 2016

Step 1 - This code works on Android 2.12

package com.example.android.courtcounter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0;
int scoreTeamB = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    displayForTeamA(0);
    displayForTeamB(0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);}

    /**
     * Increase the score for Team A by 1 point.
     */

public void addOneForTeamA(View v) {
    scoreTeamA = scoreTeamA + 1;
    displayForTeamA(scoreTeamA);
}

/**
 * Increase the score for Team A by 2 points.
 */
public void addTwoForTeamA(View v) {
    scoreTeamA = scoreTeamA + 2;
    displayForTeamA(scoreTeamA);
}

/**
 * Increase the score for Team A by 3 points.
 */
public void addThreeForTeamA(View v) {
    scoreTeamA = scoreTeamA + 3;
    displayForTeamA(scoreTeamA);
}

/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

/**
 * Increase the score for Team B by 1 point.
 */
public void addOneForTeamB(View v) {
    scoreTeamB = scoreTeamB + 1;
    displayForTeamB(scoreTeamB);
}

/**
 * Increase the score for Team B by 2 points.
 */
public void addTwoForTeamB(View v) {
    scoreTeamB = scoreTeamB + 2;
    displayForTeamB(scoreTeamB);
}

/**
 * Increase the score for Team B by 3 points.
 */
public void addThreeForTeamB(View v) {
    scoreTeamB = scoreTeamB + 3;
    displayForTeamB(scoreTeamB);
}

/**
 * Displays the given score for Team B.
 */
public void displayForTeamB(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_b_score);
    scoreView.setText(String.valueOf(score));
}

}

@aldeecura

This comment has been minimized.

Copy link

commented Aug 2, 2016

Step 2 - creating menu_main.xml
Step 3 - update strings.xml

Below link is the steps:
https://discussions.udacity.com/t/quiz-setting-up-the-methods-cannot-resolve-symbol-in-mainactivity-java/170714/6

@lichang333

This comment has been minimized.

Copy link

commented Aug 21, 2016

In the new version of android studio, they change "blank activity" as "basic activity.

My suggestion is, don't update to new android studio.

If you has already updated, delete your project and create a new. use "basic activity" instead of "blank activity".

~~another option is create a new folder inside of res folder, set name as "menu"
then, create a new file named "menu_main.xml"

copy and paste the following code:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.android.courtcounter.MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</menu>

@SherifSalem

This comment has been minimized.

Copy link

commented Aug 26, 2016

i got these two errors, i just copied n pasted the code n i got these two errors in the java code
court counter 1
court counter 2

@SherifSalem

This comment has been minimized.

Copy link

commented Aug 26, 2016

anyone can help me with this problem?
thanks a lot

@evrisd

This comment has been minimized.

Copy link

commented Sep 3, 2016

I used an alternative for displaying the value of each button, i give ids to each button and create 1 function instead of 3 which is used on the onClick of each button :

public void scoreDisplay(View view){
switch (view.getId()) {
case R.id.three_p:
displayForTeamA(3);
break;
case R.id.two_p:
displayForTeamA(2);
break;
case R.id.free_throw:
displayForTeamA(1);
break;
default:displayForTeamA(0);
}
}

@ItIsAnismit

This comment has been minimized.

Copy link

commented Sep 11, 2016

my code is not working even i created menu_main.xml, please help me.

it says this kind of error:
Error:(8, 24) No resource found that matches the given name (at 'title' with value '@string/action_settings').

@filip123456789123

This comment has been minimized.

Copy link

commented Sep 12, 2016

if its not working for you press Alt+Enter it will give you some option choose the first one

@Vitnere

This comment has been minimized.

Copy link

commented Oct 31, 2016

Here is my code, it works without problems. Android studio 2.2.2.

`

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:padding="4dp"
    android:text="Team A" />

<TextView
    android:id="@+id/team_a_score"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:padding="4dp"
    android:text="0" />

<Button
    android:id="@+id/three"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:onClick="display3"
    android:text="+3 points" />

<Button
    android:id="@+id/two"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:onClick="display2"
    android:text="+2 points" />

<Button
    android:id="@+id/one"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:onClick="display1"
    android:text="free throw" />
`

`package com.example.android.shottracker;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

public void display3(View v) {
    displayForTeamA(3);
}

public void display2(View v) {
    displayForTeamA(2);
}

public void display1(View v) {
    displayForTeamA(1);
}

}
`

@ajayanku

This comment has been minimized.

Copy link

commented Nov 6, 2016

screenshot 46when i open my mainactivity.java only this code see in image how can i find more code?

@luisleao

This comment has been minimized.

Copy link

commented Nov 22, 2016

Para alunos que o projeto aparece erros como abaixo, isso ocorre porque a versão utilizada do Android Studio (e a criação do novo projeto) não gerou o arquivo de menu.

R.menu.menu_main faz uma referência ao arquivo menu_main.xml que deveria existir.
R.id.action_settings faz referência a um Menu Item com ID 'action_settings' que deveria estar dentro do menu_main.xml.

Para resolver você pode remover os métodos onCreateOptionsMenu e onOptionsItemSelected uma vez que não há nenhum comando relacionado aos menus no momento, ou criar um arquivo de menu novo conforme abaixo:

  1. "File" > "New" > "Android Resource File"
  2. Em "File name" informe "menu_main.xml"
  3. Em "Resource Type" escolha "Menu"
  4. Clique em "OK"
  5. Abra o arquivo de menu
  6. Na aba "Pallete" clique e arraste um "Menu Item" para a aba "Component Tree"
  7. Clique no Menu Item novo e na aba "Properties" altere o campo ID para "action_settings"

For students with the error bellow, that happens because the Android Studios' version (or at the moment the project was created) didn't created the menu file.

R.menu.menu_main refers to menu_main.xml file.
R.id.action_settings refers to a Menu Item with ID 'action_settings' supposed to exists inside menu_main.xml.

To solve that, you can remove the methods onCreateOptionsMenu and onOptionsItemSelected since they are not used to any import action at the moment, or you can create the menu file using the steps above:

  1. "File" > "New" > "Android Resource File"
  2. In "File name" fill "menu_main.xml"
  3. In "Resource Type" choose "Menu"
  4. Click "OK"
  5. Open the menu file
  6. In tab "Pallete" click and drag a "Menu Item" to "Component Tree" tab
  7. Click in the new Menu Item and change the ID field to "action_settings" at tab "Properties"

image
image

@dhamadou

This comment has been minimized.

Copy link

commented Dec 16, 2016

Hi, please help me.

I have write the code below but when i run it , i have this message:

  • Gradle tasks [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies]
  • java.lang.NullPointerException (no error message)
  • BUILD FAILED

XML code:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="4dp"
    android:text="Team A" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="4dp"
    android:text="0"
    android:id="@+id/team_a_score"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:onClick="addThreeForTeamA"
    android:text="+3 Points" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="+2 Points"
    android:onClick="addTwoForTeamA"
    />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:onClick="addOneForTeamA"
    android:text="Free throw" />

Java Code:

package com.example.android.courtcounter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

public void display3(View v) {
    displayForTeamA(3);
}

public void display2(View v) {
    displayForTeamA(2);
}

public void display1(View v) {
    displayForTeamA(1);
}

}

(https://cloud.githubusercontent.com/assets/22197925/21277215/77521734-c3d5-11e6-8a4d-52e1402e2f3c.PNG)

@fragargon

This comment has been minimized.

Copy link

commented Jan 24, 2017

xml code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.android.courtcounter.MainActivity">

    <!-- TeamA -->

 <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="4dp"
            android:text="Team A" />

        <TextView
            android:id="@+id/team_a_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="4dp"
            android:text="0" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:onClick="addThreeForTeamA"
            android:text="+3 points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:onClick="addTwoForTeamA"
            android:text="+2 points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:onClick="addOneForTeamA"
            android:text="free throw" />
    </LinearLayout>

on MainActivity.java this is the right code :

package com.example.android.courtcounter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

/**
 * Increase the score for Team A by 1 point.
 */
public void addOneForTeamA(View v) {
    displayForTeamA(1);
}

/**
 * Increase the score for Team A by 2 points.
 */
public void addTwoForTeamA(View v) {
    displayForTeamA(2);
}

/**
 * Increase the score for Team A by 3 points.
 */
public void addThreeForTeamA(View v) {
    displayForTeamA(3);
}

/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

}

@ghost

This comment has been minimized.

Copy link

commented Jan 30, 2017

How to get this grey line between Team A ans Team B?
line

@moacirsis

This comment has been minimized.

Copy link

commented Feb 26, 2017

Someone had this error.

Error:(66, 49) error: cannot find symbol variable team_a_score

@jcunanan05

This comment has been minimized.

Copy link

commented Mar 4, 2017

in the android studio 2.3, you don't need to paste onCreateOptionsMenu() and onOptionsItemSelected. just delete it and everything will be alright :) :))

@chatzi2197

This comment has been minimized.

Copy link

commented Mar 20, 2017

I had the same issue. Thank you!

@albertzubkowicz

This comment has been minimized.

Copy link

commented Apr 10, 2017

@jcunanan05 that helped :)

@robertonadj

This comment has been minimized.

Copy link

commented Apr 21, 2017

luisleao
Não consigo realizar os passo 5 a 7 arrastar o menu item até o component tree. Tentei de todas as formas e não consigo realizar essa tarefa. Poderia explicar esses passos mais detalhadamente? Obrigado

@robertonadj

This comment has been minimized.

Copy link

commented Apr 21, 2017

Eu só consegui resolver o problema, apagando os dois métodos citados. Como dito acima por um colega, no android studio 2.3 não dá pra fazer esse processo de arrastar o menu item até o component tree!

@slickr1ck

This comment has been minimized.

Copy link

commented May 9, 2017

I keep getting stuck and i yet again have had some one to respond to me :(

package com.example.android.courtcounter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

public void display3(View v) {
    displayForTeamA(3);
}

public void display2(View v) {
    displayForTeamA(2);
}

public void display1(View v) {
    displayForTeamA(1);
}

}

@tanshiv2

This comment has been minimized.

Copy link

commented Jul 1, 2017

@override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;

if (id == R.id.action_settings)

Menu and action_settings shows in red. What is the solution for this?

@iarunsingh07

This comment has been minimized.

Copy link

commented Jul 12, 2017

This should work 👍
ps I'm on android v 2.2.3

                                                     XML
                                          **********************

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  | xmlns:tools="http://schemas.android.com/tools"
  | android:layout_width="match_parent"
  | android:layout_height="match_parent"
  | android:orientation="vertical">
  |  
  | <TextView
  | android:layout_width="match_parent"
  | android:layout_height="wrap_content"
  | android:gravity="center"
  | android:padding="4dp"
  | android:text="Team A" />
  |  
  | <TextView
  | android:id="@+id/team_a_score"
  | android:layout_width="match_parent"
  | android:layout_height="wrap_content"
  | android:gravity="center"
  | android:padding="4dp"
  | android:text="0" />
  |  
  | <Button
  | android:layout_width="match_parent"
  | android:layout_height="wrap_content"
  | android:layout_margin="8dp"
  | android:onClick="addThreeForTeamA"
  | android:text="+3 Points" />
  |  
  | <Button
  | android:layout_width="match_parent"
  | android:layout_height="wrap_content"
  | android:layout_margin="8dp"
  | android:onClick="addTwoForTeamA"
  | android:text="+2 Points" />
  |  
  | <Button
  | android:layout_width="match_parent"
  | android:layout_height="wrap_content"
  | android:layout_margin="8dp"
  | android:onClick="addOneForTeamA"
  | android:text="Free throw" />
  |


                       Java 

package com.example.android.courtcounter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


/**
 * Increase the score for Team A by 1 point.
 */
public void addOneForTeamA(View v) {

    displayForTeamA(1);
}

/**
 * Increase the score for Team A by 2 points.
 */
public void addTwoForTeamA(View v) {
    displayForTeamA(2);
}

/**
 * Increase the score for Team A by 3 points.
 */
public void addThreeForTeamA(View v) {

    displayForTeamA(3);
}

/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

}


Thanks.

@Nejdanchik

This comment has been minimized.

Copy link

commented Aug 25, 2017

Made 2 steps and app worked.
Step 1: created menu_main.xml
Step 2: created id value for action_settings
Don't know why, but it works now o_O

@ignindra

This comment has been minimized.

Copy link

commented Sep 27, 2017

@ghost You've probably already found your answer, but in case anyone wondering how, you can make that thin grey vertical separator line by using this

<View android:layout_width="2px" android:layout_height="match_parent" android:background="?android:attr/listDivider" />
click here for more info + another solution

@chily11

This comment has been minimized.

Copy link

commented Dec 16, 2017

Great worked for me, after using Alt + Enter

@bebistentales

This comment has been minimized.

Copy link

commented Dec 19, 2017

Really thanks!!!! @fajaradibowo

@AdellaR

This comment has been minimized.

Copy link

commented Jan 5, 2018

Hello,

I read I a did all the changes but the app don't work on my phone .Can someone help me to understand what I did wrong ?

image






And Java

package com.example.android.courtcounter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
int scoreTeamA=0;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
/**
 * Increase the score for Team A by 1 point.
 */
public void addOneForTeamA (View theButton) {
     scoreTeamA=scoreTeamA +1;
    displayForTeamA(scoreTeamA);
}

/**
 * Increase the score for Team A by 2 points.
 */
public void addTwoForTeamA (View theButton) {
    scoreTeamA=scoreTeamA +2;
    displayForTeamA(scoreTeamA);
}

/**
 * Increase the score for Team A by 3 points.
 */
public void addThreeForTeamA(View theButton) {
    scoreTeamA=scoreTeamA +3;
    displayForTeamA(scoreTeamA);
}

/**
 * Increase the score for Team A by 3 points.
 */

/**
 * Displays the given score for Team A.
 */

@AdellaR

This comment has been minimized.

Copy link

commented Jan 5, 2018

When I run the app on my phone keeps stopping !

@felipedurli

This comment has been minimized.

Copy link

commented Jan 10, 2018

For those who are getting errors. It worked for me with this code on MainActivity.java;

just past this code below:

package com.example.android.courtcounter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

/**
 * Increase the score for Team A by 1 point.
 */
public void addOneForTeamA(View view) {
    displayForTeamA(1);
}

/**
 * Increase the score for Team A by 2 points.
 */
public void addTwoForTeamA(View view) {
    displayForTeamA(2);
}

/**
 * Increase the score for Team A by 3 points..
 */
public void addThreeForTeamA(View view) {
    displayForTeamA(3);
}

/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

}

@felipedurli

This comment has been minimized.

Copy link

commented Jan 10, 2018

@AdellaR I guess you need to change the onClick writing to addOneForTeamA, instead of addOneFromTeamA. That's why when you click it doesn't call on the MainActivity.java where it says addOneForTeamA.

@kenehiz

This comment has been minimized.

Copy link

commented Jan 12, 2018

@nassimjd thank you! Your code is a life saver. It works perfectly well.

@izissoft

This comment has been minimized.

Copy link

commented Jan 22, 2018

Way of felipedurli is correct and work properly :) Thanks

@laranylara

This comment has been minimized.

Copy link

commented Jan 23, 2018

We had some difficulties with the Java codes too. First we tried to solve the problem in different ways but nothing helped. Then we tried to solve the problem by copying the Java codes that were provided here but it didn't work either.
Then we found that we had to delete some lines and then it worked finally! Our codes look the following:

package com.example.android.courtcounter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


/**
 * Increase the score for Team A by 1 point.
 */
public void addOneForTeamA(View v) {
    displayForTeamA(1);
}

/**
 * Increase the score for Team A by 2 points.
 */
public void addTwoForTeamA(View v) {
    displayForTeamA(2);
}

/**
 * Increase the score for Team A by 3 points.
 */
public void addThreeForTeamA(View v) {
    displayForTeamA(3);
}

/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

}

@dainantonio

This comment has been minimized.

Copy link

commented Jan 26, 2018

I had to delete the code lines in red since we are not using anyways. It worked fine after deleting.

@mathsways

This comment has been minimized.

Copy link

commented Feb 2, 2018

thank you @lyuisleao you help me and i resolv the problem

@abdullahelsheikh

This comment has been minimized.

Copy link

commented Feb 6, 2018

thanks

@abdullahelsheikh

This comment has been minimized.

Copy link

commented Feb 7, 2018

1- xml code

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        tools:context="com.example.android.courtcounter.MainActivity">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif"
            android:gravity="center_horizontal"
            android:padding="14dp"
            android:text="Team A"
            android:textColor="#616161"
            android:textSize="25sp" />

        <TextView
            android:id="@+id/team_a_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif"
            android:gravity="center_horizontal"
            android:padding="16dp"
            android:text="0"
            android:textColor="#000000"
            android:textSize="56sp" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:onClick="threePointForTeamA"
            android:text="+3 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:onClick="twoPointForTeamA"
            android:text="+2 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:onClick="onePointForTeamA"
            android:text="Free throw" />
    </LinearLayout>

    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_marginTop="16dp"
        android:background="@android:color/darker_gray" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        tools:context="com.example.android.courtcounter.MainActivity">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif"
            android:gravity="center_horizontal"
            android:padding="14dp"
            android:text="Team B"
            android:textColor="#616161"
            android:textSize="25sp" />

        <TextView
            android:id="@+id/team_b_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif"
            android:gravity="center_horizontal"
            android:padding="16dp"
            android:text="0"
            android:textColor="#000000"
            android:textSize="56sp" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:onClick="threePointForTeamB"
            android:text="+3 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:onClick="twoPointForTeamB"
            android:text="+2 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:onClick="onePointForTeamB"
            android:text="Free throw" />
    </LinearLayout>
</LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="24dp"
    android:layout_marginRight="24dp"
    android:onClick="resetScore"
    android:text="Reset" />

2- java code :

package com.example.android.courtcounter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
int pointA = 0;
int pointB = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
/**
 * Displays three scores for Team A.
 */
public void onePointForTeamA(View view) {
    pointA  = pointA + 1;
    displayForTeamA(pointA);
}
/**
 * Displays two scores for Team A.
 */
public void twoPointForTeamA(View view) {
    pointA  = pointA + 2;
    displayForTeamA(pointA);
}
/**
 * Displays one score for Team A.
 */
public void threePointForTeamA(View view) {
    pointA  = pointA + 3;
    displayForTeamA(pointA);
}
/**
 * Displays three scores for Team A.
 */
public void onePointForTeamB(View view) {
    pointB  = pointB + 1;
    displayForTeamB(pointB);
}
/**
 * Displays two scores for Team A.
 */
public void twoPointForTeamB(View view) {
    pointB  = pointB + 2;
    displayForTeamB(pointB);
}
/**
 * Displays one score for Team A.
 */
public void threePointForTeamB(View view) {
    pointB  = pointB + 3;
    displayForTeamB(pointB);
}
public void resetScore(View v){
    displayForTeamA(0);
    displayForTeamB(0);
    pointA = 0;
    pointB = 0;

}
/**
 * This method displays the given quantity value on the screen.
 */
private void displayForTeamA(int number) {
    TextView quantityTextView = (TextView) findViewById(R.id.team_a_score);
    quantityTextView.setText("" + number);
}
/**
 * This method displays the given quantity value on the screen.
 */
private void displayForTeamB(int number) {
    TextView quantityTextView = (TextView) findViewById(R.id.team_b_score);
    quantityTextView.setText("" + number);
}

}

3- style code :

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#FF9800</item>
    <!-- Background color of buttons in the app -->
    <item name="colorButtonNormal">#FF9800</item>
</style>

screenshot_ - - - - -

@azzurri21

This comment has been minimized.

Copy link

commented Feb 11, 2018

for android studio 3.0.1
the xml

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Team A"
    android:gravity="center_horizontal"
    android:padding="4dp"/>

<TextView
    android:id="@+id/team_a_score"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="0"
    android:padding="4dp"
    android:gravity="center_horizontal"/>

<Button
    android:id="@+id/AddThreePoints"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="3 points"
    android:layout_margin="8dp"
    android:gravity="center_horizontal"
    android:onClick="AddThreePoints"/>

<Button
    android:id="@+id/AddTowPoints"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="2 points"
    android:layout_margin="8dp"
    android:onClick="AddTowPoiunts"/>

<Button
    android:id="@+id/AddOnePoint"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="free throw"
    android:layout_margin="8dp"
    android:onClick="AddOnePoint"/>

the java main activity

package com.example.lenovo.courtcounter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    displayForTeamA(0);
}


public void AddThreePoints (View view){
    displayForTeamA(3);
}

public void AddTowPoiunts(View view){
    displayForTeamA(2);
}
public void AddOnePoint (View view){
    displayForTeamA(1);
}

/*
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

}

@abdahma01

This comment has been minimized.

Copy link

commented Feb 14, 2018

package com.example.android.courtcounter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    displayForTeamA(8);
}
public void addThreeForTeamA(View v){
    displayForTeamA(3);
}
public void addTwoForTeamA(View v){
    displayForTeamA(2);
}
public void addOneForTeamA(View v){
    displayForTeamA(1);
}
/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

}

@Eng-navyman

This comment has been minimized.

Copy link

commented Feb 25, 2018

you can check the code from here if you have any errors
https://gist.github.com/Eng-navyman/7882860eb16d437cd884647597673341

@JRicardoRomero

This comment has been minimized.

Copy link

commented Mar 8, 2018

Actually what I think that happened and created so much confusion among us is that Layla started working not with a Blank/Empty activity but with one that included a menu, hence all those methods and item that are nowhere to be found on an empty activity. SO no problem, you really don't need them, just disregard them.

@tarsbirsingh

This comment has been minimized.

Copy link

commented Mar 21, 2018

You can check if any error in Your`program

screenshot_20180321-212110
`
``

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Team A"
    android:gravity="center_horizontal"
    android:padding="8dp"/>
<TextView
    android:id="@+id/team_a_score"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="0"
    android:gravity="center_horizontal"
    android:padding="8dp"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="+3 POINTS"
    android:onClick="addThreeForTeamA"
    android:layout_margin="20dp"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="+2 POINTS"
    android:onClick="addTwoForTeamA"
    android:layout_margin="20dp"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="FREE THROW"
    android:onClick="addOneForTeamA"
    android:layout_margin="20dp"/>

``package com.example.android.courtcounter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void addOneForTeamA(View v){
    scoreTeamA = scoreTeamA+1;
    displayForTeamA(scoreTeamA);
}
public void addTwoForTeamA(View v){
    scoreTeamA = scoreTeamA+3;
    displayForTeamA(scoreTeamA);
}
public void addThreeForTeamA(View v){
    scoreTeamA = scoreTeamA+1;
displayForTeamA(scoreTeamA);
}
/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(scoreTeamA));
}

}

@quochungedu

This comment has been minimized.

Copy link

commented Mar 22, 2018

It depends on the Android Studio Version you work with. Since the codes the OP uploaded some years ago, the codes may not always be working with a newer Android Studio. For Android Studio 3.0.1, these codes work fine:

`

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:padding="8dp"
    android:text="Team A" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:padding="8dp"
    android:text="0"
    android:id="@+id/team_a_score"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:text="+ 3 points"
    android:onClick="addThreeForTeamA" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:text="+ 2 points"
    android:onClick="addTwoForTeamA" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:text="Free Throw"
    android:onClick="addOneForTeamA" />

`


`

package com.example.android.courtcounter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

/**
 * Increase the score for Team A by 1 point.
 */
public void addOneForTeamA(View v) {
    displayForTeamA(1);
}

/**
 * Increase the score for Team A by 2 points.
 */
public void addTwoForTeamA(View v) {
    displayForTeamA(2);
}

/**
 * Increase the score for Team A by 3 points.
 */
public void addThreeForTeamA(View v) {
    displayForTeamA(3);
}

/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

}

`


When you copy and paste the codes, remember to omit the ' at the beginning and the end of each code, if it appears. Good luck!

@lakshmiprasannatirumala

This comment has been minimized.

Copy link

commented Mar 31, 2018

It has been very interesting working with court counter app..
This is my app.
capture

@shoooka

This comment has been minimized.

Copy link

commented May 13, 2018

i have this problem "red line in menu "
and thanks for all that is solve
In the new version of android studio, they change "blank activity" as "basic activity.

My suggestion is, don't update to new android studio.

If you has already updated, delete your project and create a new. use "basic activity" instead of "blank activity".

@salah27

This comment has been minimized.

Copy link

commented Jun 9, 2018

As I tried before i find the solution for the two problems
-Problem one "Red Line In Menu"
it can be solved by add menu_main.xml

-problem two " Red Line in action_setting"
Just change it to "action_text" it will work fine^^

Please inform me if its not work ^^

@kxngnathan

This comment has been minimized.

Copy link

commented Jun 13, 2018

@Vitnere your a life saver

@raphaelrow

This comment has been minimized.

Copy link

commented Jun 26, 2018

Try removing the parts that show the error.
That is remove this:

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

The code will work just fine

@itkarpathy

This comment has been minimized.

Copy link

commented Aug 1, 2018

Hi Guys,

If you have any bug on your app please check these details:

  1. make sure that your id name is : "team_a_score".
  2. Don't use the menu items rows because that is makes a bug because you don't have menu item in the xml.

xml:
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">Li

<TextView
    android:id="@+id/team_a_score"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="4dp"
    android:text="0" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="+3 Points"
    android:onClick="submit3"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="+2 Points"
    android:onClick="submit2"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="Free throw"
    android:onClick="submitFreeThrow"/>

Java:

package com.example.android.courtcounter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    }
public void submit3 (View view){
    displayForTeamA(3);
}

public void submit2 (View view) {
    displayForTeamA(2);
}
public void submitFreeThrow (View view){
    displayForTeamA(1);
}

public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

}

Just works well!

@youmei0426

This comment has been minimized.

Copy link

commented Aug 4, 2018

public void addOneForTeamA(View v) {
displayForTeamA(1);
}

public void addTwoForTeamA(View v) {
displayForTeamA(2);
}

public void addThreeForTeamA(View v) {
displayForTeamA(3);
}

I thought all we need to do for the display is display(1) or display (2), for example. So I don't understand why we need to write displayFor
TeamA(1)? When and where was ForTeamA declared in the code?

Thanks!

@Dharmesh40

This comment has been minimized.

Copy link

commented Sep 3, 2018

if you are getting red action_settings..... use this code..

package com.example.android.courtcounter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    displayForTeamA(8);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.actions) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * Increase the score for Team A by 1 point.
 */
public void addOneForTeamA(View v) {
    displayForTeamA(1);
}

/**
 * Increase the score for Team A by 2 points.
 */
public void addTwoForTeamA(View v) {
    displayForTeamA(2);
}

/**
 * Increase the score for Team A by 3 points.
 */
public void addThreeForTeamA(View v) {
    displayForTeamA(3);
}

/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

}

i know you want xml also..... here you go

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="4dp"
    android:text="Team A" />

<TextView
    android:id="@+id/team_a_score"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="4dp"
    android:text="0" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:onClick="addThreeForTeamA"
    android:text="+3 Points" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:onClick="addTwoForTeamA"
    android:text="+2 Points" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:onClick="addOneForTeamA"
    android:text="Free throw" />
@OmimaAlazaka

This comment has been minimized.

Copy link

commented Oct 23, 2018

for MainActivity.java
this is my code
package com.example.android.countcounter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    displayForTeamA(8);
}

public void Button1(View view) {
    int Quantity = 3;
    display(Quantity);
}


public void Button2(View view) {
    int Quantity = 2;
    display(Quantity);
}

public void Button3(View view) {
    int Quantity = 1;
    display(Quantity);
}


/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

private void display(int quantity) {
    TextView quantityTextView = (TextView) findViewById(R.id.team_a_score);
    quantityTextView.setText("" + quantity);
}

}
and this is for activity_main.xml

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Team A"
    android:gravity="center_horizontal"
    android:padding="4dp" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="0"
    android:gravity="center_horizontal"
    android:padding="4dp"
    android:id="@+id/team_a_score"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="+ 3 Points"
    android:gravity="center_horizontal"
    android:padding="4dp"
    android:onClick="Button1"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="+ 2 Points"
    android:gravity="center_horizontal"
    android:padding="4dp"
    android:onClick="Button2"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Free Throw"
    android:gravity="center_horizontal"
    android:padding="4dp"
    android:onClick="Button3"/>
and the same output i have , but i cannot understand this lines that you wrote , So can anyone help me??

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

@override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
  return true;
}

return super.onOptionsItemSelected(item);

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.