How to Create Drawable from Resource

How to create Drawable from resource

Your Activity should have the method getResources. Do:

Drawable myIcon = getResources().getDrawable( R.drawable.icon );



As of API version 21 this method is deprecated and can be replaced with:

Drawable myIcon = AppCompatResources.getDrawable(context, R.drawable.icon);

If you need to specify a custom theme, the following will apply it, but only if API is version 21 or greater:

Drawable myIcon =  ResourcesCompat.getDrawable(getResources(), R.drawable.icon, theme);

How can I get drawable in Kotlin?

at first, check that drawable in the root drawable, not in xxx or something else

  • remove id from Vector shape as well

in your Activity do

val drawble = resources.getDrawable(R.drawable.ic_training,theme)

if in Fragment

val drawble = context!!.resources.getDrawable(R.drawable.ic_training,context!!.theme)

How do you obtain a Drawable object from a resource id in android package?

Drawable d = getResources().getDrawable(android.R.drawable.ic_dialog_email);
ImageView image = (ImageView)findViewById(R.id.image);
image.setImageDrawable(d);

How can I get drawable resource by string?

You can use LocalContext to get current context, and then use same methods as you used in view based Android:

val context = LocalContext.current
val drawableId = remember(name) {
context.resources.getIdentifier(
name,
"drawable",
context.packageName
)
}
Image(
painterResource(id = drawableId),
contentDescription = "..."
)

How to create this layout in Drawable Resource and make it as background in activity

there are many ways to design this layout

first set image as a background like this

android:background="@drawable/dummy"

Second create custom layout and used as a back ground like this

laout_custom_bg.xml (add in layout)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".2"
android:background="#fff"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".8"
android:background="#2196F3"/>
</LinearLayout>

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<include
layout="@layout/layout_custom_bg"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#FF9800"/>
</LinearLayout>
</FrameLayout>

Sample Image

xml drawable or programmatically made drawable?

First of all, there is no big difference in performance.

The advantages of both methods are as follows:

Advantages of xml

  1. Easy to reuse views or to bring and write from different layouts.
  2. View is separated from logic, making it easy to manage in the long run.
  3. Previews give us a preview of which view they represent.

Advantages of drawing with code

  1. The view can be flexibly adjusted according to the state of logic.
  2. There is no need to create a separate resource file.

Getting Drawable Resource

You're very close, but I think you're mixing Java and C#.

You can access this using the Resource structure. I've put both the Mono for Android and Java versions below for reference but in your case you want the C# version if you're using Mono for Android.

Mono for Android (C#)

Drawable icon = Resources.GetDrawable(Resource.Drawable.Icon);

Android SDK (Java)

Resources res = getResources();
Drawable icon = res.getDrawable(R.drawable.Icon);


Related Topics



Leave a reply



Submit