Clickable widgets in android
First, add a static variable with a constant.
public static String YOUR_AWESOME_ACTION = "YourAwesomeAction";
Then you need to add the action to the intent before you add the intent to the pending intent:
Intent intent = new Intent(context, widget.class);
intent.setAction(YOUR_AWESOME_ACTION);
(Where widget.class is the class of your AppWidgetProvider, your current class)
You then need to create a PendingIntent with getBroadcast
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Set the onClickPendingIntent for the clickable view in your widget
remoteView.setOnClickPendingIntent(R.id.widgetFrameLayout, pendingIntent);
Next, override the onReceive method in the same class:
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
And then respond to your button presses by querying the intent returned for your action within the onReceive method:
if (intent.getAction().equals(YOUR_AWESOME_ACTION)) {
//do some really cool stuff here
}
And that should do it!
Android clickable Widget with ListView not clickable on ListItems
finally it is working:
in the Profider onUpdate:
final Intent activityIntent = new Intent(context, MainActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.WidgetItem, pendingIntent);
and in the ViewsFactory getViewAt:
Bundle infos = new Bundle();
infos.putInt(TickWidgetProvider.WIDGET_LISTID, this.listId);
final Intent activityIntent = new Intent();
activityIntent.putExtras(infos);
remoteView.setOnClickFillInIntent(R.id.widgetListItemLayout, activityIntent);
Do not know where I found it, but this example helped me making it work:
https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget/LoremWidget
how to make whole widget area clickable?
For the child views you need: android:clickable="false"
and android:duplicateParentState="true"
. This causes the child views to not be clickable and to also take on the parent view's state (e.g., pressed). For the parent view you need to set android:clickable="true"
. To make it clickable. Then you only handle the onClick event from the parent view.
click listener on android widget Button not working
So finally I solve the problem after searching for two days. Here is the correct code:
Intent intent = new Intent(context, BluetoothToggle.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
intent.putExtra(toggleBluetoothAction, toggleBluetoothAction);
And here is the onReceive()
method which is called every time I clicked on the widget.
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
if (bundle != null && bundle.getString(toggleBluetoothAction) != null)
{
// My Logic
}
}
Take a look at the intent
setAction
and putExtra
, which are useful in my case to solve the problem. I checked many website but most of them didn't include this piece of code to make widget clickable. All they did is to create an Intent
and then immediately create a PendingIndent
. I did the same but nothing helped me in any way.
Thanks.
Android, gestures over clickable widgets
Solved myself. The idea is to catch gesture events from GestureOverlayView and, apart from passing them to GestureDetector, measure the distance of user's gesture. The distance shall be stored in a private activity's field (thus available for all event handlers). Finally, fling and click handlers shall check the value of this field; if it is below certain value (say, 10 pixels shall be fine), it should be interpreted as a click. Otherwise - as a gesture.
Note, that the button will both look clicked and its click handler will be called. I've decided to create a low-level click handler (attached to all buttons over GestureOverlayView), which does the gesture/click check and if the result is a click, chooses one of higher-level click handlers.
The solution works for me; however if one wished to disable the clicked look of button and prevent the handler from being called, it would probably involve redefining the button and/or GestureOverlayView components.
Related Topics
How to Do a Junit Assert on a Message in a Logger
Java 8: Lambda-Streams, Filter by Method with Exception
Safely Casting Long to Int in Java
Hibernate Criteria Returns Children Multiple Times with Fetchtype.Eager
How to Hash Some String with Sha256 in Java
Who Sets Response Content-Type in Spring MVC (@Responsebody)
What Does Value & 0Xff Do in Java
How to Convert Int[] to Integer[] in Java
Is There a Java API That Can Create Rich Word Documents
Difference Between 'Optional.Orelse()' and 'Optional.Orelseget()'
Java 11 Package Javax.Xml.Bind Does Not Exist
How to Randomize Two Arraylists in the Same Fashion
Why Doesn't Mockito Mock Static Methods
How to Return 2 Values from a Java Method
How to Run Certain Task Every Day at a Particular Time Using Scheduledexecutorservice