6

I'm creating an ImageButton like so:

          <ImageButton
                android:id="@+id/one"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="0.333"
                android:adjustViewBounds="true"
                android:background="@null"
                android:contentDescription="@string/description_image_button_one"
                android:scaleType="fitEnd"
                android:src="@drawable/dialpad_1" />

I would like to implement a way to have the button flash or change color when clicked/pressed just to identify that it as been clicked. I know I can reference the background as a drawable with a selector of state_pressed to a certain color. I'm trying to avoid making a separate xml file for each button in the drawable. What's the best way of doing this without creating all those extra xml files?

ono
  • 2,984
  • 9
  • 43
  • 85

3 Answers3

10

You need to create custom drawable selectors for your button background.

This file will live in your XML folder, and look something like this (each element describes the button background when in different selected states):

File would be named: res/drawable/my_custom_selector.xml

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/blue_button_on"
        android:state_focused="true"
        android:state_pressed="true"/>
    <item
        android:drawable="@drawable/blue_button_on"
        android:state_focused="false"
        android:state_pressed="true"/>
    <item
        android:drawable="@drawable/blue_button_off"
        android:state_focused="true"
        android:state_pressed="false"/>
    <item
        android:drawable="@drawable/blue_button_off"
        android:state_focused="false"
        android:state_pressed="false"/>
</selector>

Then to apply that background to your ImageView (or any View), just set it as the background:

 <ImageButton
                android:id="@+id/one"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="0.333"
                android:adjustViewBounds="true"
                android:background="@drawable/my_custom_selector"
                android:contentDescription="@string/description_image_button_one"
                android:scaleType="fitEnd"
                android:src="@drawable/dialpad_1" />
Booger
  • 18,579
  • 7
  • 55
  • 72
  • Got it. So if I have 12 buttons I need to create 12 xml files? – ono Jul 30 '13 at 13:17
  • No, the same background can be used in all your buttons. ie. just apply the same background to each of your buttons (and they will all be the same look when pressed). button1.setBackground(my_drawable); button2.setBackground(my_drawable); etc – Booger Jul 30 '13 at 13:21
1

The XML files you are trying to avoid is the easy way to achieve what you want. The other way would be the do it using code in the onClickListener, which in my opinion is a task that needs much more work.

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
-1

You should XML files or else don't do anything.Android provides default color for the button pressed.

user
  • 1,979
  • 4
  • 18
  • 18