I have 6 image buttons in my activity each having different images..
For those 6 image buttons I am having 6 xml of button states in drawable folder for each button.
Is there any way or method so that I can club all the 6 xml into 1 single xml?
I have 6 image buttons in my activity each having different images..
For those 6 image buttons I am having 6 xml of button states in drawable folder for each button.
Is there any way or method so that I can club all the 6 xml into 1 single xml?
Yes, you can. You need not have 6 xmls for 6 different images, and then one more xml for combining the 6 XML files.
You can just have one XML, that contains all the state images.
Check this sample.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/btn_default_normal" /> <item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/btn_default_normal_disable" /> <item android:state_pressed="true" android:drawable="@drawable/btn_default_pressed" /> <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/btn_default_selected" /> <item android:state_enabled="true" android:drawable="@drawable/btn_default_normal" /> <item android:state_focused="true" android:drawable="@drawable/btn_default_normal_disable_focused" /> <item android:drawable="@drawable/btn_default_normal_disable" /> </selector>
As you can see, in this single xml, you can refer to the images(drawables), directly.
android:drawable="@drawable/btn_default_selected"
please read: http://developer.android.com/training/improving-layouts/reusing-layouts.html
As to findViewById(), you will have sub-views with identical ids; to find these views, you will have to find the root view and use that view to find-by-id its child view. (You can e.g. use a LinearLayout as a button and this is how you can have multiple such buttons.)
By the way, if the xml is a drawable, you can just reference that drawable from all buttons.
UPDATE (yes I do know the above does not answer the question after the question is edited):
I used the same background drawable (a selector) for several buttons. The foreground was text (in fact, a LinearLayout). I'd suggest you try to reuse the same background drawable.
Please note that you can have images even on a text button, there are setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) and setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom).