What Method I Must Use Instead Of Findviewbyid In App Widget?
Solution 1:
You don't need a findViewById() in Widget just do this
create a static variable, which will be your onClick name tag:
privatestaticfinalStringMyOnClick="Button OnClick";
Set this onClick tag to your view as below:
RemoteViewsviews=newRemoteViews(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
publicvoidonReceive(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;
publicclassWidgetextendsAppWidgetProvider {
privatestaticfinalStringSYNC_CLICKED="automaticWidgetSyncButtonClick";
@OverridepublicvoidonUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = newRemoteViews(context.getPackageName(), R.layout.widget_layout);
watchWidget = newComponentName(context, Widget.class);
remoteViews.setOnClickPendingIntent(R.id.sync_button, getPendingSelfIntent(context, SYNC_CLICKED));
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
@OverridepublicvoidonReceive(Context context, Intent intent) {
// TODO Auto-generated method stubsuper.onReceive(context, intent);
if (SYNC_CLICKED.equals(intent.getAction())) {
AppWidgetManagerappWidgetManager= AppWidgetManager.getInstance(context);
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = newRemoteViews(context.getPackageName(), R.layout.widget_layout);
watchWidget = newComponentName(context, Widget.class);
remoteViews.setTextViewText(R.id.sync_button, "TESTING");
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intentintent=newIntent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
}
Solution 2:
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,StringMethodName(onClick),StringValue);
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
Post a Comment for "What Method I Must Use Instead Of Findviewbyid In App Widget?"