C Sharper
C Sharper

Reputation: 8646

Apply image to button background

I wanted to apply image to background of button.

I written style for it as follows:

 <style name="SingnupButton" parent="@android:style/TextAppearance.Medium">
    <item android:drawable="@drawable/signupButton.jpeg" /> 
    </style>

Image is in my drawable-hdpi folder.

I am applying it as follows:

<Button
            style="@style/SingnupButton"
            android:id="@+id/btn_register"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="@string/btn_register" />

But this image is not getting applied to background of button.

What is wrong with my code?

Please guid me.

Upvotes: 0

Views: 404

Answers (5)

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Try this:

<style name="SingnupButton" parent="@android:style/TextAppearance.Medium">
    <item android:drawable="@drawable/signupButton" /> 
</style>

Upvotes: 1

Piyush
Piyush

Reputation: 18923

change it with

<item android:drawable="@drawable/signupButton.jpeg" /> 

to

<item android:background="@drawable/signupButton" /> 

just rename that image name and remove .jpeg extension

Upvotes: 1

Renjith
Renjith

Reputation: 5803

You are setting the background wrongly in style.

Try this way..

<style name="SingnupButton" parent="@android:style/TextAppearance.Medium">
    <item name="android:background">@drawable/signupButton</item>
</style>

Upvotes: 1

Kalai.G
Kalai.G

Reputation: 1610

Try to set background for the button and put your selector to drawable folder. Dont use(android:drawable="@drawable/button_bg_shape.jpg) any extensions like jpeg,png,gif,...while invoking.Try using png files in drawable folder

XML file:

       <Button
            android:id="@+id/Bundle_clear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/selector_button"
            android:text="Clear"
            android:textColor="#FFF"
            android:textSize="@dimen/text_size"
            android:textStyle="bold" />

Create selector_button in drawable folder

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

    <item android:drawable="@drawable/button_bg_shape" android:state_pressed="true"/>
    <item android:drawable="@drawable/bt"/>

     </selector>

Upvotes: 1

pumpkee
pumpkee

Reputation: 3357

Remove the .jpg in

 <item android:drawable="@drawable/signupButton.jpeg" /> 

Upvotes: 1

Related Questions