i need to know, how to recognize, which button is pressed.
Like if i have two buttons ,say button 1 and button2,and both of them performing the same method, say method(),how to determine which button pressed ?
Regards
Most ellegant pattern to follow:
public void onClick(View v) {
switch(v.getId())
{
case R.id.button_a_id:
// handle button A click;
break;
case R.id.button_b_id:
// handle button B click;
break;
default:
throw new RuntimeException("Unknow button ID");
}
This way it's much simplier to debug it and makes sure you don't miss to handle any click.
OR... you can just put a android:onClick="foo" in the xml code of the
button, and define a method on java with the signature. Inside the
method foo, get the id and compare it with the one you need
public void foo(View v){
if (v.getId() == R.id.yourButton){
}
else if (v.getId() == R.id.nextButton){
}
}
I have 10 buttons performing the same method updateText(), I used this code to get the clicked button's text:
public void updateText(View v){
Button btn = (Button) findViewById(v.getId());
String text = btn.getText().toString();
}
If by "performing the same method" you mean theirs OnClickListener then you have to reference the parameter being passed to it.
public void onClick(View v) {
if(v==btnA) {
doA();
} else if(v==btnB) {
doB();
}
}
Ok got the solution
if (yesButton.getId() == ((Button) v).getId()){
// remainingNumber
}
else if (noButton.getId() == ((Button) v).getId())
{
// it was the second button
}
Related
I have a method which on button click needs to perform in a specific way and if no button is clicked, it should alternatively act differently.
Basically on clicking on the button, it should destroy my service. And if I don't click on it, it will eventually be destroyed. The thing is that I want the destroy method act differently based on these 2 scenarios.
You could add the clicked View as method argument:
myOnClick(View view){
if(view != null){
//Button was clicked
....
}else{
//No Button was clicked
.....
}
}
Is the method performing periodically? Then just add a bool to see if the button was clicked before.
like:
Main:
boolean clicked = false;
onCreate:
Button b = (Button)findViewById(R.id.buttonName);
b.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if(clicked){
clicked = false;
}else{
clicked = true;
}
});
}
I have 2 activities, A and B.
In activity A I have radiogroup, a CHECK button and NEXT button .
before I can press the NEXT button , I want to choose first one of the radio button in the radio group and CHECK it first so that I can know if its correct or incorrect before I can press the next button.
here's my code:
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (radio1.isChecked() || radio2.isChecked() || radio3.isChecked()) {
if (btnCheck.isPressed()) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(i);
}
} else {
return;
}
}
});
I can't press the NEXT button even I have chosen one of the radio button and check it.
Please help.
Your answers will be appreciated.
Thank You.
I'm not sure what the problem is, but you could just create a boolean to check if you're concerned that isPressed() isn't working as you'd expect.
boolean hasChecked = false;
btnCheck.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// put conditions here to check if it was pressed at the right time
hasChecked = true;
}
});
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (radio1.isChecked() || radio2.isChecked() || radio3.isChecked()) {
if (hasChecked) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(i);
}
}
}
});
if (radio1.isChecked() || radio2.isChecked() || radio3.isChecked()) {
//Do something here.
}
The above if you want to check if any one of the radio buttons is checked.
if (radio1.isChecked() && radio2.isChecked() && radio3.isChecked()) {
//Do something here.
}
The above if you want to check if all the radio buttons are checked.
If I understand you correctly, you also want to check some btnChk's click, before you press NEXT. So put a FLAG (Global Variable) in your btnChk's onClick method, if you want to log that if that particular button was presed.
If you want anyone of the radio button checks to be true, use the || condition in the if statement. Then to check if btnChk was pressed do the following:
Declare a Global FLAG at just below the class declaration:
public int FLAG = 0;
Set a buttonclick event on your button in onCreate():
Button btnChk = (Button)findViewById(R.id.YourId);
btnChk.setOnClickListener(btn);
private View.OnClickListener btn = new View.OnClickListener() {
#Override
public void onClick(View v) {
FLAG =1;
}
};
Then in your NEXT button check the following:
if (radio1.isChecked() || radio2.isChecked() || radio3.isChecked()) {
if(FLAG ==1){
//Do something here.
}
}
What the FLAG does: FLAG is init to zero initially, as soon as you press btnChk, FLAG becomes 1, if btnChk is not pressed, FLAG remains 0. so in the button click of Next button you check the value of FLAG and do your stuff accordingly.
In part of my program, I change the id and text of a button when it is clicked. When the button is clicked again, the id and text are reverted to the previous values. However, I am getting error. (I have added a comment to the line where I am getting the error).
I just need to change text by clicking a button. Then change it back to the old values by clicking it again. Does anyone have a solution or some better idea on how to accomplish this?
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_start_again:
mPlayer.stop();
start.setText("Pause");
start.setId(R.id.pause);
break;
case R.id.pause:
start.setText("Pause"); //here it is not accepting pause
start.setId(R.id.btn_start_again);
break;
}
}
I don't think you want to change the id of the Button. What it looks like you are doing is having a Button that starts play then turns to pause while playing. Simply have the two Buttons in the same space in your xml. Start with the visibility of the pause Button as gone then check the visibility in the function.
Something like
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_start_again:
mPlayer.stop();
btnPause.setVisibility(View.VISIBLE);
btnPlay.setVisibility(View.GONE);
break;
case R.id.pause:
btnPause.setVisibility(View.GONE);
btnPlay.setVisibility(View.VISIBLE);
break;
}
Button Docs
You don't need to change the ID of the button, use something like a boolean flag to keep track of the state of the button:
boolean isPlaying = false;
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_start_again:
if(isPlaying){
mPlayer.stop();
start.setText("Play");
isPlaying = !isPlaying;
}else{
//mPlayer.start() <--- you don't start it anywhere?
start.setText("Pause");
isPlaying = !isPlaying;
}
break;
}
}
There a better way you can do it.
Set different tag to the button according to the status.
changing ID programmatically is a bad idea, better solution is to define a global boolean value:
private boolean isSelected = false;
and then in Your onClick:
public void onClick(View v) {
if(isSelected==false){
mPlayer.stop();
start.setText("Pause");
isSelected=true;
}else{
start.setText("Pause");
isSelected=false;
}
you could just have 1 button which can handle different events and change its behaviour based upon the current button text.
public void onClick(View v) {
switch(v.getText()){
case "Pause":
mPlayer.stop();
start.setText("Play");
break;
case "Play":
start.setText("Pause");
mPlayer.play();
break;
}
}
or you could have the button initally setup to fire 1 click event which once fired then sets the button click event to the second click event and vice versa
public void onClickOne()
{
// do stuff
btn.OnClick = onClickTwo();
}
public void onClickTwo()
{
// do stuff
btn.OnClick = onClickOne
}
I ended up doing it in this way:
String value=start.getText().toString();
if(value.equals("Start")){
start.setText("Pause");
}
else if(value.equals("Pause")){
start.setText("Start");
}
This question already has answers here:
android: How to get the ID of a textview when clicked?
(2 answers)
Closed 9 years ago.
I'm using MyActivity extends Activity implements OnClickListener{
This activity references around 10+ buttons and has setOnClicklistener(this) method called on every button.
#Override
public void onClick(View v){
//here I need to get the id of the view that was clicked...
//Depending on the button that was clicked different actions need to be called...
//How do I get the ID of the button that was clicked...
}
#Override
public void onClick(View v){
switch(v.getId()){
case R.id.btnCancel:
//your code for onclick of that button
break;
}
you can use following method to get id.
v.getId()
#Override
public void onClick(View v){
int id = v.getId();
if(id == R.id.button_ok){
}
}
The View parameter that is sent to your onClick method is the actual button that was clicked, therefore you can check which one it is, for example:
#Override
public void onClick(View v){
switch(v.getId()) {
case R.id.button_1: ...; break;
case R.id.button_2: ...; break;
case R.id.button_3: ...; break;
...
default: //unknown button clicked
}
}
This is only one option, there are other. Search google for more info.
use :
if(v.getId()==R.id.whatever)
{
// do something
}
or you can even use :
Button btn = (Button)findViewById(R.id.btn);
if(v==btn)
{
// do something
}
but the second one is not recommended.
Instead of have to call separate method for each button, would it be possible to just use one method and check which button it was that was clicked by it's id?
I'm using the simple way like android:onClick="activateButtons"
public void activateButtons(View v)
{
if(?? == btn_1)
{
Code for button with ID = btn_1
}
if(?? == btn_1)
{
Code for button with ID = btn_2
}
}
You can do that if you tag the buttons with an Id. Set the android:id="#+id/yourbuttonid" in the xml file. Then do if(v.getId() == R.id.yourbuttonid) in the onClick method.
Yes it is possible. You can assign specific ids(eg: button1) for buttons and then call v.getId() in activateButtons(onclick method) to check if it is equal to R.id.button1 and then do your operation there.
Cheers,
Richie
public void onClickKeyPad(View view)
{
EditText text = (EditText)findViewById(R.id.text_password);
switch(view.getId())
{
case R.id.button_0:
text.setText(text.getText() + "0");
break;
case R.id.button_1:
text.setText(text.getText() + "1");
break;
case R.id.button_cancel:
finish();
break;
default:
break;
}
}