1

I am trying to update an existing Android 4+ app to supply a widget. I followed the tutorial on developer.android.com but was not able to get it running. The widget does not show up in the widget list and can thus not be installed/used on the home screen.

This is what I did to add the widget to the app:

1. Added the WidgetProvider and Settings Activity to the Manifest file:

    <!-- Widget -->
    <receiver
        android:name=".Widget.WidgetProvider"
        android:label="Example Widget"
        android:icon="@drawable/some_drawable">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/appwidget_info" />
    </receiver>
    <activity
        android:name=".Widget.WidgetSettingsActivity"
        android:label="@string/Settings" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
        </intent-filter>
    </activity>

2. Added the WidgetProvider

package com.mycompany.myapp.Widget;

import android.appwidget.AppWidgetProvider;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;

public class WidgetProvider extends AppWidgetProvider{
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        for (int i = 0; i < appWidgetIds.length; ++i) {
            // Do something
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
}

3. Added the res/xml/appwidget_info.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android">
    android:minWidth="50dp"
    android:minHeight="50dp"
    android:minResizeHeight="50dp"
    android:minResizeWidth="50dp"
    android:updatePeriodMillis="0"
    android:initialLayout="@layout/widget__content"
    android:configure="com.mycompany.myapp.Widget.WidgetSettingsActivity
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen">
</appwidget-provider>

4. Added the Settings Activity

// From the developer.android.com Example
package com.mycompany.myapp.Widget;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;

import android.os.Bundle;
import android.view.View;

import com.mycompany.myapp.R;

public class WidgetSettingsActivity extends Activity {
    private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the result to CANCELED.  This will cause the widget host to cancel
        // out of the widget placement if they press the back button.
        setResult(RESULT_CANCELED);

        // Set the view layout resource to use.
        setContentView(R.layout.widget__settings);

        // Find the EditText
        //mUsername = (EditText)findViewById(R.id.username);
        //mPassword = (EditText)findViewById(R.id.password);

        // Bind the action for the save button.
        //findViewById(R.id.save_button).setOnClickListener(mOnClickListener);

        // Find the widget id from the intent.
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null)
            appWidgetId =     extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

        // If they gave us an intent without the widget id, just bail.
        if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
            finish();
    }
}

Und das Layout in /res/layout/

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"
    android:layout_height="match_parent"     android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.mycompany.myapp.Widget.WidgetSettingsActivity">

    <TextView android:text="@string/Settings" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

5. Added the Widget Layout in /res/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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="@dimen/margin_default"
        android:layout_marginRight="@dimen/margin_default"
        android:layout_marginTop="@dimen/margin_default"
        android:layout_weight="20" >

        <Button
            android:id="@+id/TButton"
            style="@style/BigButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="@dimen/margin_medium"
            android:layout_weight="1"
            android:text="@string/Test" />

        <Button
            android:id="@+id/IButton"
            style="@style/BigButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="@dimen/margin_medium"
            android:layout_weight="1"
            android:text="@string/New" />
    </LinearLayout>
</LinearLayout>

The app compiles and runs without any error. But I cannot access/see/use the widget anywhere.

I found other questions regarding this topic but they all had different solutions (e.g. missing intent-filter, installed on SD card instead in internal memory, etc.) which are not the case here.

Any idea what I might be doing wrong?

Andrei Herford
  • 17,570
  • 19
  • 91
  • 225

1 Answers1

1

Got it! After another two hours of searching I found the error. Thanks to a hint in another question:

The appwidget_info.xml was malformed:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android">
    ...
    android:widgetCategory="home_screen">
</appwidget-provider>

The >at the end of the second line is wrong since the tag is closed in the second last line: ...="home_screen">

So the correct xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="50dp"
    android:minHeight="50dp"
    android:minResizeHeight="50dp"
    android:minResizeWidth="50dp"
    android:updatePeriodMillis="0"
    android:initialLayout="@layout/widget__content"
    android:configure="com.mycompany.myapp.Widget.WidgetSettingsActivity
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen">
</appwidget-provider>

I wonder why Android Studio does not show any error or warning about this. Would have saved me a lot of time... Additionally I have no idea why I made the same mistake as the user linked in the question above. Maybe I copied the code from a bad example?

However: If any one encounters the same issue, check your XML first :-)

Community
  • 1
  • 1
Andrei Herford
  • 17,570
  • 19
  • 91
  • 225