Note that there are some explanatory texts on larger screens.

plurals
  1. POChange background Drawable padding dynamically
    primarykey
    data
    text
    <p>I want to Create a View that receives a percentage and displays only this part of background dynamically, as a parameter.</p> <p>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).</p> <p>I tried to create a DrawableGradient as a View background, but it overlaps whole background and I cannot resize this background.</p> <p>My next choice was to create InsetDrawable or Layer-List like in this answer: <a href="https://stackoverflow.com/questions/13591836/android-drawable-with-background-and-gradient-on-the-left">Android drawable with background and gradient on the left</a>, but I cannot change the padding dynamically.</p> <p>The example I use currently: res/layout/acitvity_main.xml</p> <pre><code>&lt;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" &gt; &lt;TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/percentage_background_80" android:text="80%" android:textSize="16sp"/&gt; &lt;TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/percentage_background_20" android:text="20%" android:textSize="16sp"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>drawable/percentage_background_20.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android" &gt; &lt;item android:bottom="5dp" android:drawable="@drawable/background" android:left="5dp" android:right="5dp" android:top="5dp"/&gt; &lt;item android:bottom="5dp" android:drawable="@drawable/view_background" android:left="5dp" android:right="200dp" android:top="5dp"/&gt; &lt;/layer-list&gt; </code></pre> <p>drawable/percentage_background_80.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android" &gt; &lt;item android:bottom="5dp" android:drawable="@drawable/background" android:left="5dp" android:right="5dp" android:top="5dp"/&gt; &lt;item android:bottom="5dp" android:drawable="@drawable/view_background" android:left="5dp" android:right="50dp" android:top="5dp"/&gt; &lt;/layer-list&gt; </code></pre> <p>drawable/background.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" &gt; &lt;solid android:color="@android:color/white" /&gt; &lt;/shape&gt; </code></pre> <p>drawable/view_background.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item&gt;&lt;shape&gt; &lt;gradient android:angle="0" android:startColor="@android:color/holo_blue_dark" android:endColor="@android:color/white" android:type="linear" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;/selector&gt; </code></pre> <p>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?</p> <p><img src="https://i.stack.imgur.com/vnLSf.png" alt="Background with percentage"></p> <p>Clarification: in drawable/percentage_background_80.xml and drawable/percentage_background_20.xml there are elements:</p> <pre><code>android:right="XXXdp" </code></pre> <p>I want to change the values XXXdp programatically.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload