Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6559467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:19:51+00:00 2026-05-25T13:19:51+00:00

I am trying to code the following xml by code. —-drawable\my_layerdrawable.xml <?xml version=1.0 encoding=utf-8?>

  • 0

I am trying to code the following xml by code.

—-drawable\my_layerdrawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
      <bitmap android:src="@drawable/my_image"
        android:gravity="left"/>
    </item>
    <item android:left="10dp">
      <bitmap android:src="@drawable/my_image"
        android:gravity="left" />
    </item>
    <item android:top="10dp">
      <bitmap android:src="@drawable/my_image"
        android:gravity="left"/>
    </item>
    <item android:top="10dp" android:left="10dp">
      <bitmap android:src="@drawable/my_image"
        android:gravity="left" />
    </item>
    <item android:top="20dp">
      <bitmap android:src="@drawable/my_image"
        android:gravity="left"/>
    </item>
    <item android:top="20dp" android:left="10dp">
      <bitmap android:src="@drawable/my_image"
        android:gravity="left" />
    </item>
</layer-list>

I coded the following block but it is stretching the images.

    InsetDrawable[] layers = new InsetDrawable[this.itemCount];

    Resources resources = getResources();

    ImageButton imgButton = (ImageButton) findViewById(R.id.btnItems);
    int layerTop = 0;
    for (int i = 0; i < this.itemCount; i++)
    {

        int layerLeft = i % 2 == 1 ? 5 : 0;

        Drawable dr = resources.getDrawable(R.drawable.my_image);
        layers[i] = new InsetDrawable(dr, layerLeft, layerTop, -layerLeft, -layerTop);

        layerTop += i % 2 == 1 ? 10 : 0;


    }

    LayerDrawable layerDrawable = new LayerDrawable(layers);

    imgButton.setImageDrawable(layerDrawable);

When I assign the drawable xml to the imgButton It is working correctly no stretch or any change.

ImageButton imgButton = (ImageButton) findViewById(R.id.btnItems);
imgButton.setImageResource(R.drawable.my_layerdrawable);

Do you have any idea to make the layer drawable by code?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:19:51+00:00Added an answer on May 25, 2026 at 1:19 pm

    You could use BitmapDrawable.

    A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You can create a BitmapDrawable from a file path, an input stream, through XML inflation, or from a Bitmap object.

    Your code would then look like this:

    Drawable[] layers = new Drawable[this.itemCount];
    Resources resources = getResources();
    ImageButton imgButton = (ImageButton) findViewById(R.id.btnItems);
    
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
    for (int i = 0; i < this.itemCount; i++)
    {
        Bitmap bm = BitmapFactory.decodeResource(resources, R.drawable.icon);
        BitmapDrawable bmd = new BitmapDrawable(bm);
        bmd.setGravity(Gravity.TOP);        
        bmd.setTargetDensity(metrics);
        layers[i] = bmd;   
    }
    LayerDrawable layerDrawable = new LayerDrawable(layers);
    
    int layerTop = 0;
    for (int i = 0; i < this.itemCount; i++)
    {
    
        int layerLeft = i % 2 == 1 ? 5 : 0;
        layerDrawable.setLayerInset(i, layerLeft, layerTop, -layerLeft, -layerTop);
        layerTop += i % 2 == 1 ? 10 : 0;
    }
    
    imgButton.setImageDrawable(layerDrawable);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML: <?xml version=1.0 encoding=UTF-8?> <gnm:Workbook xmlns:gnm=http://www.gnumeric.org/v10.dtd xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://www.gnumeric.org/v9.xsd> <office:document-meta xmlns:office=urn:oasis:names:tc:opendocument:xmlns:office:1.0
I am trying to parse the following XML: <?xml version=1.0 encoding=UTF-8?> <!-- _tag -->
I'm trying to use the following code # LOAD XML FILE $XML = new
I'm trying the following code http://code.google.com/apis/ajax/playground/#change_the_playing_video It works well when runned from the playground
I have the following XML code that I'm trying to parse, but I'm sure
I can use the following XML just fine <shape android:shape=rectangle xmlns...> <gradient android:startColor=#255779 android:centerColor=#3e7492
I am trying to write the following into an XML document: <html xmlns:o=urn:schemas-microsoft-com:office:office xmlns:x=urn:schemas-microsoft-com:office:excel
I'm trying to validade a XML against a W3C XML Schema. The following code
i have the following xml <?xml version=1.0 encoding=ISO-8859-1 ?> <bookstore> <book category=cooking> <title lang=en>Everyday
I am trying to deserialize the following xml structure into an object... <?xml version=1.0

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.