Well, firstly, there are a lot of duplicates on SO regarding this issue but the solutions posted there didn't work for me.
Basic info:
- Developing for SDK 19
- Using Android Studio for development
Added the following to AndroidManifest.xml
<receiver
android:name=".widgetProvider"
android:icon="@drawable/ic_launcher"
android:label="Home Automation">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_xml" />
</receiver>
Added the widgetProvider
class to src/main/java/com.xxx.yyy.zzz
package com.xxx.yyy.zzz;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
public class widgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
// implementation will follow
}
}
Added the widget_layout.xml
to res/layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="320dp"
android:layout_height="100dp"
android:background="@android:color/background_dark"
android:focusable="true">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="320dp"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="@+id/fan_widget"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="FAN"
android:textColor="@android:color/white"
android:textStyle="bold" />
<Button
android:id="@+id/light_widget"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="LIGHT"
android:textColor="@android:color/white"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
And finally, I added the widget_xml.xml
to the res/xml
folder.
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minHeight="72dp"
android:widgetCategory="home_screen"
android:minResizeHeight="10dp"
android:minResizeWidth="10dp"
android:minWidth="72dp"
android:resizeMode="none"
android:updatePeriodMillis="86400000" />
I haven't added any functionality yet because I just wanted to check out the layout, but for some reason, it doesn't show up in the widget list. My phone is running 4.4.4 and I am compiling for 4.4.2. Can't figure out what the issue is here. Please help.