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
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
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);
}
}
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
© 2022 - 2024 — McMap. All rights reserved.
findViewById
. RemoteViews work another way. You can't manipulate them directly. – Marisolmarissa