nilskober
nilskober

Reputation: 188

Android: set drawable over button

in my app are 3 Buttons (screenshot at the bottom). Now I want to set an AnimatedDrawable (a finger) over the first Button (image at the bottom). I'm using a LinearLayout and the background of the Button is just a color (no drawable). I tried this:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
   <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/black"/>
        </shape>
    </item>
    <item android:drawable="@drawable/finger"></item>
</layer-list>

and set the layer-list as background of the Button but then the finger is as big as the Button. I also tried to use an RelativeLayout inside my LinearLayout but then I cant set the weight of the Buttons. I can't use an ImageButton because of the Text on the Button. So is there any possibility to manage that problem ?

Greetings Nils

Like it is now (without the finger

Like I'd like it

Upvotes: 2

Views: 1267

Answers (2)

sherin
sherin

Reputation: 1091

Try this code:

<Button
    android:id="@+id/Submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="23dp"
    android:drawableRight="@android:drawable/ic_menu_directions"
    android:text="Button" />

Instead of this "@android:drawable/ic_menu_directions" you can choose any image from drawable

Upvotes: 2

Shailendra Madda
Shailendra Madda

Reputation: 21551

You can set image on button programatically by:

final Button btn = (Button) findViewById(R.id.your_Button);


btn.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        btn.setBackgroundResource(R.drawable.icon);
    }
});

You can use the "Image Button" it's better for you, and the code like this :

final ImageButton next = (ImageButton) findViewById(R.id.your_Button) ;
Drawable d = Drawable.createFromPath("@drawable/your_image");
next.enter.setImageDrawable(d);

Upvotes: 0

Related Questions