How to programmatically disable onClick handler on Android AppWidget Button

允我心安 提交于 2019-12-05 09:17:26

When you create your RemoteViews instance, you supply a layout. When you want to disable the button, choose a layout with android:enabled="false" on that button.

Or, you could use setOnClickPendingIntent() and supply an Intent that just will not go anywhere (e.g., a broadcast Intent for an action that nobody uses).

Basically I had the same problem, but wanted to do it entirely programmatic, so we just pass the boolean value to the setBoolean() function:

remoteViews.setBoolean(R.id.imageButton1, "setEnabled", false);

Don't forget to tell Android to update the home screen:

    RemoteViews remoteViews = new RemoteViews(context.getResources().getText(R.string.package_name).toString(), R.layout.your_layout);
    ComponentName thisWidget = new ComponentName(context, Widget.class);
    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    manager.updateAppWidget(thisWidget, remoteViews);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!