Artem Zinnatullin
Artem Zinnatullin

Reputation: 4447

Widget layout change after widget resize

My widget supports widget resizing.

But when the widget size is changed, the text size remains the same. What should I do to handle it?

I looking for something like percentage of the text size which would depend on the widget size.

Upvotes: 7

Views: 3611

Answers (2)

MinceMan
MinceMan

Reputation: 7592

To add to Artem's answer, you can get your new options out of the bundle by doing the following.

public class YourWidgetProvider extends AppWidgetProvider {

    @Override
    public void onAppWidgetOptionsChanged (Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
        // This is how you get your changes.   
        int minWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)
        int maxWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH)
        int minHeight = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
        int maxHeight = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)
    }
}

Then unfortunatly things are never that easy on Android. TouchWiz (Samsung's galaxy line) does not make this call, but we can get their broadcast. So in your AppWidgetProvider class do the following:

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    // Handle TouchWiz
    if(intent.getAction().contentEquals("com.sec.android.widgetapp.APPWIDGET_RESIZE")) {
        handleTouchWiz(context, intent);
    }
}

private void handleTouchWiz(Context context, Intent intent) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

    int appWidgetId = intent.getIntExtra("widgetId", 0);
    int widgetSpanX = intent.getIntExtra("widgetspanx", 0);
    int widgetSpanY = intent.getIntExtra("widgetspany", 0);
    
    if(appWidgetId > 0 && widgetSpanX > 0 && widgetSpanY > 0) {
        Bundle newOptions = new Bundle();
        // We have to convert these numbers for future use
        newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, widgetSpanY * 74);
        newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, widgetSpanX * 74);
        
        onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
    }
}

You may need to change how you handle that bundle if you are using MAX_HEIGHT or MAX_WIDTH which I am not.

Upvotes: 6

Artem Zinnatullin
Artem Zinnatullin

Reputation: 4447

I got it, as documentation says: You need to override onAppWidgetOptionsChanged in your AppWidgetProvider, so you will recieve Broadcast when widget resized.

public class YourWidgetProvider extends AppWidgetProvider {

    ...

    @Override
    public void onAppWidgetOptionsChanged (Context context, 
                                      AppWidgetManager appWidgetManager,
                                      int appWidgetId, Bundle newOptions) {
        // Here you can update your widget view
    }

    ...

}

Upvotes: 4

Related Questions