I have a tab layout in which i am setting the text and vector image like this:
TextView tab2 = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tab2.setText("OFFER");
tab2.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_offer, 0, 0);
tabLayout.addTab(tabLayout.newTab().setCustomView(tab2));
How can i change the tint color for the drawable vector image?
Try like this:
Drawable drawables[] = textView.getCompoundDrawables();
drawables[0].setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
Drawable drawable = res.getDrawable(R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
This work but i would all dinamically.
Drawable drawable = res.getDrawable(R.drawable.path);
button.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
where path is a string that contains a name of image..
thanks to all!
If I got you right, you are trying to get drawable by the image name. right?
If so,
int imageResource = res.getIdentifier(path, "drawable", getContext().getPackageName());
Drawable drawable = res.getDrawable(imageResource);
then assign the button drawable...
The drawableLeft property of the Button class has to be changed to another image when clicked.
What is the equivalent java code for the following xml content.
<Button
android:drawableLeft="#drawable/play_button />
Use this
Drawable drawable = getResources().getDrawable(R.drawable.play_button);
button.setCompoundDrawables(drawable, null, null, null);
//button is your button.
Try this :
Bitmap b = BitmapFactory.decodeResource(context.getResources(), R.drawable.play_button);
Drawable d = new BitmapDrawable(context.getResources(),b);
yourButtonName.setCompoundDrawablesWithIntrinsicBounds(d, null , null , null );
I know about set drawableRight in XML. but i required to do it programmatically because it is change as per some condition.
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
Find Further here
EditText myEdit = (EditText) findViewById(R.id.myEdit);
myEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon, 0);
// where params are (left,top,right,bottom)
You can also set drawable padding programmatically:
myEdit.setCompoundDrawablePadding("Padding value");
Try like below:
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
EdtText.setCompoundDrawablesWithIntrinsicBounds( 0, 0, img, 0);
Edit :
int img = R.drawable.smiley;
EdtText.setCompoundDrawablesWithIntrinsicBounds( 0, 0, img, 0);
Try:
EditText editFirstname = (EditText) findViewById(R.id.edit_fname);
Drawable icUser = getResources().getDrawable(R.drawable.ic_user);
editFirstname.setCompoundDrawablesWithIntrinsicBounds(null, null, icUser, null);
Then you can add a touch listener to that specific drawable.
For changing left and right both at a time I use this single line.
download.setCompoundDrawablesWithIntrinsicBounds( R.drawable.ic_lock_open_white_24dp, 0, R.drawable.ic_lock_open_white_24dp, 0);
int img = R.drawable.smiley;
editText.setCompoundDrawables( null, null, img, null );
Explained here
setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
et_feedback.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
}
et_feedback.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.feedback_new, 0, 0);
et_feedback.setTextColor(Color.BLACK);
}
});
Hide Drawable using this
et_feedback.setCompoundDrawablesWithIntrinsicBounds(0,0, 0, 0);
If it requires android graphics drawable then this will work
Drawable dw = getApplicationContext().getResources().getDrawable(R.drawable.edit);
Button start = (Button)findViewById(R.id.buttonStart);
start.setCompoundDrawablesWithIntrinsicBounds(dw, null, null, null);
You can use your editText view (here it is txview) built in function setCompoundDrawablesWithIntrinsicBounds() to do what you are looking for
in my code I Used it like this .
txview.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.ic_arrow_drop_up,0);
txview.setCompoundDrawablesWithIntrinsicBounds(left,top,right,bottom);
I'm using a Button
<Button
android:id="#+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/trans"
android:drawableLeft="#drawable/left_img"
android:fontFamily="arial"
android:text="My Name is "
android:textSize="50sp" />
and changing its text color with :
zoom.setTextColor(Color.parseColor("voilet"));
but not able to understand how to change its image??
Try this:
int imgResource = R.drawable.left_img;
button.setCompoundDrawablesWithIntrinsicBounds(imgResource, 0, 0, 0);
Reference
The safest way to set the left drawable without changing the values of the other drawables (top, right, and bottom):
Drawable[] drawables = textViewExample.getCompoundDrawables();
textViewExample.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, drawables[1], drawables[2], drawables[3]);
To do this, you can use the
setCompoundDrawables(...);
method. Be aware that comes with TextView, not Button.
This is how to use it:
Drawable img = getContext().getResources().getDrawable( R.drawable.yourimage);
img.setBounds( 0, 0, 60, 60 ); // set the image size
txtVw.setCompoundDrawables( img, null, null, null );
Taken from: How to programmatically set drawableLeft on Android button?
I recomend that instead of using a button you use an Imageview and add an onclick listener to it. That way you can just do Imageview.setbitmap(bitmap) and create a bitmap from one of your drawables
just follow this code i hope it's really helpful for you..
boolean isIconChange;
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
isIconChange = !isIconChange;
if(isIconChange){
button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like, 0, 0, 0);
button.setTextColor(Color.BLACK);
} else {
button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.dislike, 0, 0, 0);
button.setTextColor(Color.RED);
}
}
});