Pass color attribute from custom view to drawable shape in Android -
i have created custom view uses 2 <shape>
's background in view xml. right have hardcoded <solid color=""/>
of shape, pass color argument code custom view , forward shape.
how achieve this? i'd pass color reference both @+id/rectangle
, @+id/triangle
code.
shape:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="1dp"/> <solid android:color="@color/giddyologyorange"/> <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" /> <size android:height="60dp" /> <stroke android:width="1dp" android:color="#c95800" /> </shape>
view:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <linearlayout android:id="@+id/rectangle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/rectangle" android:orientation="horizontal" android:padding="5dp"> <imageview android:id="@+id/iconimageview" android:layout_width="32dp" android:layout_height="32dp" android:layout_gravity="center_vertical|left" android:contentdescription="icon" android:src="@drawable/iconplaceholder" android:scaletype="fitcenter"/> <view android:id="@+id/spaceview" android:layout_height="match_parent" android:layout_width="10dip"/> <textview android:id="@+id/labeltextview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|left" android:textcolor="@color/white" android:gravity="center|left" android:textsize="16dip" android:maxwidth="150dip" android:text="this long text should wrap"/> </linearlayout> <view android:id="@+id/triangle" android:layout_width="20dp" android:layout_height="20dp" android:layout_gravity="center_horizontal" android:layout_margintop="-1dp" android:background="@drawable/triangle" android:rotation="180"/> </linearlayout>
did try this?
linearlayout myview; myview = (linearlayout)findviewbyid(r.id.rectangle); myview.setbackgroundcolor(current_color);
i have layout has imagebutton
<imagebutton android:id="@+id/color_swatch" android:layout_width="@dimen/standard_btn_width" android:layout_height="@dimen/standard_btn_width" android:background="@drawable/checker_tile" android:contentdescription="@string/paint" android:src="@drawable/paint" />
my src layer-list drawable--
layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape android:shape="rectangle" > <stroke android:width="2dp" android:color="#ff999999" /> <solid android:color="#00000000" /> <padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp" /> </shape> </item> <item> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <stroke android:width="4dp" android:color="#ff999999" /> <solid android:color="#00000000" /> <corners android:radius="0dp" /> </shape> </item> </layer-list>
the background layer-list drawable-
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <gradient android:type="radial" android:gradientradius="500" android:startcolor="#17568a" android:endcolor="#494c4f" /> </shape> </item> <item> <bitmap android:src="@drawable/checkerpattern" android:tilemode="repeat" /> </item> </layer-list>
i able setbackgroundcolor color 50% opacity , see checkerpattern below
Comments
Post a Comment