android - Programmatically assign color to shape -


i have layout this:

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android">     <item android:id="@+id/user_color">         <shape android:shape="rectangle">             <solid android:color="#000000" />         </shape>     </item>     <item android:right="25dp">         <shape android:shape="rectangle">             <solid android:color="@color/user_background" />         </shape>     </item> </layer-list> 

how i'm calling shape:

<linearlayout     android:orientation="horizontal"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:background="@drawable/shape"> 

how can programmatically change color of rectangle shape id user_color?

    @user5262809          // add id linear layout          <linearlayout          android:id="@+id/linearlayout"         android:orientation="horizontal"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:background="@drawable/shape">          // in code :         linearlayout linearview = (linearlayout) findviewbyid(r.id.linearlayout);          // added anitha's code here     int colortopaint = getresources().getcolor(android.r.color.white);// color want     drawable tempdrawable =    getresources().getdrawable(r.drawable.xml_layout);     layerdrawable bubble = (layerdrawable) tempdrawable; //cast root element in xml     gradientdrawable solidcolor = (gradientdrawable) bubble.finddrawablebylayerid(r.id.user_color); solidcolor.setcolor(colortopaint);     // ,      linearview.setbackground(tempdrawable);        // hope helps :) 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -