2

I use a xml file to color my button. In the xml a have this code to define the color of my button when it's clicked.

    <?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" >
        <shape>
            <gradient
                  android:startColor="@color/mainRaddoppiaButtonBackgroundEnd"
                android:endColor="@color/mainRaddoppiaButtonBackgroundStart"

                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="@color/mainRaddoppiaButtonBackgroundStart" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item android:state_focused="true" >
           <shape>
            <gradient
                  android:startColor="@color/mainRaddoppiaButtonBackgroundEnd"
                android:endColor="@color/mainRaddoppiaButtonBackgroundStart"

                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="@color/mainRaddoppiaButtonBackgroundStart" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>        
        <shape>
            <gradient
                android:startColor="@color/mainRaddoppiaButtonBackgroundStart"
                android:endColor="@color/mainRaddoppiaButtonBackgroundEnd"
                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="@color/mainRaddoppiaButtonBackgroundStart" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

Is it possibile from java to keep my button pressed and color it with "state_pressed" values set in my xml file?

Someting like:

  public void onClick(View button) {
     button.seLayout(R.xml.xmlFileName.state_pressed)
}

Of course this code has no sense, i just made it up to let you understand what i want to do

MDP
  • 4,177
  • 21
  • 63
  • 119

3 Answers3

5

Try this.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_pressed"
      android:state_pressed="true"/>
<item android:drawable="@drawable/btn_normal" />
</selector>
Pankaj Singh
  • 2,241
  • 2
  • 16
  • 16
2

use StateListDrawable for setting selector by code as:

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));

 button.setBackgroundDrawable(states);//FOR BUTTON
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
  • mmm i did this: states.addState(new int[] {android.R.attr.state_pressed},getResources().getDrawable(R.xml.XML_FILE_NAME_WHERE_I_DEFINED_THE_STATE_PRESSED)); but when i click the button, the button disappear – MDP Jan 18 '13 at 12:08
  • ok, sorry, i thought it was able to keep the color set in my xml file! Thanl you – MDP Jan 18 '13 at 12:19
  • @MatteoDepasquali You need to add your selector file inside one of drawables. e.g **drawables/myselector.xml** --> `getResources().getDrawable(R.drawable.myselector))` – Vikalp Patel Jan 18 '13 at 12:21
  • I did it, but it still doesn't work. I miss something. I put the file with the code i wrote above (`......`) in drawable folder and then i write `states.addState(new int[] {android.R.attr.state_pressed},getResources().getDrawable(R.drawable.XML_FILE_NAME));` but the button still disappear when i click it. – MDP Jan 18 '13 at 12:32
  • @MatteoDepasquali Did you added your ` inside `?? – Vikalp Patel Jan 18 '13 at 12:35
  • These SOQ is just duplicate of your question. You may found time worth visiting it --http://stackoverflow.com/questions/4697528/replace-selector-images-programmatically – Vikalp Patel Jan 18 '13 at 12:46
0

Use this:

StateListDrawable states = new StateListDrawable(){
    @Override
    protected boolean onStateChange(int[] stateSet) {
        //
    }
};
itemView.setBackground(states);
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
babith
  • 1