Vincent Mon
Vincent Mon

Reputation: 13

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!

Upvotes: 1

Views: 4142

Answers (3)

xxxzhi
xxxzhi

Reputation: 481

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

you can learn from this for drawable resource.

Upvotes: 0

Parth Bhayani
Parth Bhayani

Reputation: 1924

You can set the background like this,

Button btn = (Button) findViewById(R.id.btn);
btn.setBackgroundResource(R.drawable.ic_launcher);

Upvotes: 1

Orkun Kocyigit
Orkun Kocyigit

Reputation: 1147

Layouts are not background images. They contain the information which is necessary to draw application views/screens. To draw a button with custom background you can use drawable resource file.

http://developer.android.com/guide/topics/resources/drawable-resource.html

Upvotes: 0

Related Questions