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);
Related
How to set
android:drawableTop="#drawable/your_drawable_selector"
through java for a RadioButton in android?
You can use setCompoundDrawablesWithIntrinsicBounds, since RadioButton is a subclass of TextView. E.g.
radioButton.setCompoundDrawablesWithIntrinsicBounds (0, R.drawable.your_drawable_selector, 0, 0).
If you have a Bitmap you can use
Drawable drawable = new BitmapDrawable(getResources(), yourBitmap);
radioButton.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
From the documentation
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.
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].
HI'm having an imageview having EditText on that Image. I want to create single Image of that imageview with EditText.
I tryed this,
editTextOptOneInput.buildDrawingCache();
imageViewOptOne.setImageBitmap(editTextOptOneInput.getDrawingCache());
imageViewOptOne.buildDrawingCache();
Bitmap bitmap1 = imageViewOptOne.getDrawingCache();
but chamge my image to black as my text color is black(I guess).
You may use only 1 buildDrawingCache(), the subsequent ones will replace the previous contents.
So, you better group the EditText and the ImageView into a container and shoot that one.
OR...
Instead of using an EditText, just use a TextView.
This one can have one or more compound drawable inside.
So, you may shoot the TextView only.
This is a preferred solution, since it reduces the View and (possibly) the layout count.
[EDIT]
To use compound drawables simply use in xml the android:drawableLeft = "#drawable/your_drawable" (and/or drawableRight, drawableTop, drawableBottom) attribute/s of your TextView.
To set them in Java, use setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom), as found in the official docs: http://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)
You need get drawing cache of Parent of that imageview & EditText.
parentLayout.buildDrawingCache();
Bitmap bitmap1 = parentLayout.getDrawingCache();
Where parentLayout contains imageview & EditText.
I don't understand why you need that but as I understand you should create new Bitmap object from drawing cache before set as source of ImageView
Reminder:
If you are capturing the result image+text from screen, the quality of the output image is certainly compromised
If you want to keep the original quality of image, you should use Canvas and Bitmap to help you
Canvas c=new Canvas();
c.setBitmap(bitmap); // *mutable* copy of bitmap of the image for the ImageView
c.drawText(text, x, y, paint); // font size and typeface can be set through "Paint" class
bitmap.compress(CompressFormat.PNG, 100, new FileOutputStream(out)); // export output
Short question:
Suppose I have a TextView, and I wish that the text itself (not the backround) will have a drawable set to it (and not just a solid color), how should I do it?
The only solution I've found is this one , but it's only for gradient colors, and I wish to be able to use any drawable.
Ok, I think I've found a possible solution, but I have some notes about it:
It's not for any drawable. It's for a bitmap, so you will need to somehow convert it to a bitmap.
It requires that you know the size of the textView (which you can use this for this purpose)
It requires that you have enough memory for the scaled bitmap that is in the same size of the textView.
Not sure how to make it work per character or per line.
Here's the code:
public static void setBitmapOnTextView(final TextView tv, final Bitmap bitmap) {
final TileMode tile_mode = TileMode.CLAMP;
final int height = tv.getHeight();
final int width = tv.getWidth();
final Bitmap temp = Bitmap.createScaledBitmap(bitmap, width, height, true);
final BitmapShader bitmapShader = new BitmapShader(temp, tile_mode, tile_mode);
tv.getPaint().setShader(bitmapShader);
}
I hope that there is a better solution for this.
edited:
I don't believe it's possible to change the actual stroke used on the text, the best closest option you probably have is to use a different font.
In android you can "draw" any text using any *.ttf font.
for that you must include the file in your assets folder and call from code (maybe there's a way using XML, but I don't know how) this:
TypeFace mFont = TypeFace.createFromAsset(mContext.getAssets(), "my_font.ttf");
mTextView.setTypeFace(mFont);
I reckon that is the closes you'll get to have a different stroke on the text.
original answer:
TextView have the following properties:
Drawable Top
Drawable Bottom
Drawable Left
Drawable Right
that you set on the XML. Or you can call
setCompoundDrawables(left, top, right, bottom)
from code!
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.