user2183779
user2183779

Reputation:

Adding second onClick method in Android Studio?

I've just started to learn Java and I've been stumped on adding the code for a second button in an activity. I apologize for my (possible dumb question) and any wrong terminology.

Here is the MainActivity Java code:

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

        Button btnGo = (Button) findViewById(R.id.btnGo);
        btnGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, GoActivity.class));
            }
        });
    }
}

How can I add to this code for btnEscape to go to EscapeActivity?

Upvotes: 0

Views: 1596

Answers (3)

DSlomer64
DSlomer64

Reputation: 4283

In code, what the two comments by Shiram and Nik above are saying is to add the block below that begins Button btnEscape... after Button btnGo.setOnclick's block.

@Override
protected void onCreate(Bundle savedInstanceState) {
...
    Button btnGo = (Button) findViewById(R.id.btnGo);
...
    Button btnEscape = (Button) findViewById(R.id.<<name of button in xml>>);
    btnEscape.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ... whatever ...            }
    });
}

In short, the following is a really good pattern to have at your fingertips:

    Button btn___ = (Button) findViewById(R.id.<<name of button in xml>>);
    btn___.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ... whatever ...            }
    });

Upvotes: 0

Aakash
Aakash

Reputation: 5261

The optimised way to do that is implement your class with View.OnClickListener and override the method onClick and inside it use switch cases to switch between views and apply on clicks like this:

public class SampleActivity extends AppCompatActivity implements View.OnClickListener{
Button btnGo,btnEscape;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnGo = (Button) findViewById(R.id.btnGo);
        btnEscape= (Button) findViewById(R.id.btnEscape);
        btnGo.setOnClickListener(this);
        btnEscape.setOnClickListener(this);
}
@Override
    public void onClick(View v) {
        switch (v.getId()){
      case R.id.btnGo:
      startActivity(new Intent(MainActivity.this, GoActivity.class));
       break;
     case R.id.btnEscape:
      startActivity(new Intent(MainActivity.this, EscapeActivity.class));
      break;
     default:
      break;
     }
    }
}

Upvotes: 1

Kamil Kamiński
Kamil Kamiński

Reputation: 389

You just have to do exacly the same thing you do with btnGo - this is find your button by id, and then set clickListener to it. It could looks like that:

 Button btnEscape = (Button) findViewById(R.id.btnEscape);
        btnEscape.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, EscapeActivity.class));
            }
        });

Upvotes: 2

Related Questions