I have a basic widget that displays a text field. I'm trying to figure out how to launch a website url when a user clicks on the widget itself. I can't seem to find any code on the matter.
5 Answers
We can't use onClickListener
directly with view in widget. Instead we should wrap our Intent
with PendingIntent
and setOnClickPendingIntent
to view.
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
// There maybe > 1 instance of our widget
for (int i : appWidgetIds) {
startBrowsing(context, appWidgetManager, i);
}
}
// Processing click on widget
private void startBrowsing(Context ctx,
AppWidgetManager appWidgetManager, int widgetID) {
RemoteViews widgetView = new RemoteViews(ctx.getPackageName(), R.layout.widget);
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
PendingIntent pIntent = PendingIntent.getActivity(ctx, widgetID, intent, 0);
// viewID - our clickable view ID
widgetView.setOnClickPendingIntent(R.id.viewID, pIntent);
appWidgetManager.updateAppWidget(widgetID, widgetView);
}
Set update period to 0 in metadata.xml because we update it manually
android:updatePeriodMillis="0"
And don't forget
<uses-permission android:name="android.permission.INTERNET"/>

- 5,155
- 2
- 17
- 15
First, set up an action identifier.
private final String WIDGET_CLICK = "WidgetClick";
Then, when initializing the widget, register a PendingIntent for the root view of your widget. Replace "YourAppWidgetProvider" with your widget class that overrides AppWidgetProvider.
Intent intent = new Intent(context, YourAppWidgetProvider.class);
intent.setAction(WIDGET_CLICK);
views.setOnClickPendingIntent(R.id.widget_layout, PendingIntent.getBroadcast(context, 0, intent, 0));
Finally, override onReceive and use the context provided by it to launch your website. Replace "urlString" with the address of your website and don't forget to include http:// in the address.
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction() != null && intent.getAction().equals(WIDGET_CLICK)) {
try {
Intent webIntent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(urlString));
webIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(webIntent);
} catch (RuntimeException e) {
// The url is invalid, maybe missing http://
e.printStackTrace();
}
}
}

- 986
- 2
- 15
- 34
Basically there are 2 options:
- Link alike - create linkified TextView, so by clicking on those link you'll be there (in web)
- Through onClickListener - described in previous answer.
I'd prefer 1st approach:
TextView tv=(TextView ) findViewById(R.id.myTextView);
tv.setText(Html.fromHtml("<a href='stackoverflow.com'>Go StackOverFlow!</a>"));

- 16,638
- 18
- 73
- 146
I'm guessing since you said "widget" that the previous answers aren't working for you. I'm in the same boat--you'll likely need to set a pending intent via the RemoteViews API like is done here (http://stackoverflow.com/questions/2082998/how-to-implement-a-button-on-an-android-widget) except that you'll pass in the textview ID instead of a button ID... I think.
You shouldn't need the fancy intent action stuff... just replace the Intent that they use in the PendingIntent call with the one from @CommonsWare's answer to this question.

- 6,532
- 1
- 46
- 57
set that TextView to be clickable with TextView.setOnClickListener(this)
and then let your activity implement the OnClickListener
interface and define the public void onClick(View v)
method.
Inside the onClick(View v)
method, do this:
public void onClick(View v){
startActivity(new Intent(Intent.ACTION_VIEW, url));
}
This will open the url
in the device browser.
You can also have another activity that uses WebView to display the website.
I hope it was helpful.

- 11,231
- 7
- 46
- 81
-
Android widgets are built around an AppWidgetProvider, not an Activity. AppWidgetProvider does not have such a method as startActivity(Intent intent). – Finnboy11 Dec 28 '13 at 08:56
-
What I suggested here is to let the Widget be clickable and then use `startActivity()` where he handles the click, to fire an intent to open the URL – Aman Alam Dec 28 '13 at 17:43
-
Please take a look at the answer I added. It differs a lot from the one you have. Your answer would be brilliant if this was a normal Activity, but widgets behave differently compared to that. I'm sorry but I have to keep the -1 vote on this one, but don't let that ruin your day. – Finnboy11 Dec 30 '13 at 12:37
-
I don't mind the downvote. But I feel your solution is a tougher approach to something simple, for no good reason. – Aman Alam Dec 30 '13 at 12:41
-
Have you ever tested your code with a widget class that extends AppWidgetProvider (as they usually do)? AppWidgetProvider does not have the onClick or the startActivity methods and its views can be accessed only with the RemoteViews bundle which doesn't support setOnClickListener. – Finnboy11 Dec 30 '13 at 12:45