I've read several questions about this on stackoverflow, and none of the answers are solving my problem.
I'm trying to add a home screen widget to my app, and my widget does not appear in Android's widget list.
I've tried rebooting the device, reinstalling the application, changing parameters in the configuration file, ensuring app is not installed on SD card. Nothing is working.
Here is the code I currently have:
Inside AndroidManifest.xml's application tag:
<receiver
android:name=".GulpWidgetProvider"
android:icon="@mipmap/ic_launcher"
android:label="Gulp">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/gulp_widget_info" />
</receiver>
Inside /res/xml/gulp_widget_info.xml:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/layout_widget"
android:minHeight="150dip"
android:minWidth="400dip"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen"
android:resizeMode="none"
android:minResizeHeight="150dip"/>
Inside /res/layout/layout_widget.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dip"
android:background="@drawable/gradient"
android:gravity="center_horizontal|top">
<RelativeLayout
android:layout_width="170dip"
android:layout_height="150dip"
android:gravity="center">
<ProgressBar
android:id="@+id/widget_consumption_progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="150dip"
android:layout_height="150dip"
android:layout_centerInParent="true"
android:indeterminate="false"
android:max="100"
android:progress="0"
android:progressDrawable="@drawable/progressbar"
android:secondaryProgress="100" />
<ProgressBar
android:id="@+id/widget_time_progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="150dip"
android:layout_height="150dip"
android:layout_centerInParent="true"
android:indeterminate="false"
android:max="100"
android:progress="0"
android:progressDrawable="@drawable/progressbar2"
android:secondaryProgress="100" />
<TextView
android:id="@+id/widget_water_consumed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:maxLines="1"
android:textColor="@color/white"
android:textSize="30sp" />
<TextView
android:id="@+id/widget_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/water_consumed"
android:layout_centerInParent="true"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:maxLines="1"
android:text="@string/milliliters_abbrev"
android:textColor="@color/white"
android:textSize="12sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
android:paddingLeft="10dip"
android:paddingRight="10dip">
<TextView
android:id="@+id/widget_add"
style="@style/GulpButton"
android:layout_width="60dip"
android:layout_height="60dip"
android:background="@drawable/whitecirclebutton"
android:gravity="center"
android:text="@string/add_abbrev" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/widget_goal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:textColor="@color/white"
android:textSize="22sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:text="@string/target"
android:textColor="@color/white_semi_translucent"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/widget_remaining"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:textColor="@color/white"
android:textSize="22sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:text="@string/remaining"
android:textColor="@color/white_semi_translucent"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Inside /java/info/andrewdahm/gulp/GulpWidgetProvider.java:
public class GulpWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// 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, Main.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.layout_widget);
views.setOnClickPendingIntent(R.id.widget_add, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
Any ideas why my widget isn't appearing on the list of widgets?
It is an issue about the values you set in gulp_widget_info.xml
for android:minHeight
and android:minWidth
.
Try to set a smaller values and the widget should appear in the launcher's widget list:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/layout_widget"
android:minHeight="40dip"
android:minWidth="40dip"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen"
android:resizeMode="none" />
I tested this settings in my phone with Google Now Launcher and it appears with size 1x1
The rule to calculate the size is 70 × n − 30
where n is the number of cells you want. You can find more information about how to determine the size of the widget here.
ACcording to this link the Activity must be declared in the AppWidgetProviderInfo XML file, with the android:configure attribute and you are missing this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With