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.

Community
  • 1
  • 1
Benjamin
  • 3,217
  • 2
  • 27
  • 42

2 Answers2

1

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

pskink
  • 23,874
  • 6
  • 66
  • 77
-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.

Community
  • 1
  • 1
artemiygrn
  • 1,036
  • 7
  • 18
  • 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 Nov 07 '13 at 08:04
  • @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. – artemiygrn Nov 07 '13 at 08:09
  • 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. – Benjamin Nov 07 '13 at 08:16
  • 1
    Why not use any layout container for TextView and change background drawable only? – artemiygrn Nov 07 '13 at 08:21
  • This is the question. How to create a background drawable with dynamically set background parameters... – Benjamin Nov 07 '13 at 08:23
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40705/discussion-between-kvirair-and-benjamin) – artemiygrn Nov 07 '13 at 08:34