Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameter from a button to android:onClick method

Hi I have something like this (3 buttons) in my activity xml pointing to same method:

 <Button
        android:id="@+id/Button_1"
        android:onClick="printNo"
        android:text="@string/Button_1" />
 <Button
        android:id="@+id/Button_2"
        android:onClick="printNo"
        android:text="@string/Button_2" />

 <Button
        android:id="@+id/Button_3"
        android:onClick="printNo"
        android:text="@string/Button_3" />

Is there any way I could determine which button was pressed while in the printNo method ?

like image 316
Murphy316 Avatar asked Oct 04 '12 12:10

Murphy316


1 Answers

public void printNo( View v ) {
    switch (v.getId()) {
    case (R.id.Button_1):
        //stuff
    break;
    case (R.id.Button_2):
        //stuff
    break;
    case (R.id.Button_3):
        //stuff
    break;
}
like image 198
VicVu Avatar answered Sep 23 '22 09:09

VicVu