A
A
Artem2012-09-08 14:42:56
Android
Artem, 2012-09-08 14:42:56

How to programmatically customize the appearance of a widget?

I have a widget:
Markup:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:layout_margin="2dip"
    android:background="@drawable/widget"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dip"
        android:layout_marginRight="3dip"
        android:layout_marginTop="3dip"
        tools:ignore="UseCompoundDrawables" >

        <TextView
            android:id="@+id/widget_title"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/app_name"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ImageView
            android:id="@+id/edit_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dip"
            android:contentDescription="@string/edit"
            android:src="@drawable/widget_edit" />
    </LinearLayout>

    <TextView
        android:id="@+id/widget_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="4dip"
        android:layout_marginRight="4dip" />

</LinearLayout>

and @drawable/widget:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <stroke
        android:width="1dp"
        android:color="#FFFFFFFF" />

    <solid android:color="#00000000"/>

    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />

</shape>

You need to be able to set the widget's background color, widget's transparency, border color, and font color/size for both TextViews in the widget's configurator. The question is how do I get the TextView object on the widget and how do I access the properties specified in the shape?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2012-09-08
@bartwell

Access widget_title:
findviewbyid(R.id.widget_title)
Access widget_content:
findviewbyid(R.id.widget_content)
Access shape:
getResources().getDrawable(R.drawable.widget);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question