1

I want to Create a View that receives a percentage and displays only this part of background dynamically, as a parameter.

E.g. when creating a view with parameter 0.8; the background is drawn only in 80% of View width (0% padding left, 20% padding right).

I tried to create a DrawableGradient as a View background, but it overlaps whole background and I cannot resize this background.

My next choice was to create InsetDrawable or Layer-List like in this answer: Android drawable with background and gradient on the left, but I cannot change the padding dynamically.

The example I use currently: res/layout/acitvity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="5dp"
tools:context=".MainActivity" >

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/percentage_background_80"
    android:text="80%" 
    android:textSize="16sp"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/percentage_background_20"
    android:text="20%"
    android:textSize="16sp"/>

</LinearLayout>

drawable/percentage_background_20.xml

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

    <item
        android:bottom="5dp"
        android:drawable="@drawable/background"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp"/>
    <item
        android:bottom="5dp"
        android:drawable="@drawable/view_background"
        android:left="5dp"
        android:right="200dp"
        android:top="5dp"/>

</layer-list>

drawable/percentage_background_80.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:bottom="5dp"
        android:drawable="@drawable/background"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp"/>
    <item
        android:bottom="5dp"
        android:drawable="@drawable/view_background"
        android:left="5dp"
        android:right="50dp"
        android:top="5dp"/>
</layer-list>

drawable/background.xml

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

drawable/view_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item><shape>
            <gradient
                android:angle="0"
                android:startColor="@android:color/holo_blue_dark"
                android:endColor="@android:color/white"
                android:type="linear" />
        </shape>
   </item>
</selector>

This is how it looks, but I cannot change the values of padding in a list from a source code. Do you know how can I achieve it?

Background with percentage

Clarification: in drawable/percentage_background_80.xml and drawable/percentage_background_20.xml there are elements:

android:right="XXXdp"

I want to change the values XXXdp programatically.

8
  • 1
    why dont you use a ClipDrawable ? Commented Nov 7, 2013 at 8:16
  • Well, ClipDrawable is almost what I need, but it clips the Drawable from both sides. I couldn't clip it only from the right-hand side. Commented Nov 7, 2013 at 8:22
  • 1
    use proper Gravity in ClipDrawable constructor Commented Nov 7, 2013 at 8:33
  • Oh yeah, you were right. But there is still question, because ClipDrawable removes some part of the background drawable, so GradientDrawable has no use here. Do you know other Drawable, that resizes its child instead of clipping it? Commented Nov 7, 2013 at 8:41
  • 1
    ScaleDrawable? i suggest some docs reading on Drawable class Commented Nov 7, 2013 at 8:46

2 Answers 2

1

either use ClipDrawable or ScaleDrawable depending on wheter you just want to clip target Drawable or resize it

Sign up to request clarification or add additional context in comments.

Comments

-1

Need add to:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/percentage_background_80"

    android:padding="your_padding"

    android:text="80%" 
    android:textSize="16sp"/>

This should works. For dynamically, please read this:

how to manually set textview padding?

Adding Margins to a dynamic Android TextView

EDIT:

Also, you may add your TextView to layout and change background, like this:

<LinearLayout      
  android:id=@"+id/your_id"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/percentage_background_80"
        android:text="80%" 
        android:textSize="16sp"/>

</LinearLayout>

And in your class just use method findViewById and set background.

6 Comments

This would add padding to the TextView, so the long text won't match the whole width of a TextView. I only want to change the background.
@Benjamin, what do you mean by 'so the long text won't match the whole width'? You also may use paddingTop, paddingBottom for this purposes.
i want to create a background like displayed above, not to create padding in a TextView. I want to change background drawable only, without any change in a TextView view itself. Your proposal changes TextView padding, so the inside would be padded as well.
Why not use any layout container for TextView and change background drawable only?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.