Note that there are some explanatory texts on larger screens.

plurals
  1. POInflating drawable from XML
    text
    copied!<p>I'm trying to do a bottom-level menu like this:</p> <p><img src="https://i.stack.imgur.com/YIUyn.jpg" alt="enter image description here"></p> <p>I've defined the layout this way:</p> <pre><code>&lt;LinearLayout android:id="@+id/bottomBar" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginTop="4dp" android:orientation="horizontal" android:layout_alignParentBottom="true" &gt; &lt;HorizontalScrollView android:layout_width="wrap_content" android:layout_height="wrap_content" android:fillViewport="true" android:scrollbars="none"&gt; &lt;LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"&gt; &lt;!-- First item of the menu --&gt; &lt;ImageButton android:id="@+id/BConex1" android:contentDescription="@string/desc_gen_image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.2" android:layout_gravity="left" android:background="@drawable/menu_bottom" android:layout_marginLeft="4dp" /&gt; &lt;!-- Second item of the menu --&gt; &lt;ImageButton android:id="@+id/BConex2" android:contentDescription="@string/desc_gen_image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.2" android:layout_gravity="left" android:background="@drawable/menu_bottom" android:layout_marginLeft="4dp" /&gt; &lt;!-- ... Some additional items in the menu --&gt; &lt;/LinearLayout&gt; &lt;/HorizontalScrollView&gt; &lt;/LinearLayout&gt; </code></pre> <p>The @drawable/menu_bottom is a layer-list, defined this way:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android" &gt; &lt;item&gt; &lt;shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"&gt; &lt;stroke android:width="1px" android:color="#55ffffff" /&gt; &lt;padding android:left="16px" android:top="6px" android:right="16px" android:bottom="6px" /&gt; &lt;solid android:color="#cc000000" /&gt; &lt;gradient android:startColor="#222222" android:centerColor="#111111" android:endColor="#000000" android:angle="-90" /&gt; &lt;corners android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;item&gt; &lt;bitmap android:src="@+drawable/bitmap_to_change" android:tileMode="disabled" android:width="30dp" android:height="15dp" android:gravity="center" /&gt; &lt;/item&gt; &lt;/layer-list&gt; </code></pre> <p>Now the thing is: I want to use exactly the same background for each button in the menu, in exception of the <em>src</em> field of the bitmap. I've tried to use LayoutInflater on one of the buttons and tried to change it programatically, like this:</p> <pre><code>View first = LayoutInflater.from(this).inflate(R.drawable.menu_bottom, null); first.findViewById(R.drawable.bitmap_to_change).setBackground(this.getResources().getDrawable(R.drawable.another_resource)); </code></pre> <p>But this returns an exception:</p> <blockquote> <p>12-11 11:35:53.556: E/AndroidRuntime(897): java.lang.RuntimeException: Unable to start activity ComponentInfo{XXX.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class layer-list</p> </blockquote> <p>So I assume that's not the correct way. Is there a way to accomplish this instead of defining one XML file per menu item?</p> <p>Thanks!</p> <p><strong>----------- EDIT -----------</strong></p> <p>I've found a "dirty" workaround to achieve my goal: getting the background of one of the buttons as a LayerDrawable and substituting the Bitmap with the correct. This is not the way I want to do it (as I want it entirely programatically) nor it's the original question, so I'm not going to answer myself letting the question opened in the hope someone can help, but here's what I'm doing now:</p> <p>I assigned an id to the corresponding "item" in the XML file:</p> <pre><code>&lt;item android:id="@+id:bitmap_layer"&gt; </code></pre> <p>And programatically:</p> <pre><code>ImageButton firstButton = (ImageButton) findViewById(R.id.BConex1); LayerDrawable bg = (LayerDrawable) firstButton.getBackground(); bg.setDrawableByLayerId(R.id.bitmap_layer, getResources().getDrawable(R.drawable.the_new_drawable)); </code></pre>
 

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