the_big_blackbox the_big_blackbox - 6 months ago 57
Java Question

get xml attribute of button android java

Please help I have the a number of buttons with text and images in a layout in my activity_main.xml file.

<Button
android:drawableTop="@drawable/ring"
android:id="@+id/Button1"
android:text="pic1" />


Each of the other buttons have a different @drawable resource, id and text.

Question: How would I programmatically loop through each of the buttons in MainActivity.java and obtain the value of the @drawableTop attribute, so in the above case - ring

Answer

From documentation:

Drawable[] getCompoundDrawables ()

Returns drawables for the left, top, right, and bottom borders.

So

button.getCompoundDrawables()[1];

will return drawable for top.