Brownman Revival
Brownman Revival

Reputation: 3850

Get button ID not entire button

ArrayList<View> allButtons;
String button;
button = ((RelativeLayout) findViewById(R.id.activity_main)).getTouchables().toString();

System.out.println(button);

Outputs:

System.out: [android.support.v7.widget.AppCompatImageButton{7107785 VFED..C.. ........ 32,74-176,210 #7f0b0056 app:id/imageButton2}, android.support.v7.widget.AppCompatButton{c4b99da VFED..C.. ...P.... 66,256-242,352 #7f0b0057 app:id/button}]

How can i get just the id which is button

Upvotes: 1

Views: 1214

Answers (3)

Anuj.T
Anuj.T

Reputation: 1628

So your want to check which button is clicked in the layout and process accordingly.

This you can achieve by two way.

1. With getId()

xml

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next Layout"
    android:id="@+id/Next"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

YourActivity

  final Button nxtbtn = (Button) findViewById(R.id.Next);



    nxtbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int ID = nxtbtn.getId();

            if(ID == R.id.Next) // your R file will have all your id's in the literal form.
            {
                Log.e(TAG,""+ID);
            }
        }
    });

2. With TAG

xml

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next Layout"
    android:tag="buttonA"
    android:id="@+id/Next"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

Just add a TAG attribute in your xml

Your Activity

  final Button nxtbtn = (Button) findViewById(R.id.Next);



    nxtbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           String Tag = nxtbtn.getTag().toString();
            Log.e("TAG", "" + Tag);
          if (Tag.equal("ButtonA")){ /* Your code here */}
        }
    });

Hope this might help.

Upvotes: 0

Pankaj Kumar
Pankaj Kumar

Reputation: 82958

You should write your code a s

ArrayList<View> allTouchables = ((RelativeLayout) findViewById(R.id.activity_main)).getTouchables();
for (View view : allTouchables) {
    System.out.println(view.getId());
}

As above code returns all touchable views present into given container, you should also check about the type of view like

ArrayList<View> allTouchables = ((RelativeLayout) findViewById(R.id.activity_main)).getTouchables();
for (View touchable : allTouchables) {
   // To check if touchable view is button or not
    if( touchable instanceof Button) {
        System.out.println(touchable.getId());
    }
}

To get string representation of id, you can write

String idString = view.getResources().getResourceEntryName(view.getId());

Upvotes: 4

ujjwal mainali
ujjwal mainali

Reputation: 389

aah you can check the type of the views before adding to the arraylist.

ArrayList<Button>buttons = new ArrayList<>();
parentLayout = (RelativeLayout)findViewById(parentID);

for(int i=0; i<parentLayout.getChildCount(); i++){
    View view = parentLayout.getChildAt(i);

   if(view instanceof Button){
       buttons.add((Button)view);
    } 
}

Upvotes: 2

Related Questions