2

I have an app and have decided to add a widget to it, I've followed all the official documentation for it, but when I install the app on my Galaxy Nexus, the widget doesn't appear in the app/widget list.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dysign.livetubecountdown"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.dysign.livetubecountdown.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="com.dysign.livetubecountdown.Widget" android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <intent-filter>
            <action android:name="android.appwidget,action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" />
    </receiver>

</application>

widget_provider.xml

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

widget_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/widgetTextView"/>
</FrameLayout>

Widget.java

package com.dysign.livetubecountdown;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.widget.RemoteViews;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

public class Widget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerClass(context, appWidgetManager), 1, 1000);
    }

    private class TimerClass extends TimerTask {
        RemoteViews remoteViews;
        AppWidgetManager appWidgetManager;
        ComponentName thisWidget;
        DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());

        public TimerClass(Context context, AppWidgetManager appWidgetManager) {
            this.appWidgetManager = appWidgetManager;
            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_main);
            thisWidget = new ComponentName(context, Widget.class);
        }

        @Override
        public void run() {
            remoteViews.setTextViewText(R.id.widgetTextView, "TIME = " +format.format(new Date()));
            appWidgetManager.updateAppWidget(thisWidget, remoteViews);
        }
    }
}

I've been looking all over the web to try to fix this, but every suggestion I try just doesn't work. Also there are no errors in LogCat or any mention of anything widget related.

Any help is much appreciated, thanks!

Tom O
  • 1,780
  • 2
  • 20
  • 43
  • Up one for inspiration ))). Turned out my cause for this issue (not listed in available widgets) was also application manifest... ...sepcifically, my receiver section was outside of my application tags ))). – tom Mar 11 '16 at 19:08

2 Answers2

5

The problem turned out to be a typo in the manifest.

<action android:name="android.appwidget,action.APPWIDGET_UPDATE" />

Should of been:

<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />

Notice the comma after android.appwidget.

Tom O
  • 1,780
  • 2
  • 20
  • 43
  • Ouch. I too lost half a day to troubleshooting because I typed "android.appwidget.provder" instead of "android.appwidget.provider". I just wish there was some log message which told you why android failed to process an element in the manifest file. – Andrew Shepherd Feb 26 '14 at 02:42
2

I have experienced the same problem a few times.

The thing that helped me was rebooting my phone, after that the widget started appearing in the widget list. Why is this happening i dont know i have googled and found out that many have similar problems, that it even happens that users install the app from the Market and the widget doesnt appear in the list.

Some solutions that were offered by others: re-installing the app(didn work for me), starting its main activity a few times(also nothing), rebooting phone(this one worked for me, since it even happened during development that the widget works fine for days and the suddenly it was missing in the list when re-installing from eclipse??? Rebooting the phone helped me.

Hope it helps!

JanBo
  • 2,925
  • 3
  • 23
  • 32
  • Rebooted it, reinstalled countless times, both dev versions and release versions of the APK, still nothing :( – Tom O Aug 14 '13 at 20:26
  • Hm..have you tried on a different device or emulator? Also try downloading some projects as examples and run to see if they work. – JanBo Aug 14 '13 at 20:32
  • Maybe try this one, download and import and see if it works: http://forum.xda-developers.com/showthread.php?t=1732939 ...i suppose you already tried different tutorials besides the official documentation? ( the official documentation didnt work on my phone i had to use alternative ways to get widgets running...) – JanBo Aug 14 '13 at 20:36
  • That is the one I originally followed in conjunction with the official documentation. Also it doesn't work in a emulator either. – Tom O Aug 15 '13 at 09:19
  • The only thing i would suggest is to try connecting a different device and see if the same code runs there...also i suppose widgets from different apps work on your phone? – JanBo Aug 15 '13 at 09:32
  • Widget isn't appearing on my Motorola Xoom too, other widgets from apps in the Play Store work perfectly fine. Gonna try a different project now. – Tom O Aug 15 '13 at 09:36
  • It turned out to be a comma in the manifest instead of a dot. – Tom O Aug 16 '13 at 17:00