Swelling the shape list layer in android

I have the following file in my dropdown folder named /drawable/mybkg.xml . I want to inflate it to change colors programmatically. Is this possible in Android?

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape android:shape="oval" > <padding android:top="5dp" android:bottom="2dp" android:left="2dp" android:right="2dp" /> <solid android:angle="270" android:color="#1bd4f6" /> <stroke android:width="2dp" android:color="#121ce5" /> </shape> </item> <!-- Foreground --> <item> <shape android:shape="oval" > <solid android:color="#1b90f6" /> </shape> </item> </layer-list> 

I just saw a class called LayerDrawable . Does anyone know how I could convert my XML file into such a LayerDrawable either by writing code or bloating existing xml?

+6
source share
1 answer

You should use the Resources.getDrawable(int id) method. Here is the documentation: http://developer.android.com/reference/android/content/res/Resources.html#getDrawable(int) It inflates any available resources. The only thing left for you to do is to return the returned drawing to the most specific type that you need (in your case, LayerDrawable ). Here is an example:

 // calling from Activity LayerDrawable d = (LayerDrawable) getResources().getDrawable(R.drawable.my_drawable); 
+1
source

Source: https://habr.com/ru/post/950961/


All Articles