Android load drawable programmatically and resize it
Found it:
/**
* Loads image from file system.
*
* @param context the application context
* @param filename the filename of the image
* @param originalDensity the density of the image, it will be automatically
* resized to the device density
* @return image drawable or null if the image is not found or IO error occurs
*/
public static Drawable loadImageFromFilesystem(Context context, String filename, int originalDensity) {
Drawable drawable = null;
InputStream is = null;
// set options to resize the image
Options opts = new BitmapFactory.Options();
opts.inDensity = originalDensity;
try {
is = context.openFileInput(filename);
drawable = Drawable.createFromResourceStream(context.getResources(), null, is, filename, opts);
} catch (Exception e) {
// handle
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e1) {
// log
}
}
}
return drawable;
}
Use like this:
loadImageFromFilesystem(context, filename, DisplayMetrics.DENSITY_MEDIUM);
set width and height of a drawable object in runtime process in android
Drawable drawable = getResources().getDrawable(R.drawable.yourAwesomeDrawable);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
// Scale it to 100 x 100
Drawable newDrawable = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 100, 100, true));
You could also try setBounds()
, but I am not quite sure..
Changing the drawable size inside a button
Solution 1:
Adding a drawable to a button has very limited functions. You can't change the drawable size, so the best way to fix it is to add a button with an image view beside it in a linear layout like this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/ronaldo"
android:orientation="horizontal">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:background="@null"
android:text="Play Game"
android:textColor="#ff0000"
android:textStyle="italic" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="@drawable/messi" />
</LinearLayout>
Solution 2:
You can also do the following if you prefer this one, but you will have to add onClickListener() for each one of them:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/ronaldo"
android:orientation="horizontal">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:background="@null"
android:text="Play Game"
android:textColor="#ff0000"
android:textStyle="italic" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="@drawable/messi" />
</LinearLayout>
Solution 3:
As you have suggested in the comments, you would do something like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_gravity="center"
android:background="@drawable/ronaldo">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_alignLeft="@+id/buttonLayout"
android:layout_alignRight="@+id/buttonLayout"
android:background="@null" />
<LinearLayout
android:id="@+id/buttonLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:gravity="center"
android:text="Play Game"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ff0000"
android:textStyle="italic" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="@drawable/messi" />
</LinearLayout>
</RelativeLayout>
how to change a drawableLeft icon size on a button?
Wrap your resource in a drawable that defines your desired size similar to:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/icon"
android:width="@dimen/icon_size"
android:height="@dimen/icon_size"
/>
</layer-list >
After that, use this drawable in your android:drawableLeft
tag
Set size of button with drawable background dynamically/programmatically
try this way
final Button buttonStudy = (Button) findViewById(R.id.button_study);
buttonStudy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewGroup.LayoutParams params = buttonStudy.getLayoutParams();
params.width = 100;//change the width size
params.height= 100;//change the hight size
buttonStudy.setLayoutParams(params);
}
});
cheerz
Related Topics
How to Center Crop a Background Image of Linear Layout
Java.Sql.Sqlexception: Field Doesn't Have a Default Value
Simplifying the If-Else Condition Using Java Functional Style Programming
Keytool Error Bash: Keytool: Command Not Found
Scheduling a Job With Spring Programmatically (With Fixedrate Set Dynamically)
Typecasting an Object from Parent Class to Child
Spring Boot Application Shutdown Immediate After Starting
How to Change Cookie Processor to Legacycookieprocessor in Tomcat 8
Spring Webclient: How to Stream Large Byte[] to File
Deserialization With @Jsonsubtypes for No Value - Missing Property Error
How to Do Name Validation Allowing Alphabet, Spaces and Dot in Android
Automatically Convert Style Sheets to Inline Style
Splitting a String Containing Special Characters in to Individual Characters
How to Access Variable Outside a Try Catch Block
Spring Data JPA Problem With Updating Multiple Entities
Java.Time.Format.Datetimeparseexception: Text Could Not Be Parsed At Index 3