Open a URL when click on ImageView on Android - java

I get the following error below
startActivity (android.content.intent) in activity cannot be applied to (intent)
For this code - I have tried many different things - is there anything I need to add to the manifest? Thank you so much for your help!
img = (ImageView) findViewById(R.id.Fb);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
intent browserintent = new intent("android.intent.action.VIEW", Uri.parse("www.yahoo.com"));
startActivity(browserintent);
}
}

Change your code to this
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yahoo.com"));
startActivity(browserIntent);

You are missing a scheme http or https in your Uri:
Intent browserintent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yahoo.com"));
startActivity(browserintent);

Try this code
Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.yahoo.com"));
startActivity(intent);

Related

Consider adding a `<query>` declaration to your manifest when calling this \method;

I am getting this warning on resolveActivity line while using implicit intents and I am unable to open any website because of that.
btnWeb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String v = et.getText().toString();
Uri uri = Uri.parse(v);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (intent.resolveActivity(getPackageManager())!=null){
startActivity(intent);
}
}
});
Get rid of resolveActivity(). Just call startActivity(), but do so in a try/catch, so you can catch the ActivityNotFoundException if there is no Web browser available.

Not able to go from one activity to other activity in android

I'm trying to use a button to change from Main Activity on android studio to Main Activity 2 and I get the error
no suitable constructor found for Intent(<anonymous OnClickListener>,Class<MainActivity2>)
Intent intent = new Intent(this, MainActivity2.class);
^
I'm on version 4.1 and I want to assume i'm following an old tutorial or I just missed some punctuation.
This is my code:
buttonPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openMainActivity2();
}
public void openMainActivity2(){
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
Just to give more clarity to Caio's answer, when you use this
Intent intent = new Intent(this, MainActivity2.class);
The intent is created in an anonymous inner class i.e. OnClickListener. Thus this does not refer the instance of your Activity (or Context) as intended. You need to provide the correct context of your class.
Hence , do this:
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
You have to try getApplicationContext() or activity.this instead of this.
I think you are having this issue for "this". Cause, I can't see any other issue.
Intent intent = new Intent(getApplicationContext(),MainActivity2.class)
startActivity(intent);
you should provide the correct context of your class.
try this:
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
This java code is for the old version, it does not work with android studio's latest version.
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
startActivities(intent);
here is code for the more recent versions:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openMainActivity();
}
public void openMainActivity() {
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
};

Different methods to open a website

I want to implement a button in my app which leads to a website. I found these two easy ways to solve it and I wonder if there is actually a functional difference between these two methods?
1st way:
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse("http://google.com/");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
2nd way:
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://www.yourURL.com"));
startActivity(intent);
}
});
According to the Intent documentation (http://developer.android.com/reference/android/content/Intent.html), the first and second parameters to the constructor merely set the Action and the Data URL (respectively).
So:
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
Would be functionally identical to:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
The only difference I see would be the inclusion of the category in the second block, which is documented here: http://developer.android.com/reference/android/content/Intent.html#addCategory%28java.lang.String%29

Android - How to open a specific folder in a gallery using an implicit intent?

We are building a camera app that saves photos in a specific folder in the gallery. And we have to open the folder of our app in the gallery using an intent. We are using this code however it shows all folders.
View.OnClickListener goToGallery = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
};
To open gallery I use this intent.
public static final int RESULT_GALLERY = 0;
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent , RESULT_GALLERY );
Try this code.
It will retrieve view pictures under storage/emulated/0/Pictures/MyAppPics
You can change the file path according to your directory path.
File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File f = new File(sdDir, "MyAppPics");
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.withAppendedPath(Uri.fromFile(f), "/MyAppPics"), "image/*");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

Open url from transparent activity

How to open an url in specifying browser from transparent activity?
I'm using the code:
public void open() {
Log.d("info","start");
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setData(Uri.parse("http://rambler.ru"));
i.setAction("com.android.browser");
ComponentName comp = new ComponentName("com.android.browser", "com.android.browser.BrowserActivity");
i.setComponent(comp);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(
i);
finish();
}
but it doesn't work.
Browser is already opened
Use below code:
String url= "your url"
final Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(Url));
startActivity(browserIntent);

Categories