My TextView Widget is not updating on the home screen.
The new value of the text does not appear on the widget but the old value still remains as it was. This is my code for the widget class and activity that calls the widget update:
public class MotivappWidget extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Intent intent = new Intent(context, ViewAll.class);
PendingIntent pending = PendingIntent
.getActivity(context, 0, intent, 0);
RemoteViews updateViews = new RemoteViews(context.getPackageName(),
R.layout.widget);
// updateViews.setTextViewText(R.id.widget_text, input);
updateViews.setOnClickPendingIntent(R.id.widget_text, pending);
appWidgetManager.updateAppWidget(appWidgetIds, updateViews);
}
public class ViewAll extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_view);
}
protected void onResume() {
super.onResume();
updateWidgetText();
}
private void updateWidgetText() {
LayoutInflater inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi_widgy = inflater.inflate(R.layout.widget, null); //there is a layout xml file called widget.xml already made
TextView tv_widgy = (TextView) vi_widgy.findViewById(R.id.widget_text); //in widget.xml, there is a text view called widget_text
tv_widgy.setText("Text has just changed."); //this text never appears on the widget home even after updating
updateAllWidgets();
}
private void updateAllWidgets(){
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(this, MotivappWidget.class));
if (appWidgetIds.length > 0) {
new MotivappWidget().onUpdate(this, appWidgetManager, appWidgetIds);
}
}
}
Related
I am trying to create a simple widget. I wanted that, when touchet, to show a toast. But it does not show anything. I don't know where I'm wrong, it looks quite well(maybe i have forgotten something i didn't notice).
Code:
static String clAc = "CLICKED";
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
Intent intent = new Intent(context, LocationWidget.class);
intent.setAction(clAc);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent,0);
Paper.init(context);
String title = Paper.book().read("content");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.location_widget);
views.setTextViewText(R.id.appwidget_text, title);
views.setOnClickPendingIntent(R.id.layout_wrapper, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
#Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
#Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if(intent.getAction().contentEquals(clAc))
Toast.makeText(context, "clicked",Toast.LENGTH_LONG).show();
}
Try to update not each appWidgetId, but all appWidgetIds with your logic.
I'm a newbie and i'm finding difficulty in getting texts from a string array. I tried creating a widget in my app and i wanted the widget to update it's text view from the string array i declared in the resources folder, but this does not seams to work out. Here is my code.
MainActivity.Java
public class MainActivity extends AppCompatActivity {
public static TextView Quote;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Quote = (TextView) findViewById(R.id.appwidget_text);
final String[] Quotes = getResources().getStringArray(R.array.Qoutes);
Random rand = new Random();
int randNum = rand.nextInt(Quotes.length);
Quote.setText(Quotes[randNum]);
}}
NewAppWidget.java
public class NewAppWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = MainActivity.Quote.getText().toString();
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
views.setTextViewText(R.id.appwidget_text, widgetText);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0 , intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
remoteViews.setOnClickPendingIntent(R.id.interfaceID,pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
#Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
#Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}}
Thank You
I want to create an app widget with a confguration activity, the problem is when I add the widget to home the configuration activity is shown but when I click on button config the main activity does not start.... when I click on the Widget nothing happens. There are no errors in my Main activity. because if I start the app, it works without any errors...
I don't know what I m doing wrong :(. Thank you very much for your help...
here is the code:
public class WidgetConfiguration extends Activity{
final Context context = this;
EditText url;
EditText password;
int mAppWidgetId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(RESULT_CANCELED);
initializeAppWidget();
showConfigDlg();
final Context context = WidgetConfiguration.this;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
}
private void initializeAppWidget(){
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
}
private void showConfigDlg(){
setContentView(R.layout.widget_config);
((Button) findViewById(R.id.saveBtn)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Log.d("blablabla", "setting the onClick1");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
});
}
}
public class WidgetProvider extends AppWidgetProvider
{
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
Log.d("blablabla", "setting the onClick2");
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Log.d("blablabla", "setting the onClick3");
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
I am using asynctask to do my feature function and getting the values there. But my text view is not getting updated, though my log statements giving me the result.
I am not using custom intent here, just the basic one.
Here are my code snippets:
public class ListViewWidget extends AppWidgetProvider{
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
updateViews = new RemoteViews(context.getPackageName(), R.layout.list_layout);
thisWidget = new ComponentName(context, ListViewWidget.class);
FetchTask fetchTask=new FetchTask();
AppWidgetManager.getInstance(context.getApplicationContext());
fetchTask.execute();
}
//FetchTask
public static class FetchTask extends AsyncTask<URL,Integer,String> implements ServerRequestEnvironment{
protected String doInBackground(URL... arg0) {
//logic part and stuff not entered
//This is the end part that returns me the result, which is not getting printed to text view.
String name="";
int i = new Random().nextInt(27);
storeObject=store.getStores().getItems().get(i).getStore();
name= storeObject.getName();
resultStuff(name);
Log.i("StoreTag","storeval:"+name); //returns name of the 0th item
return name;
}//end of doInBackground() method
protected void onPostExecute(String name) {
// TODO Auto-generated method stub
Intent intent=new Intent("android.appwidget.action.APPWIDGET_UPDATE");
PendingIntent pendingIntent=PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
updateViews.setTextViewText(R.id.text_view,resultStuff(name));
updateViews.setOnClickPendingIntent(R.id.next, pendingIntent);
AppWidgetManager manager = AppWidgetManager.getInstance(context.getApplicationContext());
manager.updateAppWidget(thisWidget, updateViews);
}//end of PostExecute
}//End of async task class
}//end of ListViewWidget class
What I am missing here? Please please guide..
UPDATE: Is this fine? Async task to be called both in onReceive and onUpdate??
#Override
public void onReceive(Context context,Intent intent){
if(intent.getAction().equals("android.appwidget.action.APPWIDGET_UPDATE")){
fetchTask.execute();
}
}
Update 2:
public static String name="New text"
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
updateViews = new RemoteViews(context.getPackageName(), R.layout.list_layout);
//-----------NEW LINE ADDED-------------
updateViews.setTextViewText(R.id.text_view,name);
thisWidget = new ComponentName(context, ListViewWidget.class);
//Now in onPostExecute()
//AppWidgetManager manager = AppWidgetManager.getInstance(context.getApplicationContext());
//manager.updateAppWidget(thisWidget, updateViews);
fetchTask.execute();
}
protected void onPostExecute(String name) {
// TODO Auto-generated method stub
Intent intent=new Intent("android.appwidget.action.APPWIDGET_UPDATE");
PendingIntent pendingIntent=PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
updateViews.setTextViewText(R.id.text_view,name);
updateViews.setOnClickPendingIntent(R.id.next, pendingIntent);
AppWidgetManager manager = AppWidgetManager.getInstance(context.getApplicationContext());
manager.updateAppWidget(thisWidget, updateViews);
}
#Dororo Didn't get you. Please explain. I have created public var in my class:
public static Context context;
public static RemoteViews updateViews;
public static ComponentName thisWidget;
FetchTask fetchTask=new FetchTask();
Have you tried using this:
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(toThis);
Are you sure you're not overwriting the updated value when you create new ComponentName(context, ListViewWidget.class);? onUpdate will be called when you call manager.updateAppWidget(thisWidget, updateViews); You shouldn't need to use static, you just need an initialisation boolean which is set to true after you've created the objects for the first time.
Solved my exception and error. Was setting context to be null. I assigned context with the application context.
My app has a widget that lights the LED Flash when we click on it and swhitch it off when we click again on it.
Here is the code (thanks to Kartik from this post):
WidgetProvider.java
public class WidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Intent receiver = new Intent(context, WidgetReceiver.class);
receiver.setAction("COM_FLASHLIGHT");
receiver.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
receiver, 0);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget);
views.setOnClickPendingIntent(R.id.imageButtonWidget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, views);
}
}
WidgetReceiver.java
public class WidgetReceiver extends BroadcastReceiver {
public static boolean isLightOn = false;
public static Camera camera;
#Override
public void onReceive(Context context, Intent intent) {
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget);
if (isLightOn) {
views.setImageViewResource(R.id.imageButtonWidget,
R.drawable.widget_lamp_button_default);
} else {
views.setImageViewResource(R.id.imageButtonWidget,
R.drawable.widget_lamp_button_checked);
}
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
appWidgetManager.updateAppWidget(new ComponentName(context,
WidgetProvider.class), views);
if (isLightOn) {
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
isLightOn = false;
} else {
// Open the default i.e. the first rear facing camera.
camera = Camera.open();
if (camera == null) {
Toast.makeText(context, "R.string.no_camera",
Toast.LENGTH_SHORT).show();
} else {
// Set the torch flash mode
Parameters param = camera.getParameters();
param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
try {
camera.setParameters(param);
camera.startPreview();
isLightOn = true;
} catch (Exception e) {
Toast.makeText(context, "R.string.no_flash",
Toast.LENGTH_SHORT).show();
}
}
}
}
}
Now I would like to switch the widget off from inside my app . With this code in the main activity of my app, I can release the camera taken by the widget :
MainActivity.java
//Stop widget camera
if (WidgetReceiver.isLightOn){
Camera a = WidgetReceiver.camera;
a.stopPreview();
a.release();
a = null;
WidgetReceiver.isLightOn=false;}
But the problem is that the widget is still set to the checked drawable (R.drawable.widget_lamp_button_checked). So the FlashLight is well turned off but I still need to force the widget to set its drawable to the unchecked one (R.drawable.widget_lamp_button_default).
How can I do this ?
Edit : Problem Solved
WidgetProvider.java
public class WidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// Set widget's drawable to unchecked
RemoteViews views2 = new RemoteViews(context.getPackageName(),
R.layout.widget);
AppWidgetManager mManager = AppWidgetManager.getInstance(MainActivity
.getContext());
ComponentName cn = new ComponentName(MainActivity.getContext(),
WidgetProvider.class);
views2.setImageViewResource(R.id.imageButtonWidget,
R.drawable.widget_lamp_button_default);
mManager.updateAppWidget(cn, views2);
// Widget OnClick Behavior
Intent receiver = new Intent(context, WidgetReceiver.class);
receiver.setAction("COM_FLASHLIGHT");
receiver.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
receiver, 0);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget);
views.setOnClickPendingIntent(R.id.imageButtonWidget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, views);
}
}
WidgetReceiver.java -> kept the same
MainActivity.java
public class MainActivity extends Activity {
private static Context mContext;
public static Context getContext() {
return mContext;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
//Stop widget camera
if (WidgetReceiver.isLightOn){
Camera a = WidgetReceiver.camera;
a.stopPreview();
a.release();
a = null;
WidgetReceiver.isLightOn=false;}
// Fire Widget's update with Intent
Intent intent = new Intent(this, WidgetProvider.class);
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
// Use an array and EXTRA_APPWIDGET_IDS instead of
// AppWidgetManager.EXTRA_APPWIDGET_ID,
// since it seems the onUpdate() is only fired on that:
int[] ids = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);
}
Send a broadcast to your AppWidgetProvider and update your widget by using updateAppWidget method of AppWidgetManager class,in onReceive() method of AppWidgetProvider:
#Override
public void onReceive(Context context, Intent intent) {
...
views = new RemoteViews(context.getPackageName(),
R.layout.yourwidgetlayout);
AppWidgetManager mManager = AppWidgetManager.getInstance(App
.getContext());
ComponentName cn = new ComponentName(App.getContext(),
YourAppWidgetProvider.class);
//change your views,here I change text of text view witch it's id is "widgettextview"
views.setTextViewText(R.id.widgettextview, "lastWord");
mManager.updateAppWidget(cn, views);
...
}
Here is App definition:
public class App extends Application implements OnInitListener {
private static Context mContext;
public void onCreate() {
super.onCreate();
mContext = this;
}
public static Context getContext() {
return mContext;
}
}