Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inflating drawable from XML

I'm trying to do a bottom-level menu like this:

enter image description here

I've defined the layout this way:

<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" >

  <HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:scrollbars="none">

    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="horizontal">

      <!-- First item of the menu -->
      <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" />

      <!-- Second item of the menu -->
      <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" />

        <!-- ... Some additional items in the menu -->
    </LinearLayout>
  </HorizontalScrollView>
</LinearLayout>

The @drawable/menu_bottom is a layer-list, defined this way:

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

<item>    
  <shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
      android:width="1px"
      android:color="#55ffffff" />

    <padding
      android:left="16px"
      android:top="6px"
      android:right="16px"
      android:bottom="6px" />

    <solid
      android:color="#cc000000" />

    <gradient
      android:startColor="#222222"
      android:centerColor="#111111"
      android:endColor="#000000"
      android:angle="-90" />

    <corners
      android:bottomRightRadius="10dp"
      android:bottomLeftRadius="10dp"
      android:topLeftRadius="10dp"
      android:topRightRadius="10dp" />
  </shape>
</item>

<item>
  <bitmap 
    android:src="@+drawable/bitmap_to_change"
    android:tileMode="disabled"
    android:width="30dp"
    android:height="15dp"
    android:gravity="center" />    
</item>
</layer-list>

Now the thing is: I want to use exactly the same background for each button in the menu, in exception of the src field of the bitmap. I've tried to use LayoutInflater on one of the buttons and tried to change it programatically, like this:

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));

But this returns an exception:

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

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?

Thanks!

----------- EDIT -----------

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:

I assigned an id to the corresponding "item" in the XML file:

<item android:id="@+id:bitmap_layer">

And programatically:

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));
like image 505
nKn Avatar asked Apr 15 '26 01:04

nKn


1 Answers

You don´t need to use the iflater, getting the Drawable resurse is enough:

android.graphics.drawable.Drawable shape = getResources().getDrawable(R.drawable.item_panel_borde);

with your workaroud you can get trouble if you use this drawable resource in other views, you can create your LayerList programatically using the android.graphics.drawable.LayerDrawable(Drawable[]) constructor.

like image 64
zeusboy Avatar answered Apr 16 '26 16:04

zeusboy