what method I must use Instead of findViewById in app widget?
Asked Answered
V

2

6

When I use findViewById() I get an error.
What method should I use, instead of findViewById() in my app widget?

P.S.: I want to do something like:

 Button a = (Button) findViewById(R.id.button);

in my screen widget

Valerivaleria answered 16/6, 2017 at 11:25 Comment(2)
there is no such method, which is analog to findViewById. RemoteViews work another way. You can't manipulate them directly.Marisolmarissa
Can you please describe what kind of error you are getting, so it will be convenient to give solution.Pironi
S
6

You don't need a findViewById() in Widget just do this

create a static variable, which will be your onClick name tag:

private static final String MyOnClick = "Button OnClick";

Set this onClick tag to your view as below:

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
views.setTextViewText(R.id.your_button, "Button Name");
views.setOnClickPendingIntent(R.id.your_button, MyOnClick);

get Button Click listener like bellow

public void onReceive(Context context, Intent intent) {

if (MyOnClick.equals(intent.getAction())){
    //your onClick action is here
  }
};

Here total code

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class Widget extends AppWidgetProvider {
    private static final String SYNC_CLICKED    = "automaticWidgetSyncButtonClick";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        RemoteViews remoteViews;
        ComponentName watchWidget;

        remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        watchWidget = new ComponentName(context, Widget.class);

        remoteViews.setOnClickPendingIntent(R.id.sync_button, getPendingSelfIntent(context, SYNC_CLICKED));
        appWidgetManager.updateAppWidget(watchWidget, remoteViews);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        super.onReceive(context, intent);

        if (SYNC_CLICKED.equals(intent.getAction())) {

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

            RemoteViews remoteViews;
            ComponentName watchWidget;

            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            watchWidget = new ComponentName(context, Widget.class);

            remoteViews.setTextViewText(R.id.sync_button, "TESTING");

            appWidgetManager.updateAppWidget(watchWidget, remoteViews);

        }
    }

    protected PendingIntent getPendingSelfIntent(Context context, String action) {
        Intent intent = new Intent(context, getClass());
        intent.setAction(action);
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }
}
Sensuality answered 16/6, 2017 at 11:37 Comment(9)
This is to be In OnCreate?Valerivaleria
And what is this loop: for (int appWidgetId : appWidgetIds) { updateAppWidget(context, appWidgetManager, appWidgetId); }Valerivaleria
Check this linkSensuality
Error:(96, 2) error: reached end of file while parsingValerivaleria
and Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details.Valerivaleria
can you pass your xml files and activitysSensuality
points to the block, which belong to protected PendingIntent getPendingSelfIntent(Context context, String action)Valerivaleria
:app:compileDebugJavaWithJavac FAILEDValerivaleria
please check Widget example. Mabe it will help youSensuality
D
-1

appWidgetProvider class does not allow findViewById(). You need to use a function of RemoteView Class for this purpose.

CharSequence Method | Android Developers

in your case:

remoteViews.setCharSequence(R.id.button,String MethodName(onClick),String Value);

it works for EditText,TextView,TextClock. You can replace String MethodName with the name of method like getText,setText,setTimeZone etc. For Buttons RemoteViews offer the following function

setOnClickResponse | Android Developers

this is the equivalent of

View.setOnClickListener(android.view.View.OnClickListener)

Similarly a lot of other methods are available now such as setFloat(), setLong() which work same as setCharSequence().

you can choose between them based on the datatype of parameters you want to pass.

here is the link to understand and view the list of all RemoteView functions

RemoteViews | Android Developers

Drinkwater answered 1/12, 2020 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.