Cannot disable android.widget.Button

I have a button inside an Android widget declared as follows:

<Button android:id="@+id/newWorkBtnWidget" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/ts_on_repair"/> 

Every time I try to enable or disable this button through RemoteViews, I get the error message android.widget.RemoteViews$ActionException: view: android.widget.Button can't use method with RemoteViews: setEnabled(boolean)

My code is:

 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); remoteViews.setInt(R.id.newWorkBtnWidget, "setBackgroundResource", R.drawable.green_button); remoteViews.setBoolean(R.id.newWorkBtnWidget, "setEnabled", false); 

How to fix it?

+5
source share
2 answers

I had the same problem lately, and I found that remoteView.setBoolean () with the method name setEnabled does not work either.

using

 remoteViews.setBoolean(R.id.textView_life_id, "setEnabled", true); 

will get an error like this

 W/AppWidgetHostView: updateAppWidget couldn't find any view, using error view android.widget.RemoteViews$ActionException: view: android.widget.TextView can't use method with RemoteViews: setEnabled(boolean) at android.widget.RemoteViews$ReflectionAction.apply(RemoteViews.java:1134) at android.widget.RemoteViews.performApply(RemoteViews.java:2304) at android.widget.RemoteViews.apply(RemoteViews.java:2263) at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:402) at com.android.launcher2.LauncherAppWidgetHostView.updateAppWidget(LauncherAppWidgetHostView.java:54) at android.appwidget.AppWidgetHost.updateAppWidgetView(AppWidgetHost.java:434) at android.appwidget.AppWidgetHost$UpdateHandler.handleMessage(AppWidgetHost.java:102) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:138) at android.app.ActivityThread.main(ActivityThread.java:5089) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) 

Finally, I found the answer that only the method in the SDK with @ android.view.RemotableViewMethod can be used in remoteview.

like

 @android.view.RemotableViewMethod public final void setText(CharSequence text) { setText(text, mBufferType); } 

then you can use views.setCharSequence (R.id.textView, "setText", "Test");

+1
source

You cannot use enabled / disabled in the traditional sense, but you can either update the Button to disable the appearance, or click the "false" button and disable them with GONE / VISIBLE.

0
source

Source: https://habr.com/ru/post/1213876/


All Articles