SagiAharon
SagiAharon

Reputation: 13

Switch case in Android app

Here is the code that supposed to open a dialog box when i press the About button in my min activity page. But nothing really happens. : /

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

        View continueButton = findViewById(R.id.continue_button);
        continueButton.setOnClickListener(this);

        View newgameButton = findViewById(R.id.new_button);
        continueButton.setOnClickListener(this);

        View aboutButton = findViewById(R.id.about_button);
        continueButton.setOnClickListener(this);

        View exitButton = findViewById(R.id.exit_button);
        continueButton.setOnClickListener(this);




public void onClick(View v) {

        switch (v.getId()) {
        case R.id.continue_button:
                break;
        case R.id.about_button:     
                Intent i = new Intent(this, About.class);
                startActivity(i);
                break;
        case R.id.new_button: 
                openNewGameDialog(); 
                break;
        case R.id.exit_button:
                finish();
                break;  


        }       
    }

Main Activity XML

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:background="@color/background"
   android:layout_height="fill_parent"
   android:layout_width="fill_parent"
   android:padding="30dip"
   android:orientation="horizontal" >
<LinearLayout
   android:orientation="vertical"
   android:layout_height="wrap_content"
   android:layout_width="fill_parent"
   android:layout_gravity="center" >
 <TextView
   android:text="@string/main_title"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:layout_gravity="center"
   android:layout_marginBottom="25dip"
   android:textSize="24.5sp" />
 <Button
   android:id="@+id/continue_button"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/continue_label" />
 <Button
   android:id="@+id/new_button"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/new_game_label" />
 <Button
   android:id="@+id/about_button"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/about_label" />
 <Button
   android:id="@+id/exit_button"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/exit_label" />
</LinearLayout>
</LinearLayout>

Example of a button: About

About Activity

public class About extends Activity {

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

    }

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

}

About XML

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip" >
<TextView
android:id="@+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/about_text" />
</ScrollView>

Upvotes: 1

Views: 29666

Answers (3)

Harry
Harry

Reputation: 1472

Here's your problem:

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

    View continueButton = findViewById(R.id.continue_button);
    continueButton.setOnClickListener(this);

    View newgameButton = findViewById(R.id.new_button);
    **newgameButton**.setOnClickListener(this);

    View aboutButton = findViewById(R.id.about_button);
    **aboutButton**.setOnClickListener(this);

    View exitButton = findViewById(R.id.exit_button);
    **exitButton**.setOnClickListener(this);
}

Only your 'continueButton' has an onClickListener registered...

Also: It is good practice to also have a default case in a switch statement which gets executed if none of the cases hold. Add a default case with a log message or something and see if it gets executed.

Upvotes: 2

damian
damian

Reputation: 2011

First, make sure your activity implements View.OnClickListener interface.

Secondly, try to add @Override before the public void onClick(View view) { } method declaration. If this does not help, try to add a log statement at top of the onClick method, for example logging the ID of the view, and compare it to the ones you're trying to match it against.

Upvotes: 1

NARESH REDDY
NARESH REDDY

Reputation: 682

As far as i can say, you need to call button.setOnClickListener(this); in your activity onCreate method

Upvotes: 0

Related Questions