Android: button with two images | alignment

I created this button programmatically, and I can install iconon this using the code below

enter image description here

Drawable icon = context.getResources().getDrawable(iconResourceId);
button.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);

Now, I want to have another image ( icon) in the upper right corner, see the image below:

enter image description here

I tried to add both images using the code below:

Drawable icon = context.getResources().getDrawable(iconResourceId);
Drawable icon2 = context.getResources().getDrawable(iconResourceId2);
button.setCompoundDrawablesWithIntrinsicBounds(icon, null, icon2, null);

and, I get below results:

enter image description here

Can someone tell me how I can align it in the upper right corner?

+4
source share
1 answer

Your code will look like where btn_backgroundis the background that you currently have

    Button button = new Button(this);
    Drawable icon = context.getResources().getDrawable(iconResourceId);
    button.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
    button.setText("Test");

    if (hasNewUpdates()) {
        button.setBackgroundResource(R.drawable.btn_background_with_icon);
    } else {
        button.setBackgroundResource(R.drawable.btn_background);
    }

And this is how it btn_background_with_iconwill look like

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

    <item android:drawable="@drawable/btn_background"/>
    <item android:gravity="right" android:bottom="72dp" android:drawable="@drawable/ico_info"/>
</layer-list>
0
source

All Articles