ApmeM
ApmeM

Reputation: 147

Android WidgetProvider duplicating

I have made a simple widget with stretchable layout, and want to allow users to put widgets with different sizes using the same layout and the same widget provider class.

I have found, that I need to create 4 widget_provider_x_y.xml files (x and y are values 2,2 2,3 3,2 3,3) that is very similar except width and height.

Also I have found, that I need to create multiple copies of WidgetProvider classes that is exactly the same (except names). If I try to use single WidgetProvider class - I will see only one widget in widgets list.

I have a question: - why do I need to copy java class? It is the same, because layout is the same, and I do not want to have 4 more files in projects that I need to update simultaneously.

Here is a part of my androidManifest.xml:

    <receiver android:name=".SimpleNoteWidgetProvider_3_2" android:label="@string/app_widget_3_2">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            <action android:name="ACTION_WIDGET_UPDATE_FROM_ACTIVITY"/>
            <action android:name="ACTION_WIDGET_UPDATE_FROM_WIDGET"/>
        </intent-filter>
        <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider_3_2"/>
    </receiver>
    <receiver android:name=".SimpleNoteWidgetProvider_3_3" android:label="@string/app_widget_3_3">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            <action android:name="ACTION_WIDGET_UPDATE_FROM_ACTIVITY"/>
            <action android:name="ACTION_WIDGET_UPDATE_FROM_WIDGET"/>
        </intent-filter>
        <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider_3_3"/>
    </receiver>

Upvotes: 1

Views: 284

Answers (1)

You don't have to copy your WidgetProvider class n times to get n widget sizes. Just inherit from the main WidgetProvider class like this:

public class SimpleNoteWidgetProvider_3_2 extends SimpleNoteWidgetProvider {

}

and

public class SimpleNoteWidgetProvider_3_3 extends SimpleNoteWidgetProvider {

}

Upvotes: 1

Related Questions