Perhaps a very silly question)) It is necessary to make the installation of the application also install a widget like VKontakte. How is this possible to do?

  • one
    What's the problem? Create a class inherited from AppWidgetProvider, add to the manifest the receiver and the metadata file in res / xml / - Ziens

1 answer 1

Create an XML file with the widget metadata in the res / xml folder with something like this

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/widget" android:minHeight="110dp" android:minWidth="180dp" android:updatePeriodMillis="1800000"> </appwidget-provider> 

android:initialLayout="@layout/widget" points to a file with the layout of the widget, for example:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="@string/text"/> </LinearLayout> 

Next, you need to specify our widget in the manifest:

 <receiver android:name="MyWidget" android:icon="@drawable/widget_icon" android:label="@string/widget_name"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"> </action> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_metadata"> </meta-data> </receiver> 

Well, the class itself:

 public class MyWidget extends AppWidgetProvider { static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { RemoteViews root = new RemoteViews(context.getPackageName(), R.layout.widget); appWidgetManager.updateAppWidget(appWidgetId, root); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { for (int appWidgetId : appWidgetIds) { updateAppWidget(context, appWidgetManager, appWidgetId); } } @Override public void onEnabled(Context context) { } @Override public void onDisabled(Context context) { } }