Karl Konrad
Karl Konrad

Reputation: 15

Create drawable programmatically

How can I create this exact drawable in java code only?

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <size android:width="1dp" />
    <solid android:color="#ff0000" />

    <padding
        android:bottom="-15px"
        android:top="-5px" />
</shape>

I need the padding values to be variable so I can't do it as an xml.

Upvotes: 1

Views: 970

Answers (1)

ugur
ugur

Reputation: 3654

If you wanna draw something custom, you should open a Canvas and make your drawing there.

Read this: https://developer.android.com/training/custom-views/custom-drawing.html

And it seems a simple shape. So you can add a layout view (View,TextView, ImageView, etc.) set background with this drawable and adjust padding through this view.

Upvotes: 2

Related Questions