How to programmatically set drawableLeft on Android button?
You can use the setCompoundDrawables
method to do this. See the example here. I used this without using the setBounds
and it worked. You can try either way.
UPDATE: Copying the code here incase the link goes down
Drawable img = getContext().getResources().getDrawable(R.drawable.smiley);
img.setBounds(0, 0, 60, 60);
txtVw.setCompoundDrawables(img, null, null, null);
or
Drawable img = getContext().getResources().getDrawable(R.drawable.smiley);
txtVw.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);
or
txtVw.setCompoundDrawablesWithIntrinsicBounds(R.drawable.smiley, 0, 0, 0);
Programmatically set left drawable in a TextView
You can use setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)
set 0 where you don't want images
Example for Drawable on the left:
TextView textView = (TextView) findViewById(R.id.myTxtView);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
Alternatively, you can use setCompoundDrawablesRelativeWithIntrinsicBounds to respect RTL/LTR layouts.
Tip: Whenever you know any XML attribute but don't have clue about how to use it at runtime. just go to the description of that property in developer doc. There you will find Related Methods if it's supported at runtime . i.e. For DrawableLeft
How to set drawable in Top-Left in a textview?
Make Custom drawable class by extending a drawable class and use for drawable .eg:
public class TopLeftGravityDrawable extends BitmapDrawable {
private final Drawable mDrawable;
public TopLeftGravityDrawable(Drawable drawable) {
mDrawable = drawable;
}
@Override
public int getIntrinsicWidth() {
return mDrawable.getIntrinsicWidth();
}
@Override
public int getIntrinsicHeight() {
return mDrawable.getIntrinsicHeight();
}
@Override
public void draw(Canvas canvas) {
int halfCanvas= canvas.getHeight() / 2;
int halfDrawable = mDrawable.getIntrinsicHeight() / 2;
// align to top
canvas.save();
canvas.translate(0, -halfCanvas + halfDrawable);
mDrawable.draw(canvas);
canvas.restore();
}
}
Usage :
Drawable drawable = getResources().getDrawable(R.drawable.ic_customer);
TopGravityDrawable gravityDrawable = new TopGravityDrawable(drawable);
drawable.setBounds(0, 6, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
gravityDrawable.setBounds(0, 6, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
textview.setCompoundDrawables(gravityDrawable, null, null, null);
How to programmatically set drawableRight on Android Edittext?
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
DrawableLeft in a TextView using Drawable Object Programmatically
You can use following:
Drawable d = new BitmapDrawable(bm);
d.setBounds(0, 0, bm.getWidth(), bm.getHeight());
textView.setCompoundDrawables(null, d, null, null);
Is it possible to set drawableLeft programmatically?
Yes, use setCompoundDrawablesWithIntrinsicBounds
and define the drawable for the first parameter, then 0 for all the others.
The code should look something like this:
Button b = findViewById(R.id.myButton);
b.setCompoundDrawablesWithIntrinsicBounds(R.drawable.myDrawable, 0, 0, 0);
If your drawable was created in code as well then you need to use the other setCompoundDrawablesWithIntrinsicBounds method which takes 4 drawables, and pass null for all but the left.
How to set android:drawable to left and top?
For this purpose you have to take the seperate imageView like this and in textview field you have to add one line code that android:layout_toRightOf="@+id/imageView":
For better understanding see following code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:background="@mipmap/ic_launcher"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imageView"
android:text="this si fodfjkhsdhaffjsdfasdfjhsdfhjsdhfjhsdfhsdhfhdsjfhsdjhfjhdsfhsdhfjhsdjhfjsdhfjhsdjfhjsdhfjhsdjfhjdshfsdjhfjsdhfsdkjhfjsdhfjhsdjfhjsdhjfhsdjhfjsdhfjhjsdhfjsdhjfhsdjf"
/>
</RelativeLayout>
How to make textview with drawable on the left which could be set programmatically from a url coming from server
Well, the first step is to decode the image from the URL.
You could do something like this:
String yourUrl = "http://someUrl"; // insert your URL here
// connect, get an instance of the InputStream
HttpURLConnection connection = (HttpURLConnection) new URL(yourUrl).openConnection();
InputStream inputStream = connection.getInputStream();
// decode the stream into a Bitmap and create a Drawable from it
Bitmap tempBitmap = BitmapFactory.decodeStream(inputStream);
Drawable drawable = new BitmapDrawable(getResources(), tempBitmap);
And then, set it as a compound drawable on the left side of your TextView
:
// the order is left, top, right, bottom, so you need to set the first param
yourTextView.setCompoundDrawables(drawable, null, null, null);
Programmatically change drawableLeft of Button
Try this:
int imgResource = R.drawable.left_img;
button.setCompoundDrawablesWithIntrinsicBounds(imgResource, 0, 0, 0);
Reference
Related Topics
Overlap Scrolling View with Appbarlayout
Volley Android Networking Library
When to Clear the Cache Dir in Android
What Does Ellipsize Mean in Android
Mediarecorder and Videosource.Surface, Stop Failed: -1007 (A Serious Android Bug)
Accessing Contents of R.String Using a Variable to Represent the Resource Name
Retrofit - Multipart Request: Required Multipartfile Parameter 'File' Is Not Present
Clicking Hamburger Icon on Toolbar Does Not Open Navigation Drawer
Can't Stop the Ringing Alarm from Another Activity
How to Open a PDF Stored Either in Res/Raw or Assets Folder