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
Related
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.
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);
};
So, we're working with intents at school and I'm having trouble with the intents when I try to pass data from the "Activity2" to the "Activity1", when I do the setResult() and stuff. The problem is it won't go back to the first activity when I trigger the event the first time, but it will the second.
I've been working with Android studio only for about 12h so I really lack a lot of understanding.
Here is what I'm doing:
First I call this form the main activity.
public void CheckPassword(View view) {
password = PasswordManagement.getPassword(this);
TextView txtPassword = findViewById(R.id.txtPassword);
if (txtPassword.getText().toString().equals(password)) {
Intent intent;
intent = new Intent(this, WelcomeActivity.class);
intent.putExtra("password", password);
startActivityForResult(intent, 1);
startActivity(intent);
} else {
Intent intent;
intent = new Intent(this, RestrictedActivity.class);
startActivityForResult(intent, 1);
startActivity(intent);
}
}
Then, when I'm done from the second activity I run this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restricted);
lblRestrictedArea = findViewById(R.id.lblRestrictedArea);
lblRestrictedArea.setOnLongClickListener(
new OnLongClickListener() {
public boolean onLongClick(View view) {
intent = new Intent();
intent.putExtra(EXTRA_RESPONSE, true);
setResult(RESULT_OK, intent);
finish();
return false;
}
});
}
And back to the main activity I overwrote this to act according to the response:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
if (data.getBooleanExtra(RestrictedActivity.EXTRA_RESPONSE,false)){
LinearLayoutPasswordActivity.setBackgroundColor(getResources().getColor(R.color.red));
}else{
LinearLayoutPasswordActivity.setBackgroundColor(getResources().getColor(R.color.white));
}
}
}
}
If anyone can help I would be very glad, meanwhile I'll try to solve it my own.
Thanks!
Your are calling startActivity twice. So there are two instance of the same Activity and then you have to finish twice.
Keep your startActivityForResult(...) and delete startActivity in CheckPassword(View view)
->
public void CheckPassword(View view) {
password = PasswordManagement.getPassword(this);
TextView txtPassword = findViewById(R.id.txtPassword);
if (txtPassword.getText().toString().equals(password)) {
Intent intent;
intent = new Intent(this, WelcomeActivity.class);
intent.putExtra("password", password);
startActivityForResult(intent, 1);
// startActivity(intent);
} else {
Intent intent;
intent = new Intent(this, RestrictedActivity.class);
startActivityForResult(intent, 1);
//startActivity(intent);
}
}
Plus, note that you are using the same requestCode (1) for two different activities. The requestCode is very important for onActivityResult method.
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);
Im new on Java so its confusing me a little bit, i want to create an intent for Activity two but there seem to be a problem with the code that i have written.
#Override
public void onClick(View v) {
// TODO:
// Launch Activity Two
// Hint: use Context's startActivity() method
// Create an intent stating which Activity you would like to
// start
Intent activityTwo = new Intent(ActivityTwo.this.finish());
Intent intent = null;
// Launch the Activity using the intent
startActivity(activityTwo);
}
});
// Has previous state been saved?
if (savedInstanceState != null) {
// TODO:
// Restore value of counters from saved state
super.onRestoreInstanceState(savedInstanceState);
mCreate = savedInstanceState.getInt(CREATE_KEY);
mRestart = savedInstanceState.getInt(RESTART_KEY);
mStart = savedInstanceState.getInt(START_KEY);
}
// Emit LogCat message
Log.i(TAG, "Entered the onCreate() method");
// TODO:
from First to Second:
Button next = (Button) findViewById(R.id.button2);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),Second.class);
intent.putExtra("Tag", "Value");
startActivity(intent);
finish();
}});
Second to First:
Button previous= (Button) findViewById(R.id.button);
previous.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),First.class);
startActivity(intent);
}});
First Understand the concept of Intent.
In your case, you want to call ActivityTwo from ActivityOne.
follow this steps
Create ActivityTwo.
Declare ActivityTwo in your AndroidManifest.xml
<activity android:name=".ActivityTwo" />
Write Intent code where in onclick() method.
Intent intent = new Intent(getApplicationContext(),ActivityTwo.class);
startActivity(intent);
In this code Intent Constructor contains two parameters.
Current Context
ActivityTwo reference.