Programmatically configure widget face

How to programmatically configure widget face? I have create widget:

Layout:

<?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>

Shape (@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>

I need set background color, background transpanency, border color and font size/color for TextViews in the configuration activity. But how to get TextView object and how to access shape properities?

Answers


Unlike a normal activity, in a widget you cannot directly instantiate a TextView with findViewById.

You will need to instantiate a RemoteViews object, passing your package name as a parameter:

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.my_widget);

And then affect the TextView using the method setInt of the remoteViews object with the original function of the TextView as a parameter:

remoteViews.setInt(R.id.widget_title, "setBackgroundResource", android.R.color.white);

Finally, update the Widget:

ComponentName thisWidget = new ComponentName(context, Widget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, remoteViews);

You can try other combinations using the method public void setInt (int viewId, String methodName, int value) and passing the original methodName as parameter, but for some reason not everything is allowed in the Widget context. Try it out!


Need Your Help

Epiceditor paired with MathJax

javascript mathjax epiceditor

Is there anyway to load a script (such as MathJax) into the EpicEditor preview iFrame? I want my previews to be correct Markdown and then have the javascript run to preview the MathJax content.

is it possible to have a recursive vector in C++?

c++ parsing recursion vector

I am writing a kind of recursive descent parser. In the first stage of my syntax tree, I hold the model pieces I am parsing in a vector. The first piece is the left hand side piece (lhs) and the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.