I've created a widget for my application, but I require a initial loading layout before I show my main widget layout. I have made a load up layout as a initial and my main layout. I tried to use RemoteViews to show up my main widget layout after my stuff has finished loading at onUpdate method but it does not show up.
Is there any way to switch from the initial layout to the main layout?
Edit
Sorry for the late reply, hope I am showing the right blocks. What I did was to set the initial layout at the app widget properties and change to the actual layout that i am showing at the onUpdate method via a service in another class file.
Here are my codes:
The Widget info
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="#layout/initial_layout"
android:minHeight="146dp"
android:minWidth="294dp"
android:updatePeriodMillis="1800" >
</appwidget-provider>
This is what i typed for my onUpdate method:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
//start service
Intent myIntent = new Intent(context, WeatherNotificationService.class);
context.startService((myIntent));
}
Here is the code i typed within the service method.
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(WeatherNotificationService.this.getApplicationContext());
ComponentName thisWidget = new ComponentName(getApplicationContext(),SafetyWidget.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
new CommonOpearation().showToast(getApplicationContext(), appWidgetIds.length+"");
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetID = appWidgetIds[i];
DateFormat formatter = new SimpleDateFormat("EEE, dd/MM/yyyy");
formatter.setLenient(false);
Date a= new Date();
RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(),R.layout.widget_layout);
remoteViews.setTextViewText(R.id.todayDate,formatter.format(a));
remoteViews.setTextViewText(R.id.psiTime,currentNowCast.getCurrentPsiObj().getDatenTime());
remoteViews.setTextViewText(R.id.psiTV,currentNowCast.getCurrentPsiObj().getPsi());
remoteViews.setTextViewText(R.id.textViewTodayTemp,currentNowCast.getCurrentTemp().getTemp());
remoteViews.setTextViewText(R.id.textViewNearestRegion, nowCastWeather.getNwo().getRegion());
remoteViews.setTextViewText(R.id.hospTV, contact.getName());
appWidgetManager.updateAppWidget(appWidgetID, remoteViews);
RemoteViews function is to show up the appropriate layout when it is called at onUpdate? This is puzzling...
Related
I want my activity "but1" to update the text on my widget button through button in my but1 activity. I've tried and it shows no error but still the widget doesn't updates. Is it something Im missing in manifest file?Moreover the log cat also doesnt show any problem
following is my code in But1.class
et1.setOnClickListener(new OnClickListener() {
Intent in=new Intent();
Bundle extras=in.getExtras();
if(extras!=null)
{
awID=extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
}
aw=AppWidgetManager.getInstance(context);
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
RemoteViews rv=new RemoteViews(context.getPackageName(),R.layout.activity_main);
rv.setTextViewText(R.id.button1, "Mughazf,f,jvnl");
aw.updateAppWidget(awID, rv);
Intent result=new Intent();
result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
setResult(RESULT_OK,r
esult);
and my widget is:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, But1.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
views.setOnClickPendingIntent(R.id.button2, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
Problem should be with the Widget App ID . You need to keep remembering the Widget ID to update it later.
Your Widget Provider onUpdate() method should store the Widget id in shared preference or database
I am using shared preference here,
SharedPreferences prefs = context.getSharedPreferences("private preference", Context.MODE_PRIVATE);
// here i am saving one widget id. change your logic to store all the widget id's
prefs.edit().putInt("WidgetId",appWidgetId).commit();
Then when the Activity is launched and button clicked. You can update the widget layout like this
First Read the Widget ID from shared Preference
SharedPreferences prefs = getSharedPreferences("private preference", Context.MODE_PRIVATE);
int awID = prefs.getInt("WidgetId", AppWidgetManager.INVALID_APPWIDGET_ID);
Then, create the instance of RemoteView and set the new text to the TextView
RemoteViews rv=new RemoteViews(getPackageName(),R.layout.widget_layout);
rv.setTextViewText(R.id.textView1, "Some one updated Me");
Then , get the instance of AppWidget Manager
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
And, Finally update the Widget with new RemoteView Layout.
appWidgetManager.updateAppWidget(awID, rv);
So, awID - is the Widget Id what we got from onUpdate method.
This should work. Let me know if you see any issue.
Hello this is simple question
i create widget and test this is my simple code :
#Override
public void onReceive(Context context, Intent intent) {
ComponentName thisAppWidget = new ComponentName(context, MyWidget.class);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int ids[] = appWidgetManager.getAppWidgetIds(thisAppWidget);
Log.d(" widget ids : " + Arrays.toString(ids) );
setResultCode(0);
}
But i have got red square on home screen .. ?
My widget provider configuration :
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="80dp"
android:minHeight="80dp"
android:previewImage="#drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen|keyguard"
>
</appwidget-provider>
But i have got red square on home screen .. ?
This could be the preview image you have set in widget provider xml.
When the widget is added in home screen, the onUpdate() method will be called, where you can set the remote layout for the widget like this.
public class CustomAppWidgetProvider extends AppWidgetProvider{
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
You can also set a pending intent to an activity/service to be invoked when the whole widget or an child item in the widget is clicked
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
remoteViews.setOnClickPendingIntent(R.id.imageView1,pendingIntent);
So, here in the widget layout there is a textview , and when the user click on the textview the MainActivity will be launched.
You have to set an layout file to your appwidget-provider:
android:initialLayout="#layout/widget_view"
In this layout file you have to put Buttons, Images, TextViews etc. whatever you need.
I created a class that downloads some text from the internet and I want to take that text and update a TextView in my widget. I know that the event (OnDownloadCompleteListener) is getting triggered because I'm Logging it but I can't figure out how to update the TextView from within that event. I know it's a newbie mistake, just not sure what I'm missing.
public class Widget extends AppWidgetProvider{
InternetText internettext; //Handles downloading the text from the internet
RemoteViews views;
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
// 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
views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.tvWidgetVerse, pendingIntent);
internettext = new InternetText(context);
internettext.setOnDownloadCompleteListener(new OnDownloadCompleteListener() {
#Override
public void onEvent() {
TheText thetext = internettext.downloadedText(); //The text object
Log.i("", "Widget Text Downloaded " + thetext.getText()); //This fires so I know we've downloaded the text
TextStyling textStyle = new TextStyling();
//*****THIS IS WHERE I'M HAVING THE PROBLEM********
views.setTextViewText(R.id.tvWidgetText, Html.fromHtml(textStyle.boldWords(thetext.getText()))); //this never updates
}
});
internettext.getText();
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
Ok, I figured it out. I have to call appWidgetManager.updateAppWidget() from within the event. I knew it was a no-brainer...
appWidgetManager.updateAppWidget(appWidgetId, views);
which also means I have to make appWidgetManager and appWidgetId final
public void onUpdate(Context context, final AppWidgetManager appWidgetManager, int[] appWidgetIds) {
...
}
WHAT I NEED
I'm developing a custom Android launcher for a company, which will be installed on this company's tablets and therefore won't be published on any store.
It's basically a grid with widgets managed remotely (by a background service we're creating), which means that the app should decide which widgets to add or remove whenever the user opens it.
WHAT I HAVE
I'm using the following code to add the widgets:
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getContext());
AppWidgetHost appWidgetHost = new AppWidgetHost(getContext(), APPWIDGET_HOST_ID);
appWidgetHost.startListening();
List<AppWidgetProviderInfo> appWidgetInfos appWidgetInfos = appWidgetManager.getInstalledProviders();
for(int j = 0; j < appWidgetInfos.size(); j++)
{
if (appWidgetInfos.get(j).provider.getPackageName().equals(widgetPackage))
{
// Allocates an id
int appWidgetId = appWidgetHost.allocateAppWidgetId();
// Gets the app widget info
AppWidgetProviderInfo appWidgetProviderInfo = appWidgetInfos.get(j);
// Creates Widget
AppWidgetHostView hostView = appWidgetHost.createView(getContext(), appWidgetId, appWidgetProviderInfo);
hostView.setAppWidget(appWidgetId, appWidgetProviderInfo);
// Insers the view to the layout grid
insertView(row, column, rowspan, columnspan, hostView);
//
break;
}
}
And it works just fine. The widget shows and it's buttons respond to user touch.
Here's one of the the widgets onUpdate:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
// Perform this loop procedure for each App Widget that belongs to this provider
for (int appWidgetId : appWidgetIds)
{
// Create an Intent to launch ExampleActivity
Intent launchAppIntent = new Intent(context, MapActivity.class);
PendingIntent launchApp = PendingIntent.getActivity(context, 0, launchAppIntent, 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.widget_layout, launchApp);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
THE PROBLEM
One of the widgets (shown above) calls an Intent to start it's fullscreen Activity when clicked, and this doesn't work on my custom launcher, though it works perfectly on the default home launcher.
I read some example in internet/book about the App Widget, a normal example to update the widget is in onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) method of AppWidgetProvider like this:
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.mywidget_layout);
updateViews.setTextViewText(R.id.mytext, "updated text");
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
}
It update each Widgets in a loop.
But now, I have to implement an App Widget, it is updated in BroadcastReceiver, onReceive(Context context, Intent intent) method since there are no int[] appWidgetIds passed in. So I implemented the code like this:
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.mywidget_layout);
updateViews.setTextViewText(R.id.mytext, "updated text");
ComponentName myComponentName = new ComponentName(context, AndroidBatteryWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myComponentName, updateViews);
It didn't update widget one-by-one, but actually all widgets were updated at once. Even though it worked as I want, but I got confused as to why there is no need to update all widgets one-by-one as before.
What's the difference between two methods?
Can I send another broadcast from BroadcastReceiver.onReceive() to trigger AppWidgetProvider.onUpdate()? And how to?
It's the same thing. Update with ComponentName loops through all the ids like your first code block.
You can see it in the Android code here:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r1.2/com/android/server/AppWidgetService.java#AppWidgetService.updateAppWidgetProvider%28android.content.ComponentName%2Candroid.widget.RemoteViews%29