Suppose there are 2 ImageButtons.Now, to display the image based on its state, I should write a selector xml for each of the ImageButton. Can we write a same selector for both the views.Because my app have a lot of buttons and have to write a selector xml for each of the view. Can we optimize it?
2 Answers
if you you try it with xml file then you must create each selector for each button.
So try to do dynamically either by subclassing the StateListDrawable or try the below code which creates selector dynamically:::
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},
getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },
getResources().getDrawable(R.drawable.normal));
imageView.setImageDrawable(states);

- 34,573
- 7
- 66
- 64
-
Works great; Also as in the XML, states can be combined and negated (with a "-"). so pressed=true checked=false translates to: states.addState(new int[]{ android.R.attr.state_pressed, -android.R.attr.state_checked }); See: http://stackoverflow.com/questions/5092649 – Stan Kurdziel Oct 17 '12 at 18:05
-
great..! is there a list of states which are available for andriod.R.attr.* ? – Lalith B Aug 19 '13 at 16:21
You don't have to create a xml selector for each of your buttons, just re-use the one that fits. I use the following code in my project for many buttons with the selector and other attributes that are the same.
<LinearLayout android:id="@+id/group_news" style="@style/main_button_group" >
<ImageView android:id="@+id/image_news" style="@style/main_button_image" android:src="@drawable/news" />
<TextView android:id="@+id/text_news" style="@style/main_button_text" android:text="@string/button_news" />
</LinearLayout>
(Note: I use a LinearLayout with an ImageView and a TextView and not a TextView with the drawableLeft attribute, because I'm replacing the Images in code. Changing the drawableLeft was not working properly)
And then create an styles.xml in the res\values folder with all attribures that are the same for those buttons like:
<resources>
<style name="main_button_group">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:orientation">horizontal</item>
<item name="android:layout_marginTop">2dp</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:paddingLeft">25dp</item>
<item name="android:background">@drawable/main_button_states</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
</style>
<style name="main_button_image">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center_vertical</item>
<item name="android:contentDescription">@string/empty</item>
</style>
<style name="main_button_text">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">10dp</item>
<item name="android:layout_gravity">center_vertical</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:textSize">20dp</item>
<item name="android:textColor">@color/general_value</item>
</style>
</resources>
The selector @drawable/main_button_states looks like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/main_button_pressed" />
<item android:state_focused="true" android:drawable="@drawable/main_button_focused" />
<item android:state_hovered="true" android:drawable="@drawable/main_button_focused" />
<item android:drawable="@drawable/main_button_normal" />
</selector>
This works perfectly.

- 843
- 2
- 11
- 22
-
But we need to create all the selectors separately right.What I am asking is there any way to write all the selector codes in a single file or any way to reuse them. – Dileep Perla Apr 15 '12 at 17:03
-
1@DileepPerla, each selector is in it's separate file. You identify them with the filename (_@drawable/main_button_states_ is the file _main_button_states.xml_ in folder _drawable_) – infero Apr 15 '12 at 17:41
-
I got it. Calling the selector xml is not a problem for me. I just wanted to optimize the number of selector xml files. – Dileep Perla Apr 15 '12 at 18:13