1

How can I create Drawable from XML source code provided as a string?

I found the following method:

Drawable drawable = Drawable.createFromXml();

But it requires to provide XmlResourceParser - and I still don't see a way to create it from XML source code.

Sample of the XML source code:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap
            android:src="@drawable/icon"
            android:gravity="right"
            />
    </item>
</layer-list>
Dmitry
  • 14,306
  • 23
  • 105
  • 189

3 Answers3

0

FYI

createFromXml()

Create a drawable from an XML document using an optional Resources.Theme.

Try this

public class RecyclerViewActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);

        Drawable testDrawable;

        Resources res = getResources();
        try {
            testDrawable = Drawable.createFromXml(res, res.getXml(R.xml.test));
        } catch (Exception ex) {
            Log.e("Error", "Exception creating drawable from XML");
        }

    }


}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • I want to create it from XML code - not from XML file. – Dmitry Aug 30 '18 at 06:18
  • @Dmitry **FYI** **[createFromXml](https://developer.android.com/reference/android/graphics/drawable/Drawable#createFromXml(android.content.res.Resources,%20org.xmlpull.v1.XmlPullParser,%20android.content.res.Resources.Theme))** `Create a drawable from an XML document using an optional Resources.Theme` – AskNilesh Aug 30 '18 at 06:28
  • @Dmitry get Your drawable like this `drawable= ContextCompat.getDrawable(this,R.drawable.test);` – AskNilesh Aug 30 '18 at 06:34
  • [Convert base64 string to image in Android](https://stackoverflow.com/a/42295088/5712938) – grabarz121 Aug 30 '18 at 07:07
  • @pskink I don't feel, what is he really want to achieve `LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(context, R.drawable.drawable_item);`, and `someView.setBackgroundDrawable(shape);`. `layer-list` is a drawable, which he can use like a normal drawable image. But in a title is something about image in string form. – grabarz121 Aug 30 '18 at 07:27
  • Please remove this answer and comments because they have nothing to do with my question. – Dmitry Aug 30 '18 at 07:43
0

I just think of this, maybe not exactly syntactically correct but I believe this is something you want.

public Drawable xmlStringToDrawable(String yourString){
     XmlPullParser parser = Xml.newPullParser();
     parser.setInput(new StringReader(yourString));
     return Drawable.createFromXml(getResources(),parser)
}
Kit Mak
  • 231
  • 2
  • 4
  • 1
    Btw, as @pskink said, if you want to dynamically change a drawable that is set to some view. You should not parse a xml, since xml parsing is a very slow process. What you should do is to programmatically set drawable to a view. If you want to change attributes like gravity of a view, you can also change the **layoutParams** of the view instead. – Kit Mak Aug 30 '18 at 07:55
  • I'm getting `error: unreported exception XmlPullParserException; must be caught or declared to be thrown` with this code. – Dmitry Aug 30 '18 at 07:56
  • How can I change `layoutParams` of the background??? It's just `Drawable` and it doesn't have any `layoutParams`. – Dmitry Aug 30 '18 at 07:57
  • I have not use this api from the SDK ever, but you can try wrap it with a try catch block if it said so. – Kit Mak Aug 30 '18 at 07:58
  • May I ask when will your drawable be used ? Will it eventually be set to a View? If so, you should change the layout params of the view. I think you are mixing up some of the concepts here. Drawable does not contain any information about how it should be placed in the view, so if you need to adjust it's position, you should explicitly pass information and set it programmatically. – Kit Mak Aug 30 '18 at 08:02
  • The XML code from the question works fine. I can't change layout (it's private API) - I can only set background of already existing view. – Dmitry Aug 30 '18 at 08:06
0

This is not an answer to the question. But it solved my problem. Thanks everyone for the help:

        Drawable icon = resources.getDrawable(resources.obtainTypedArray(R.array.arrayName).getResourceId(index, 0), context.getTheme());
        BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
        bitmapDrawable.setGravity(Gravity.RIGHT);

I generate only index on run-time in this approach.

Dmitry
  • 14,306
  • 23
  • 105
  • 189