bwooster47
bwooster47

Reputation: 61

Android widget not showing on 4.3

Widget not showing in widget list had a similar question, and there are many other questions where the answer is basically: reboot, start app, wait, etc.

After the app install, it shows the widget on a Android 2.3 device. So the code is fine. But it never shows up on a 4.3 device. So 4.3 is looking for something that is not there.

Does anyone have any additional tips?

AndroidManifest.xml

    <receiver
        android:name=".WidgetProvider"
        android:label="@string/widget_name">
        <intent-filter>
           <action
               android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data 
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_info" />
    </receiver>

widget_info.xml

   <?xml version="1.0" encoding="utf-8"?>
   <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" >
    android:minWidth="208dp"
    android:minHeight="56dp"
    android:minResizeHeight="48dp"
    android:minResizeWidth="48dp"
    android:updatePeriodMillis="86400000"
    android:previewImage="@drawable/ic_launcher"
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen"
    android:configure=""
    android:initialLayout="@layout/widget_layout"
  </appwidget-provider>

WidgetProvider.java

public class WidgetProviderextends AppWidgetProvider {
   DateFormat df = new SimpleDateFormat("hh:mm:ss");

   public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
     final int N = appWidgetIds.length;

     Log.i("ExampleWidget",  "Updating widgets " + Arrays.asList(appWidgetIds));

     // Perform this loop procedure for each App Widget that belongs to this
     // provider
     for (int i = 0; i < N; i++) {
       int appWidgetId = appWidgetIds[i];

       // Create an Intent to launch ExampleActivity
       Intent intent = new Intent(context, ExampleActivity.class);
       PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

       // Get the layout for the App Widget and attach an on-click listener
       // to the button
       RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
       views.setOnClickPendingIntent(R.id.new_post, pendingIntent);

       // To update a label
       views.setTextViewText(R.id.blogname, df.format(new Date()));

       // Tell the AppWidgetManager to perform an update on the current app
       // widget
       appWidgetManager.updateAppWidget(appWidgetId, views);
     }
   }

All the code above is mostly example code for testing, and it works fine on Android 2.3 just not on 4.3... There are no errors when running the debug build in the logcat output.

Upvotes: 0

Views: 1882

Answers (3)

Mick
Mick

Reputation: 681

The "sample out there with that err" is in Android Application Development for Dummies ISBN 978-1-118-38710-8 page 179.

Upvotes: 0

bwooster47
bwooster47

Reputation: 61

Argh! Finally redid the whole thing, and it now works - there was a single character typo in the original code. The > character in widget_info.xml was wrongly placed. It should be placed after all the attributes, not before. This was not reported as a problem in eclipse, nor in building the app, nor in running the app. And on Android 4.2, the defaults must be such that it allows widget install to proceed, while on 4.3, the defaults probably disable the install.

One small clue in the posting above - Stack Overflow highlights each XML attribute in AndroidManifest.xml but not in the widget_info.xml shown in the original post. Fixing the > character fixes the highlighting in eclipse too.

widget_info.xml

   <?xml version="1.0" encoding="utf-8"?>
   <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="208dp"
    android:minHeight="56dp"
    android:minResizeHeight="48dp"
    android:minResizeWidth="48dp"
    android:updatePeriodMillis="86400000"
    android:previewImage="@drawable/ic_launcher"
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen"
    android:configure=""
    android:initialLayout="@layout/widget_layout" >
  </appwidget-provider>

Upvotes: 2

hasanghaforian
hasanghaforian

Reputation: 14022

It may be caused if your App has not any Launcher Activity.After API 11,only those broadcast receivers(and so,only those widgetProviders) can be registered that their App can be launched by Activity.

Upvotes: 0

Related Questions