Reputation: 14950
I have created 3 buttons in my application which when clicked, will go to the method giveClue.
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="24dp"
android:layout_height="22dp"
android:layout_alignTop="@+id/lifeButtonsLbl"
android:layout_toLeftOf="@+id/ImageButton2"
android:src="@drawable/icon"
android:onClick="giveClue" />
<ImageButton
android:id="@+id/ImageButton2"
android:layout_width="24dp"
android:layout_height="22dp"
android:layout_alignTop="@+id/imageButton1"
android:layout_toLeftOf="@+id/ImageButton3"
android:src="@drawable/icon"
android:onClick="giveClue"/>
<ImageButton
android:id="@+id/ImageButton3"
android:layout_width="24dp"
android:layout_height="22dp"
android:layout_alignRight="@+id/frameLayout1"
android:layout_alignTop="@+id/ImageButton2"
android:src="@drawable/icon"
android:onClick="giveClue" />
NOTE: I don't want to use different method for the
ACTIVITY CLASS:
public void giveClue(View view) {
gameAdapter.giveClue(game);
}
My problem is that I want to disable the button that was clicked.I don't want to use different method for each button. Is it possible to determine which button was clicked so I can disable it.
Thank you.
Upvotes: 3
Views: 4595
Reputation: 679
Implement one method as follows which gets the id's of child objects of layout
public static void ButtonBar(final Activity a) {
//if linear instantiate LinearLayout, if RelativeLayout instantiate RelaytiveLayout
final LinearLayout ll = (LinearLayout) a.findViewById(R.id.your_layout_id);
for (int i = ll.getChildCount() - 1; i >= 0; i--) {
View v = ll.getChildAt(i);
v.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.ImageButton1:
//disable here
break;
case R.id.ImageButton2:
//disable here
break;
case R.id.ImageButton3:
//disable here
break;
}}});
}
}
In giveClue method just call the above method..
public void giveClue(View view) {
ButtonBar(this);
// gameAdapter.giveClue(game);
}
I think this may works..
Upvotes: 0
Reputation: 1479
Have your class implement `View.OnClickListener', like
public class MyActivity extends Activity implements View.OnClickListener {
Button button1, button2, button3;
@Override
public void onCreate(Bundle bundle) {
super.onCreate();
...
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
// do stuff;
break;
case R.id.button2:
// do stuff;
break;
...
}
}
Upvotes: 0
Reputation: 878
As from: Android OnClickListener - identify a button
You will learn the way to do it, in an easy way, is:
public class Mtest extends Activity {
Button b1;
Button b2;
public void onCreate(Bundle savedInstanceState) {
...
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(myhandler1);
b2.setOnClickListener(myhandler2);
...
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
}
}
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 2nd button
}
}
}
Or, if you are working with just one clicklistener, you can do:
View.OnClickListener myOnlyhandler = new View.OnClickListener() {
public void onClick(View v) {
if( b1.getId() == ((Button)v).getId() ){
// it was the first button
}
else if( b2.getId() == ((Button)v).getId() ){
// it was the second button
}
}
}
Though, I don't recommend doing it that way since you will have to add an if
for each button you use. That's hard to maintain.
Upvotes: 0
Reputation: 5654
Since the view
that you get as an argument to onClick represents the view that was clicked, you can just call setEnabled(false)
to disable the button.
public void giveClue(View view) {
((Button)view).setEnabled(false); // disable button
}
Upvotes: 1
Reputation: 15973
Like that..
public void giveClue(View view) {
if( b1.getId() == view.getId() ){
// it was the first button
}
else if( b2.getId() == view.getId() ){
// it was the second button
}
}
Upvotes: 0
Reputation: 7013
in XML there is a property of Tag
android:tag=""
you can aad tag to all three buttons and with that tags you can handle them accordingly
Upvotes: 0
Reputation: 4708
Check for view.getId()
in the giveClue
method and compare it to R.id.imageButton1 etc. Normally done with a switch-case :)
Upvotes: 3