title: How to set different widget icon upon resize
body: <p>I have a widget icon for different screen sizes put in folders <strong>drawable-mdpi/drawable-xhdpi</strong> etc.
  <br></p>
  
  <p>Lets say device has mdpi-screen, so android will display my <code>1x1</code> widget using <code>drawable-mdpi/widget_pic.png [48x48px]</code> 
  <br></p>
  
  <p>And when I resize widget to <code>2x2</code> it will use the same icon but will put in in the center without scaling. <br>I want android to see that widget size became 2x larger and choose an icon from resources accordingly (i.e. <code>drawable-xhdpi/widget_pic.png[96x96px]</code>). 
  <br></p>
  
  <p><strong><em>So my question is how to do it?</em></strong> <br>I can think of a solution but it seems flawed: <br>
  Catch <code>onAppWidgetOptionsChanged()</code> event and there replace layout/image_src. <br>But I do not know where to get <code>2x</code> icon then.<br>
  <br><br>
  <strong>AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example"&gt;
      &lt;application&gt;
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name"
              android:theme="@style/AppTheme.NoActionBar"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;receiver android:name="com.example.MyWidget" android:label="@string/widget_to_home" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.appwidget.action.APPWIDGET_UPDATE"/&gt;
              &lt;/intent-filter&gt;
              &lt;meta-data
                  android:name="android.appwidget.provider"
                  android:resource="@xml/myWidgetXml"/&gt;
          &lt;/receiver&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>myWidgetXml.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
      android:initialKeyguardLayout="@layout/myLayout"
      android:initialLayout="@layout/myLayout"
      android:minHeight="40dp"
      android:minWidth="40dp"
      android:resizeMode="horizontal"
      android:updatePeriodMillis="86400000"
      android:widgetCategory="home_screen"&gt;
  &lt;/appwidget-provider&gt;
  </code></pre>
  
  <p><strong>myLayout.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:padding="@dimen/widget_margin"
      android:layout_margin="0dp"
      android:gravity="center"
      android:background="@android:drawable/alert_dark_frame"
      android:orientation="vertical"&gt;
  
      &lt;ImageButton
          android:id="@+id/WidgetButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@null"
          android:src="@drawable/widget_pic" /&gt;
      &lt;TextView
          android:id="@+id/WidgetTextView"
          android:textColor="@android:color/white"
          android:textSize="14sp"
          android:layout_marginTop="6dp"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:gravity="center" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>MyWidget.java</strong></p>
  
  <pre><code>public class MyWidget extends AppWidgetProvider {
  
      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
          for (int appWidgetId : appWidgetIds)
              updateAppWidget(context, appWidgetManager, appWidgetId);
      }
  
      private void updateAppWidget(Context context, AppWidgetManager _appWidgetManager, int WidgetId) {
          RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.myLayout);
          Intent intent = new Intent(context, MainActivity.class);
          Uri.Builder builder = new Uri.Builder();
          intent.setData(builder.build());
          intent.setAction(Intent.ACTION_VIEW);
          PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
          views.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent);
          views.setImageViewResource(R.id.WidgetButton, R.drawable.widget_pic);
          views.setTextViewText(R.id.WidgetTextView, "text");
          _appWidgetManager.updateAppWidget(WidgetId, views);
      }
  }
  </code></pre>