How can I set the drawableRight resource via code? - android

I am trying to set an image on the right side of my button after the button has been clicked. I want to do this via code.
I have seen how to change the background resource via code but I am not able to find any examples showing how to change the sides via code. Is it possible?

You need to use the
public void setCompoundDrawables (Drawable left, Drawable top, Drawable right,
Drawable bottom)
method with null for any that are not needed.

Usually you can change using this
Drawable draw = getResources().getDrawable(R.drawable.facebook);
myButton.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null);
Be aware you can miss the button text.

Related

Not able to set textview drawable dynamically

Im trying to create a textview dynamically . I want to set the drawable to textview`
TextView tab = new TextView(getContext());
tab.setText(title);
tab.setSingleLine();
tab.setGravity(Gravity.CENTER);
int padding = getResources().getDimensionPixelOffset(R.dimen.offset);
tab.setPadding(0,0,padding,0);
if(typeface!= null){
tab.setTypeface(typeface);
}
tab.setCompoundDrawables(getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp),null,null,null);
`
But I'm not able to achieve it. Can anyone of you help me out.
use setCompoundDrawablesWithIntrinsicBounds
Sets the Drawables (if any) to appear to the left of, above, to the
right of, and below the text. Use null if you do not want a Drawable
there. The Drawables' bounds will be set to their intrinsic bounds.
setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp),null,null,null);
OR
setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp),0,0,0);
try this use setCompoundDrawablesWithIntrinsicBounds
>
void setCompoundDrawablesWithIntrinsicBounds (Drawable left,
Drawable top,
Drawable right,
Drawable bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
Calling this method will overwrite any Drawables previously set using setCompoundDrawablesRelative(Drawable, Drawable, Drawable, Drawable) or related methods.
sample code
TextView textView = (TextView) findViewById(R.id.myTxtView);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);

setCompoundDrawables working only when drawable set to another imageview?

I am changing the color of a drawable and then setting it as drawable left of a textview, but i am observing a weird thing.
The drawable left is only working if i am setting the drawable to some other image view before setting it into the textview.
Drawable mDrawable = this.getResources().getDrawable(R.drawable.legendc);
mDrawable.setColorFilter(colorsActive[0], PorterDuff.Mode.SRC_IN);
mImageview.setImageDrawable(mDrawable);
mtextview.setCompoundDrawables(mDrawable, null, null, null);
If i remove mImageview.setImageDrawable(mDrawable);
then the setCompoundDrawables is not working and no drawable left is being applied.
Why is this happening??
The reason why setCompoundDrawables() alone do not work might be something related to image rendering and creating references in Android. There is a parameter in every Drawable variable called mCallback. When you want to skip setting ImageView it's value is null, else it has a WeakReference variable - this means something like app would say "Look, reference is bound to somewhere in the memory, now I can use it!" Looks like setImageDrawable() method creates this binding, while setCompoundDrawables() doesn't.
I'm not an expert in this topic and what I've found is just a workaround (maybe you will need an ImageLoader-like object to handle this), but looks like using mtextview.setCompoundDrawablesWithIntrinsicBounds() works well.
//mImageview.setImageDrawable(mDrawable); You can delete this line
//Using this will not require to load your Drawable somewhere else
mtextview.setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);

EditText get drawable left

Is there a way to get EditText current left drawable ?
I have some EditText where i set the left drawable whit those lines of code :
cod.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ok, 0, 0, 0);
cod.setCompoundDrawablesWithIntrinsicBounds(R.drawable.notok, 0, 0, 0);
And i want at a certain time to check what is the current drawable on my EditText.
I search but i did not find a way to get current drawable. If there is no way to do this : is there a way to add a note to my EditText? Like setting color, TextSize.... a way to set a note (text).
Thanks!
try this edittext.setTag(note); , here note can be any type of object. here is a supported documentation
To answer your question, you need to use the method "getCompoundDrawables()". This will return a drawable array with drawables for the left, top, right, and bottom borders.
I haven't used this method yet but i believe the left drawable would logically be [0].

setCompoundDrawablesWithIntrinsicBounds vs. android:drawableRight

What is the difference between setting a drawable via xml like
android:drawableRight="#drawable/arrow_right_normal"
and setting a drawable via code like
bt.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.arrow_right_normal);
Because in the first case everything works like expected (Buttontext is center horizantal and the icon is in the middle of the right side).
And in the second case the icon is at the bottom/middle of the Button and the Text is at the top left side.
right is the third parameter. The last one is bottom
bt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.arrow_right_normal,0);
Here the documentation
Drawable drawable = ResourcesCompat.getDrawable(
getResources(),
R.drawable.visibility_off,
getTheme()
);
textView.setCompoundDrawablesWithIntrinsicBounds(null,null,drawable,null);

Android align drawable left to the first line of my textView

I have a textView in which I put some text (obviously) and a Drawable left. Unfortunately, when I use phones with small screens like a Samsung Galaxy Y, instead of just a single line, the text runs over to a second line. (which is fine). However, the drawable that I set now aligns itself to the center of the textview, and not on the left of the first line.
I want my drawable to stay aligned to the first line of text in my textview, no matter how many lines of text, my textview holds. Is there a way to do this? Can anyone point me in the right direction?
my implementation is pretty straightforward
...
tv.setCompoundDrawables(drawableLeft, null, null, null);
where my drawableLeft is the image from my drawable folder.
Thanks a lot!
Try to use a drawable like a separate view (ie ImageView) and not to set it as a tag in a TextView. Using that will grant you more control over the drawable/ImageView it self.
In order to align a Drawable to the top of your TextView you can use a custom Drawable that wraps the Drawable you want to show. Then, override the method onDraw(Canvas) in your custom Drawable and translate the Canvas to manipulate the position of your Drawable.
See my answer here for an example.

Categories

Resources