Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 976
Artem Zinnatullin Avatar asked Feb 17 '23 15:02

Artem Zinnatullin


1 Answers

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.

like image 190
MinceMan Avatar answered Feb 28 '23 12:02

MinceMan