user1234
user1234

Reputation: 679

Android: Widget Won't Show On Home Screen

I have been following the App Widgets tutorial from the official Android Developer page: http://developer.android.com/guide/topics/appwidgets/index.html

I see the widget in 'Widgets', and when I go to add it to the main home screen, it loads the specified Activity, but the view does not display on the home screen.

Here is the layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center"
          android:background="@drawable/ic_launcher">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:text="My Widget..." />

<Button
    android:id="@+id/myButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:text="My test button" />

</LinearLayout>

Here is the app widget info:

<?xml version="1.0" encoding="utf-8"?>

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
                android:minWidth="72dp"
                android:minHeight="72dp"
                android:updatePeriodMillis="86400000"
                android:previewImage="@drawable/ic_launcher"
                android:initialLayout="@layout/widget_main"
                android:configure=".MainActivity"
                android:resizeMode="horizontal|vertical"
                android:widgetCategory="home_screen|keyguard"
                android:initialKeyguardLayout="@layout/widget_main">
</appwidget-provider>

And here is the receiver in the manifest:

<receiver android:name="MyWidget" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="android.appwidget.action.ACTION_WIDGET_RECEIVER"/>
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
                   android:resource="@xml/mywidget_info" />
    </receiver>

Upvotes: 1

Views: 2871

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199825

From the App Widgets Guide on Configuring Activity:

  • The App Widget host calls the configuration Activity and the configuration Activity should always return a result. The result should include the App Widget ID passed by the Intent that launched the Activity (saved in the Intent extras as EXTRA_APPWIDGET_ID).

When you are updating the widget for the first time (following on from the guide), are you ensuring you are using

Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);

before calling finish()?

Upvotes: 1

Related Questions