Android Drawable with partial filling

I would like to use a custom function to give the button rounded corners. However, I would also like the bottom 1/4 of the rectangle to be filled with a specific color. Is it possible? How can I just fill the bottom quarter?

+3


source to share


1 answer


Yes, it is possible if you are using the Layer List and define overlapping items. Here's an example ...

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
        <shape android:shape="rectangle">
            <corner android:radius="5"/>
            <solid android:color="#f000"/>
            <size android:width="200dp"
                android:height="30dp"/>
        </shape>
    </item>

    <item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
        <shape android:shape="rectangle">
            <corner android:radius="5"/>
            <solid android:color="#0f0"/>
            <size android:width="50dp"
                android:height="30dp"/>
        </shape>
    </item>
</layer-list>

      

You may need to play around with these values ​​and adjust them to your needs.

Edit

I don't think you can give relative values ​​as percentages. However, if these values ​​differ depending on the screen sizes, you can (and I recommend) move those values ​​into the dimension resource file as shown below ...

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="button_full_width">200dp</dimen>
    <dimen name="button_quarter_width">50dp</dimen>
    <dimen name="button_height">30dp</dimen>
</resources>

      



then indicate these dimensions in your drawings as shown below ...

...
<size android:width="@dimen/button_full_width"
                android:height="@dimen/button_height"/>
...

      

Now, to specify different values ​​for different screen sizes, you need to specify resource qualifiers that target different sizes by adding different "values" resource folders and putting dimension files with different values ​​in them. The Android system will pick and load the required dimensions at runtime based on the screen size of the device ...

res/
    values/
            dimens.xml
    values-small/
            dimens.xml
    values-normal/
            dimens.xml
    values-large/
            dimens.xml
    values-xlarge/
            dimens.xml

      

for more information check the documentation

+3


source







All Articles