Multiple Button Event Android Example
In my previous example i shown that how to generate an event on a button. i give a small recap here
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Write here what you want on a click event
}
});
}
}
Multiple Button Event Android Example
Now i am going to teach how to use multiple button:
Step 1: Take more than one Button on Layout File
android :onClick="setColor": using this tag we specified method name that call on click event from xml side.
Step 2: we have to specified method setColor(View v){}
public void setColor(View v)
{
switch(v.getId())
{
case R.id.button1 :
background.setBackgroundColor(getResources().getColor(R.color.red));
break;
case R.id.button2 :
background.setBackgroundColor(Color.BLUE);
break;
case R.id.button3 :
background.setBackgroundColor(getResources().getColor(R.color.black));
break;
case R.id.button4 :
background.setBackgroundColor(Color.WHITE);
break;
case R.id.button5 :
background.setBackgroundColor(getResources().getColor(R.color.gold));
}
}
No comments:
Post a Comment