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 anegative padding
andSetPadding
with apositive padding
to counter creates acentered 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");
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.Mode
s, 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
How to Mock Void Methods With Mockito
When Exactly Is It Leak Safe to Use (Anonymous) Inner Classes
Any Good Orm Tools for Android Development
How to Add Parameters to a Http Get Request in Android
Android Load from Url to Bitmap
Cannot Load R Xlsx Package on MAC Os 10.11
Linux Launch Java Program on Startup (Ec2 Instance)
How to Solve the 'Lock Obtain Timed Out' When Using Solr Plainly
Animation on Transparent-Background Jframe Linux
Random Noclassdeffound Error in Web Application
Failed to Load the Jni Shared Library (Jdk)
How to Check If an App Running on Android
Service Discovery Failed Exception Using Bluetooth on Android
Listview Onitemclicklistener Not Responding
Rjava Install Error "Java_Home Cannot Be Determined from the Registry"