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].
Related
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);
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.
I have problem with padding in StateListDrawable.
If for some in my styles i define reference on some <selector> with image resources, it set some wrong padding for my 9path images. By the way i set particular image - all is ok. But otherwise, android create StateListDrawable for my <selector> and (as i saw by using debugger on sources) it get padding by use method:
Rect getConstantPadding();
and return wrong values (in my case it not null or 0).
This method use mVariablePadding variable:
if (mVariablePadding) {
return null;
}
But i can't set it false in resources (maybe i did something wrong).
Does someone know solution for this problem? Thanks!
The problem was in 9-path images. For selector it calculate padding from right-bottom border (content) of nine-path drawable.
Beside there is variables in selector android:variablePadding, that calculate that padding (choose the biggest, etc). So it can be still non 0, even if in mostly cases images doesn't have padding.
Simple question here : I am customizing some buttons, placing an icon next to my text via android:drawableLeft or Right, but I can't seem to find how to define the size with accuracy. My image happen to be too big and go out of the shape of my button.
Oh, and while I am at it: android:drawableLeft allows to put an image on the left of the text, but how to decide the distance FROM the text ? In my case, it automatically goes to the extreme left of my button while my text is centered. The result is everything but nice !
Thanks !
I recommend to start from this topic.
but how to decide the distance FROM the text ?
Here you have an answer:
android:drawablePadding can be used to specify the amount of padding between the image and button text.
Source
i guess this will do the trick
use android:drawablePadding in conjunction with android:paddingLeft and android:paddingRight to force the text and drawable inward towards the center of the button...
If you want to define the accuracy size of the drawable, You should use Java code instead of android:drawableLeft
Drawable dw = getResources().getDrawable(R.drawable.ic);
dw.setBounds(0, 0, 100, 100); // set the size of the drawable to 100*100
button.setCompoundDrawables(dw, null, null, null);
because of
android:drawableLeft // equals with setCompoundDrawablesWithIntrinsicBounds
and
android:drawablePadding // this used to control the padding between the text and drawable
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.