0

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.

Community
  • 1
  • 1
bwooster47
  • 61
  • 5

3 Answers3

2

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>
bwooster47
  • 61
  • 5
  • Bless your heart!! You saved my poor tormented soul!!! I guess there is some sample out there with that error and poor people roaming the net searching for an answer to why their widget doesn't work, and all because of a misplaced '>' – EZDsIt Jul 07 '14 at 19:54
0

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.

hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
  • The sentences above make no sense. Are you saying "start the activity once"? I already clarified that the app has been started, uninstalled and installed, rebooted, etc. Nothing works on Android 4.3, while it worked fine on 2.3. With the same code. – bwooster47 Oct 26 '13 at 15:42
  • Are you sure you use exact above code on Android 2.3?Some properties like `android:previewImage="@drawable/ic_launcher"` or `android:resizeMode="horizontal|vertical"` are not available in Android 2.3. – hasanghaforian Oct 26 '13 at 17:53
0

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

Mick
  • 663
  • 1
  • 9
  • 18