Note that there are some explanatory texts on larger screens.

plurals
  1. PO"How to pass a parameter into a drawable" or "Create button with gradient and changeable image"
    text
    copied!<p>I have a custom drawable in my android project that should be the background of a button. But each button should also support an icon AND a text.</p> <p>What I tried so far is the following:</p> <p>I have a drawable called "btn_background.xml"</p> <pre><code>&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item&gt; &lt;layer-list &gt; &lt;item&gt; &lt;shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"&gt; &lt;gradient android:startColor="#ff0000" android:endColor="#ee1111" android:type="linear" android:angle="90"/&gt; &lt;stroke android:color="#ff0000" android:width="1dip"/&gt; &lt;size android:width="80dip" android:height="40dip" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;item android:drawable="@drawable/btn_search"/&gt; &lt;/layer-list&gt; &lt;/item&gt; </code></pre> <p></p> <p>And in my layout file i use that like this:</p> <pre><code>&lt;Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:background="@drawable/btn_background" android:textColor="#ffffff"/&gt; </code></pre> <p>This is getting me somewhere close... but there are two problems: * The image is stretched across the whole button * I can't just change the image in the layout file... so i would have to make a seperate btn_whatever.xml file for each icon?!?</p> <p>The other solution that I tried was to use an ImageButton instead of an Button and set the background to my btn_background.xml .... this would make me able to select the image in the layout file... but then i can't give it a text....</p> <p>I would be happy to hear about your solutions for my problem :)</p> <p>Thanks a lot!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload