Reputation:
I have got a group of buttons on a activity.When I click on a button it sets a text on textview and it changes the background color of the button. Then again when I press the same button it will go to the next activity.The below code does these actions. I have changed the text of button and do the second click action. Is there a better way to do it without changing text.
In the code I have shown for 1 button. In the same way I do it for other buttons.
Activity
public class Activity2 extends ActionBarActivity implements View.OnClickListener {
Button R1btn, R2btn, R3btn;
private TableLayout tableLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
tableLayout=(TableLayout)findViewById(R.id.tl);
R1btn = (Button) findViewById(R.id.button1);
R1btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.button1:
Button button1 = (Button)view;
String btnText = button1.getText().toString();
if (btnText.equals("1")) {
R1btn.setBackgroundColor(Color.RED);
R2btn.setBackgroundColor(Color.GREEN));
R3btn.setBackgroundColor(Color.GREEN));
TextView rTV = (TextView) findViewById(R.id.rTV);
rTV.setText(R1);
button1.setText("1 ");
} else if (btnText.equals("1 ")) {
Intent intent = new Intent(Activity2.this, Activity3.class);
startActivity(intent);
button1.setText("1");
break;
}
}
}
Upvotes: 0
Views: 110
Reputation: 153
Here is a example someone created his own Button class that changes from play to pause icon.
This is a pretty nice way, because all the code is on one central spot.
You just need to add a static boolean to "remember" if the button is now on "play or pause"
Then you can check it (e.g in method UpdateState) and change appearance depending on current state.
static boolean isPlay = true;
public void updateState() {
if (isPlay) {
//Change appearance
isPlay = false;
} else {
//Change appearance
isPlay=true;
}
}
Upvotes: 0
Reputation: 2339
// initialize value
boolean isButton1 = false;
// click method
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.button1:
isButton1 != isButton1;
if (isButton1) {
R1btn.setBackgroundColor(Color.RED);
R2btn.setBackgroundColor(Color.GREEN));
R3btn.setBackgroundColor(Color.GREEN));
TextView rTV = (TextView) findViewById(R.id.rTV);
rTV.setText(R1);
} else {
Intent intent = new Intent(Activity2.this, Activity3.class);
startActivity(intent);
}
break;
}
}
Upvotes: 0
Reputation: 2535
By Using tag of your button you will be able to achieve your goal. Sample code is below:
Button button1 = (Button)view;
String btnTag= button1.getTag().toString();
// For First Time when button is clicked
if (btnTag.equals("") || btnTag.equals("CLICKED2")) {
R1btn.setBackgroundColor(Color.RED);
R2btn.setBackgroundColor(Color.GREEN));
R3btn.setBackgroundColor(Color.GREEN));
button1.setTag("CLICKED1");
} else if (btnTag.equals("CLICKED1")) {
button1.setTag("CLICKED2");
Intent intent = new Intent(Activity2.this, Activity3.class);
startActivity(intent);
}
break;
Upvotes: 1
Reputation: 1001
You can define static boolean variables which will be mapped to the corresponding buttons. On click of the button make the variable true. Depending on the variable value you can do your actions.No need to change the text of the buttons.
Upvotes: 0
Reputation: 1530
use Long click
button = (Button) findViewById(R.id.button_view);
button .setOnClickListener(new OnClickListener() {
public void onClick(View v) {
shortclick();
}
});
button .setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
longclick();
return true;
}
});
public void shortclick()
{ Toast.makeText(this, "Why did you do that? That hurts!!!", Toast.LENGTH_LONG).show();
}
public void longclick()
public void shortclick()
and other single Click
Upvotes: 0