How to search a string on google using default browser - android

I want to implement a search function that open the default browser and search the passed string
public void searchOnGoogle(String keywords){
---
}
Is there a way using intent filter or I must implement everything by myself?

String query = URLEncoder.encode(keywords, "utf-8");
String url = "http://www.google.com/search?q=" + query;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);

Related

Open PDF in browser

What I want to achieve is to display PDFs directly in browser.
private void openPDF(String url) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
With this method the PDF is downloaded, but I have to manually open it in order to see it.
I remember this method worked just fine a while a go, did something change in the intent or chrome maybe?
String format = "https://drive.google.com/viewerng/viewer?embedded=true&url=%s";
String fullPath = String.format(Locale.ENGLISH, format, "PDF_URL_HERE");
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fullPath));
startActivity(browserIntent);
If this can help.

Android share intent for Pinterest not working

I am doing an android share intent for Pinterest but is not fully working. I am able to attach the image but I can't send text to the "description" field in the share window. I've tried different types (text/plain, image/*, image/png) and also tried the ACTION_SEND_MULTIPLE intent type but still no luck. Google chrome share intent works perfectly so I'm sure Pinterest supports this functionality. Here is my code:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TEXT, text);
if(file != null) intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setClassName(packageName, name);
this.startActivity(intent);
Any idea? thanks!
I found a way to share to Pinterest with plain Android intents (without using the Pinterest SDK), with help from Pin It button developer docs.
Basically you just open an URL like this with Intent.ACTION_VIEW; the official Pinterest app kindly supports these URLs. (I've earlier used a very similar approach for sharing to Twitter.)
https://www.pinterest.com/pin/create/button/
?url=http%3A%2F%2Fwww.flickr.com%2Fphotos%2Fkentbrew%2F6851755809%2F
&media=http%3A%2F%2Ffarm8.staticflickr.com%2F7027%2F6851755809_df5b2051c9_z.jpg
&description=Next%20stop%3A%20Pinterest
And for smoother user experience, set the intent to open directly in Pinterest app, if installed.
A complete example:
String shareUrl = "https://stackoverflow.com/questions/27388056/";
String mediaUrl = "http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png";
String description = "Pinterest sharing using Android intents"
String url = String.format(
"https://www.pinterest.com/pin/create/button/?url=%s&media=%s&description=%s",
urlEncode(shareUrl), urlEncode(mediaUrl), urlEncode(description));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
filterByPackageName(context, intent, "com.pinterest");
context.startActivity(intent);
Util methods used above:
public static void filterByPackageName(Context context, Intent intent, String prefix) {
List<ResolveInfo> matches = context.getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo info : matches) {
if (info.activityInfo.packageName.toLowerCase().startsWith(prefix)) {
intent.setPackage(info.activityInfo.packageName);
return;
}
}
}
public static String urlEncode(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
}
catch (UnsupportedEncodingException e) {
Log.wtf("", "UTF-8 should always be supported", e);
return "";
}
}
This is the result on a Nexus 5 with Pinterest app installed:
And if Pinterest app is not installed, sharing works just fine via a browser too:
for some reason pinterest app doesn't comply to the standard (Intent.EXTRA_TEXT) so we have to add it separately
if(appInfo.activityInfo.packageName.contains("com.pinterest"){
shareIntent.putExtra("com.pinterest.EXTRA_DESCRIPTION","your description");
}
File imageFileToShare = new File(orgimagefilePath);
Uri uri = Uri.fromFile(imageFileToShare);
Intent sharePintrestIntent = new Intent(Intent.ACTION_SEND);
sharePintrestIntent.setPackage("com.pinterest");
sharePintrestIntent.putExtra("com.pinterest.EXTRA_DESCRIPTION", text);
sharePintrestIntent.putExtra(Intent.EXTRA_STREAM, uri);
sharePintrestIntent.setType("image/*");
startActivityForResult(sharePintrestIntent, PINTEREST);

Google Search for Android

I am trying to put a simple Google Search in my app. I found this code but it's not exactly what I am searching for. Below the search comes from a given url.
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
The thing is that I would like to get the search text from user and if possible without starting a new Activity like this one:
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
String term = editTextInput.getText().toString();
intent.putExtra(SearchManager.QUERY, term);
startActivity(intent);
Get the text from the user and place that in the URI, then start the intent.
String url = "http://www.google.com/search?q=" + userText;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

Open a URL in Android's web browser from application

How can I open a url in the android web browser from my application on clicking a button.
I tried this :
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));
startActivity(myIntent);
but I am getting exception:
but I got an Exception :
"No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com"
You are doing it fundamentally correct, you just need to include the full url.
String strURL="http://www.google.com";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));
startActivity(myIntent);
Basically, the http is the protocol. It is the computer's way of trying to figure out how you want to open it. You might also be interested in https, if you want a secure connection over SSL.
Almost seems like this would be useful to read... from the page:
Uri uri = Uri.parse("http://androidbook.blogspot.com");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uri);
startActivity(launchBrowser);
There is a second way, involving the WebView, but that seems like it's not your question. Hope this helped.
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Try this:
String strURL = "http://www.google.com";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));
startActivity(myIntent);
As for the missing "http://" I'd just do something like this:
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;

Start Google search query from activity - Android

I was wondering if there is an easier way (or any way) to start a Browser with a Google search query. For example user can select a certain word or phrase and click a button and the activity will start the browser with the Google search query.
Thank you.
The Intent class defines an action specifically for web searches:
http://developer.android.com/reference/android/content/Intent.html#ACTION_WEB_SEARCH
Here's an example of how to use it:
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, query); // query contains search string
startActivity(intent);
You can do this quite easily with a few lines of code (assuming you want to search Google for 'fish'):
String escapedQuery = URLEncoder.encode(query, "UTF-8");
Uri uri = Uri.parse("http://www.google.com/#q=" + escapedQuery);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Otherwise, if you would rather start up your own Activity to handle the browsing, you should be able to do so with a WebView: http://developer.android.com/reference/android/webkit/WebView.html
I think the better answer here is #zen_of_kermit's. It would be nice though, if Android allowed a user to provide the Search engine has an extra though for the ACTION_WEB_SEARCH, rather than just using Google.
the # gave me trouble:
Uri uri = Uri.parse("https://www.google.com/search?q="+query);
Intent gSearchIntent = new Intent(Intent.ACTION_VIEW, uri);
activity.startActivity(gSearchIntent);
I recently tried this. This appears to work fine. If any modifications to be done let me know as I am new to android development.
mEdit = (EditText)findViewById(R.id.editText);
in your click view,
String q = mEdit.getText().toString();
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH );
intent.putExtra(SearchManager.QUERY, q);
startActivity(intent);
String Search= null;
try {
Search= URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Uri uri = Uri.parse("http://www.google.com/#q=" + Search);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});

Categories

Resources