Search code examples
androidlayoutsetbackground

Android: setbackground layout to button


I want to do the following:

button.setBackgroundResource(R.layout.caracbout);

But this function only works like this:

button.setBackgroundResource(R.drawable.xxxx);

my button is

<Button
            android:layout_width="0dp" 
            android:layout_height="match_parent" 
            android:layout_weight=".6"
            android:background="@layout/boutcarac"
            android:textColor="#000000"
            android:text="Refresh"
            android:clickable="true"
            android:onClick="refreshfiche"/>

and my layout caracbout xml is like

<item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="5dip"/>
            <stroke
                android:width="1dip"
                android:color="#000000" />
            <gradient
                android:angle="-90"
                android:startColor="#0000"
                android:endColor="#fff5d6"  />
        </shape>
    </item>

    <item style="@style/typocarac">
        <shape android:shape="rectangle"  >
            <corners android:radius="5dip"/>
            <stroke
                android:width="1dip"
                android:color="#fff5d6" />
            <gradient
                android:angle="-90"
                android:startColor="#fff5d6"
                android:endColor="#0000"  />
        </shape>
    </item>

and I must change the item android:state_pressed="true", so I want to create a xml caracbout2 and set it in the java. But I dont know how can I do that. Thanks!


Solution

  • create your caracbout2 xml file in directory drawable and use R.drawable.caracbout2 in java

    you can learn from this for drawable resource.