Joey
Joey

Reputation: 27

How to get a button Id in of a clicked button?

I have a couple of buttons I want to make a computation on, so I want to get the ID of the particular button clicked so I can perform an operation on.

I have configured the id for all my buttons in my XML file. I hope i can get help here.

 <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".MainActivity"
     android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="15dp"
            android:textSize="45dp"
            android:gravity="center_horizontal"
            android:textColor="@color/colorPrimaryDark"
             android:id="@+id/displayCalc"
            android:text="0"></TextView>

          <Button
             android:id="@+id/button_one"
             android:layout_width="70dp"
             android:layout_height="wrap_content"
             android:text="1"
             android:onClick="one"/>

         <Button
             android:id="@+id/button_two"
             android:layout_width="70dp"
             android:layout_height="wrap_content"
             android:text="2"
             android:onClick="two"/>

         <Button
             android:id="@+id/button_three"
             android:layout_width="70dp"
             android:layout_height="wrap_content"
             android:text="3"
             android:onClick="three"/>

         <Button
             android:id="@+id/button_add"
             android:layout_width="70dp"
             android:layout_height="wrap_content"
             android:text="+"
             android:textColor="@color/design_default_color_primary_dark"
             android:textSize="18sp"
             android:onClick="calcAdd"/>

     </LinearLayout>

Upvotes: 0

Views: 7478

Answers (3)

forpas
forpas

Reputation: 164064

You have defined in the XML file for the Button with id button_one with this attribute:

android:onClick="one"

that the listener will be a method named one, so all you have to do is create that method inside your activity like this:

public void one(View view) {
    <your code goes here>
}

If you want to refer to the Button inside that method you can do it like this:

Button button = (Button) view;

and if you want its id:

int id = button.getId();

So create 4 methods (like the one above) inside your Activity class for all 4 buttons: one(), two(), three() and calcAdd().

Upvotes: 1

Abhinav Chauhan
Abhinav Chauhan

Reputation: 1384

say you're doing something like that

Button buttonOne = findViewById(R.id.button_one); //now you have access to the button that is in your layout
buttonOne.setOnClickLister(new View.OnClickListener(){ // i hope you understand anonymous inner class
@Override 
public void onClick(View v){ //now when your button is clicked this method is called 
//and you  passed v in argument which is nothing but your button
//you're passed a View object and not a out of the box button because the 
//View.OnClickListener can be implemented on any view if you want to call button 
//specific method on it you cast it to a button , because you know it is a button 
//inside
v.getId() //gives you the id of your button which you wanted
}
}

Tell me there is still some confusion .

Upvotes: 1

Arrowsome
Arrowsome

Reputation: 2859

Your question is a little strange. If I got it correctly you want to know which button is clicked in Java/Kotlin and perform some operation based on buuton id. If that is what you want:

  1. Implement View.OnClickListener in your activity or fragment related to this xml.
  2. Implement onCLick(View) method.
  3. Now you can use when (kotlin) or switch (java) to find the clicked button id.
class MainActivity : AppCompatActivity(), View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button_one.setOnClickListener(this)
        button_two.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.button_one -> {
                //TODO (do something for button with id button_one)
            }
            R.id.button_two -> {
                //TODO (do something for button with id button_two)
            }
        }
    }
}

Upvotes: 5

Related Questions