I have a widget whos designing is now complete and consists of 4 buttons ,I want to get which button on the widget was clicked and based on the button clicked want to pass a value to the service where I take necessary actions on it.
Asked
Active
Viewed 73 times
0
-
what you want to do.. whether u want to get the text of the button which is clicked.. – user1835052 Mar 20 '13 at 05:56
-
I need to call a public method in another accessible class that this I have all done already but now I just need to get the onClick over the widget button and then based on the button that was clicked I call a method. – Naaz Mar 20 '13 at 06:01
-
so if you get the text of the button which is clicked based on that text you can call the required method right.. if yes i ll post code for that. – user1835052 Mar 20 '13 at 06:06
-
yes please! you can post code example – Naaz Mar 20 '13 at 06:11
2 Answers
0
your code should be like this
public class MainActivity extends Activity implements OnClickListener {
Button btn0,btn1,btn2,btn3;
btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(btnrunclick=true)
{
Button b = (Button)v;
buttonText = b.getText().toString();// this will return button text which is clicked
if(buttonText..equalsIgnoreCase("your text"))
{
//call required method for that button
}
else if(buttonText.equalsIgnoreCase("next button text"))
{
//call required method for that button
}
}
}

user1835052
- 455
- 1
- 5
- 11
-1
You can do this by implementing View.OnClickListener
& assigning it to all the 4 buttons you have like. I have not tried this yet but maybe you can give it a shot: -
View.OnClickListener listener = new View.OnCLickListener () {
public void onClick(View v) {
if (v instanceof Button && (Button) v == btn1) {
// Do something
} else if (v instanceof Button && (Button) v == btn2) {
// Do something
}
}
}
btn1.setOnClickListener(listener);
btn2.setOnClickListener(listener);

Ashish Gupta
- 242
- 1
- 6