How to Programmatically Set Drawableleft on Android Button

How to programmatically set drawableLeft on Android button?

You can use the setCompoundDrawables method to do this. See the example here. I used this without using the setBounds and it worked. You can try either way.

UPDATE: Copying the code here incase the link goes down

Drawable img = getContext().getResources().getDrawable(R.drawable.smiley);
img.setBounds(0, 0, 60, 60);
txtVw.setCompoundDrawables(img, null, null, null);

or

Drawable img = getContext().getResources().getDrawable(R.drawable.smiley);
txtVw.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);

or

txtVw.setCompoundDrawablesWithIntrinsicBounds(R.drawable.smiley, 0, 0, 0);

Change Button Drawable Programmatically

try something like this: for drawableLeft

int imgResource = R.drawable.your_drawable;
setFeeling.setCompoundDrawablesWithIntrinsicBounds(imgResource, 0, 0, 0);
setFeeling.setCompoundDrawablePadding(8); //for padding

Thanks to the Answer Here also.

Programmatically set left drawable in a TextView

You can use setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)

set 0 where you don't want images

Example for Drawable on the left:

TextView textView = (TextView) findViewById(R.id.myTxtView);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);

Alternatively, you can use setCompoundDrawablesRelativeWithIntrinsicBounds to respect RTL/LTR layouts.


Tip: Whenever you know any XML attribute but don't have clue about how to use it at runtime. just go to the description of that property in developer doc. There you will find Related Methods if it's supported at runtime . i.e. For DrawableLeft

Is it possible to set drawableLeft programmatically?

Yes, use setCompoundDrawablesWithIntrinsicBounds

and define the drawable for the first parameter, then 0 for all the others.

The code should look something like this:

Button b = findViewById(R.id.myButton);

b.setCompoundDrawablesWithIntrinsicBounds(R.drawable.myDrawable, 0, 0, 0);

If your drawable was created in code as well then you need to use the other setCompoundDrawablesWithIntrinsicBounds method which takes 4 drawables, and pass null for all but the left.

Programmatically change drawableLeft of Button

Try this:

int imgResource = R.drawable.left_img;
button.setCompoundDrawablesWithIntrinsicBounds(imgResource, 0, 0, 0);

Reference

DrawableLeft programmatically on Xamarin.Android?

For drawableLeft

 var draw = ContextCompat.Drawable(this, Resource.Drawable.your_drawable)
button.SetCompoundDrawablesWithIntrinsicBounds(draw, null, null, null);

Note : this is context of the Activity.

For drawablePadding use CompoundDrawablePadding.

For padding use SetPadding.

Important Note :

CompoundDrawablePadding with a negative padding and SetPadding
with a positive padding to counter creates a centered effect.

just check it out and tell me.

How to add an Icon to a Button in Android from Java?

You can use the MaterialButton:

MaterialButton button = new MaterialButton(this);
button.setIcon(ContextCompat.getDrawable(this,R.drawable.ic_add_24px));
button.setText("Button");

Sample Image

How to change drawable tint of a button below api level 23 programmatically in android

you are using PorterDuff.Mode.MULTIPLY, so you are multiplying colors. assuming (name of your drawable) your icon is black - #000000 or as int it will be 0. then 0 * GRAY (or any other color) will always give you 0, so still black...

try other PorterDuff.Modes, e.g. PorterDuff.Mode.SRC_ATOP or PorterDuff.Mode.SRC_IN

your current code will probably work with white version of icon, which should be colored properly with MULTIPLY

How to programmatically set drawableRight on Android Edittext?

You can use the function below:

editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.drawableRight, 0);

or (if you want to pass the drawable itself instead of its ID)

editText.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(context,R.drawable.drawableRight), null)

The order of params corresponding to the drawable location is: left, top, right, bottom



Related Topics



Leave a reply



Submit