setCompoundDrawablesWithIntrinsicBounds vs. android:drawableRight - android

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);

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);

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].

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.

Buttons text is not aligned any more after changing the BackgroundDrawable

i am changing my buttons' backgroundDrawable programaticaly:
myButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.card_button_blue));
After changing the background, the text in the button is not aligned any more like it was before changing the background.
(source: dsliga.eu)
target SDK version is 10.
Thanks.
Looks like the setText() method messes up the alignment. After changing the text, re-applying the gravity and padding did the job:
myButton.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
myButton.setPadding(12, 12, 12, 12);
Since you are using a 9patch, you need to specify with bottom and right side lines where the data is inside your drawing, you probably did not draw a full line at the bottom, and right side of the 9patch drawing
this is why the text inside your button stopped being centered, because the bounds of the 9patch are NOT what you think they are
your 9patch should (normally), leave only 1 pixel at the top and bottom of the right side line, and 1 pixel at the left and right of the bottom line
redraw the 9patch, remove the gravity and padding code, and see if it works
see below image

Set the side of an image when using android:drawable____ in a button

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

Categories

Resources