Android How can i change the background on a button click event? - android

I would like to change the background of a widget button on a click event and then return to the original background, something like ACTION_DOWN and ACTION_UP.
I know that i can change the background with:
RemoteViews.setInt(viewId, methodName, value);
and add an action to a click event:
Intent intent = new Intent(context, MyWidget.class);
intent.setAction("Restart");
PendingIntent pIntent = PendingIntent.getBroasdcast(context, 0, intent, 0);
RemoteViews.setOnClickPendingIntent(viewId, pIntent);

Have a look at StateList xml files. The selectors that these files use will handle changing your View's background in all of its various states, and you don't have to mess with writing your own touchlistener.
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Related

handling multiple buttons on a custom notification bar

I have a custom notification bar in my application that has 2 buttons. I found a way to make the buttons work by using contentView.setOnClickPendingIntent in my notification class and then handle the functionality in separate activity but I have a problem. Here is the code in Notification class.
//Handle the button for showing bookmarks on custom notification
Intent buttonsIntent2 = new Intent(context, NotificationBarButtonActivityHandler.class);
buttonsIntent2.putExtra(PENDING_ACTION, REGISTER_BOOKMARK);
contentView.setOnClickPendingIntent(R.id.notificationBarShowBookmarksButton, PendingIntent.getActivity(context, 0, buttonsIntent2, 0));
//Handle the button for adding bookmark on custom notification
Intent buttonsIntent = new Intent(context, NotificationBarButtonActivityHandler.class);
buttonsIntent.putExtra(PENDING_ACTION, SHOW_BOOKMARKS);
contentView.setOnClickPendingIntent(R.id.notificationBarAddBookmarkFromChromeButton, PendingIntent.getActivity(context, 0, buttonsIntent, 0));
The problem is that no matter which button is pressed, the button handler activity always receives REGISTER_BOOKMARK when I am pressing both button.
String action = (String) getIntent().getExtras().get(NotificationBarService.PENDING_ACTION);
Could someone please explain to me why is this happening? Also, is this the optimal way to handle button presses in the notification bar?

How to start a service from App Widget in Android

I am making a home screen App Widget for my Android app. In that App Widget there is a ListView. That ListView has a custom layout which has a TextView & an ImageButton. I am setting a PendingIntent on that ImageButton to start the service as follows:
Intent intent = new Intent(mContext, MyService.class);
intent.putExtra(MyConstants.MY_EXTRA, c.getString(c.getColumnIndex(MyColumns.MY_COLUMN_TITLE)));
intent.setAction(MyConstants.MY_ACTION);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
rv.setOnClickPendingIntent(R.id.MyImageButton, pendingIntent);
in getViewAt method of RemoteViewsFactory. But on clicking the ImageButton the service does not start.
I have put an intentfilter with MY_ACTION for MyService. And also tried without it which did not make things work.
Anyone knows how to start service on click of a button inside a list view item in home screen App Widget in Android?
Everything works fine i.e. the ListView is populated perfectly from SQLite database.
From the android javadoc
public void setOnClickPendingIntent
Equivalent to calling
setOnClickListener(android.view.View.OnClickListener) to launch the
provided PendingIntent. When setting the on-click action of items
within collections (eg. ListView, StackView etc.), this method will
not work. Instead, use {#link
RemoteViews#setPendingIntentTemplate(int, PendingIntent) in
conjunction with RemoteViews#setOnClickFillInIntent(int, Intent).
In your update method of your AppWidgetProvider you have to add
final PendingIntent clickPendingIntentTemplate = PendingIntent.getService...
views.setPendingIntentTemplate(R.id.widget_list, clickPendingIntentTemplate);
and in the getViewAt method of your RemoteViewsFactory :
final Intent fillInIntent = new Intent();
fillInIntent.putExtra(...);
views.setOnClickFillInIntent(R.id.widget_list_item, fillInIntent);

Get touch location for appwidget

When a user clicks on my appwidget, the widget's configuration activity is opened up. So far that has suited my needs.
Now I'd like to add the ability to perform a special action (e.g. a widget refresh) when the user touches a particular feature or area on the widget, rather than open up the configuration activity.
Is there any way to do that? Even if it's just a case of detecting whether the touch was in the left half of the widget, for example, that would be useful.
Intent myIntent = new Intent(...);
...
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, myIntent, 0);
remoteViewsFive.setOnClickPendingIntent(R.id.imgRefresh, pendingIntent);
Like this, imgRefresh is your ImageView with refresh icon (for example). You should define an Intent then create a PendingIntent that set on view
A easy solution to do what you want it's use a transparent png in imgRefresh

Launching an activity from an AppWidget

How do i launch an activity from an AppWidget? I want to give the user the ability to edit its settings.
All the AppWidget methods have a Context: you can use that to create an Intent to launch your Activity.
EDIT: strictly speaking, you don't even need the Context (just create an Intent and then call startActivity).
you can find a lot of examples around the internet this example is excellent courtesy of Mark Murphy
https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget
You have to do something like this:
Intent intent = new Intent(context, MainWidgetActivity.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 rv = new RemoteViews(context.getPackageName(), R.layout.appwidget);
rv.setOnClickPendingIntent(R.id.button, pendingIntent);
while MainWidgetActivity.class is the Activity you want to launch

appWidget layout reseting after device restart

I have a simple appWidget that displays an image. The image is selected in the configuration activity and the widget is updated via the remove view. This is done on a button push executing the code below:
Intent intent = new Intent(context, KidDialog.class);
intent.setData(selectedImage);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
widgetView.setOnClickPendingIntent(R.id.centerBrd, pendingIntent);
widgetView.setImageViewUri(R.id.widImg, selectedImage);
appWidgetManager.updateAppWidget(appWidgetId, widgetView);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
setResult(RESULT_OK, resultValue);
finish();`
As you can see, it is also seeing an PendingIntent for the onClickListener of the image. I don't think that is relevant here. Anyway, this work great. The widget does exactly what I want it to. Until I restart the device.
When I restart the widget loads with the default view from the xml in the apk. It does not keep the image update from the initial configuration. My questions is how do I get the widget to load back up after a restart with the updated view set during the configuration activity? I will also need to reset the onClick Pendingintent, but I will save that for later. I expect they are related anyway. So I am out of ideas.
I am not sure of the best answer, but when you receive the intent from the button push you could save the image name to a SharedPreference.
And when you create the widget, check the value of the sharedpreference and use that to restore the desired image..

Categories

Resources