I have a really beginner question about activities, I have just started a new project and I added a button. I would like to know how I can create a second activity (in eclipse) and THEN how do I link the first activity to the second with a button.
To open another activity from an activity you should use Intents.
Tutorial from android doc: http://developer.android.com/guide/components/intents-filters.html
Example:
// The context, The activity to open
Intent intent = new Intent(this, NewActivity.class);
// It will open the activity
startActivity(intent);
Intent constructor, startActivity
It will open NewActivity activity, you should replace NewActivity.class with the Class name to open.
And remember you should add the Activity in the AndroidManifest.xml
Since you asked, to open an Activity at button click you need to use the OnClickListener of the Button, setOnClickListener will be used to set the listener.
// i get the reference to the button from the XML
Button button = (Button)findViewById(R.id.button);
// now i set the listener
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// Here you should add the code you want to execute when the button is clicked
// In our case we want to open the activity
Intent intent = new Intent(this, NewActivity.class);
// It will open the activity
startActivity(intent);
// ... and stop.
}
});
new View.OnClickListener() this line creates an anonymous class which implements interface View.OnClickListener... Read more here if you want to know.
Related
I'm getting this error when I write the intent inside the onCreate method.
But when I write the intent inside an outer method and call it, it works.
Button click listener is an interface and you implemented it here as an anonymous class, so inside of that class this refers to that anonymous class, not your activity class, but Intent constructor needs activity class implementation, therefore as #ADITYA RANADE answered you need to change it to MainActivity.this.
However if you replace anonymous class with lambda you can avoid this:
Button button = new Button(context);
button.setOnClickListener(v -> {
Intent intent = new Intent(this, MainActivity.class);
});
Change it to MainActivity.this inside the intent
Well that is because of the place or more precisely "context" (not the androidish Context) of where you are calling it.
When you call it from the anonymously created inner class which implements the listener for a view click, so in this case the this represents something else - anonymous class.
But on the other side when you make the method e.g. openScheduleActivity() inside the activity itself, the this keyword represents the activity itself and in fact represents the androidish Context or in this particular case even the activity. So you can either stay with case that you have already had there and slightly edit it, or you can use lambda expression, or you can use the method inside the activity itself as you have already discovered.
edited case:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, schedule.class);
startActivity(intent);
}
});
lambda expression:
button.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, schedule.class);
startActivity(intent);
});
I have the first activity and a button on it , when I click the button , I go to the second activity. And when I moved / am on the second activity I would like the first activity not to be destroyed but to pause(FirstActivity.Pause). Tell me if there are ways to implement this ?
I want to implement this so that the data from the first activity is not deleted.
Thanks!
Your activity is supposed to pause.
You should add this line of code to your on create method:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, SecondaryActivity.class));
}
});
Your Main activity is going to be paused whilst you are on your secondaryActivity.
its the defualt behavior to pause first activty you didn't call finish() on it .
your Intent should be like this
Intent i= new Intent(context, secondActivity.class);
startActivity(i);
please share your code to find issue .
how can I order a button in an activity to open another activity not by writing codes and only by using properties menu or right-clicking on the button(pointed in the picture)image.
also I create a new activity(.java file) in src file.
Is it possible or I must write codes?
For calling other activity you need to write code i.e. Intent like this you can call another activity
Intent intent = new Intent(this,anotherActivity.class);
startActivity(intent);
You must. But its 1 line of code only. Add this to your button's onClick
startActivity(new Intent(this, ActivityToOpen.class));
In your layout xml file inside button write attribute android:onClick
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MyButton"
android:onClick="YourMethodName" />
and inside Activity
public void YourMethodName(View v) {
// ButtonClickAction
}
and this will call the method when button is clicked
how ever i would recommend to use Intent
You have to write code, there is no escaping that. For opening another activity after clicking a button add the following on the OnClickListener of the button:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
});
Remember to add the next activity within your AndroidManifest.xml file as follows or the activity won't run:
<activity android:name=".ActivityB"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar.BlackActionBar" />
Please how do I link a new activity from my main activity using a clickable text. I have set the text to clickable from my main.xml but I don't know how to call the new activity from my MainActivity.java class. I know I have to use this code "textView.setOnClickListener(new View.OnClickListener());" I found in a similar question, but I don't know how and where to place it on my MainActivity.java class so that it calls a the next activity I named display
Check out Intent. You use these to start new activities or services within your application.
You're correct in that you have to assign an OnClickListener interface to your text, after you made it clickable. In the interface's onClick() method you would need to do something like this.
For example:
#Override
public void onClick(View v) {
// Create the intent which will start your new activity.
Intent newActivityIntent = new Intent(MainActivity.this, NewActivity.class);
// Pass any info you need in the next activity in your
// intent object.
newActivityIntent.putExtra("aString", "some_string_value");
newActivityIntent.putExtra("anInteger", some_integer_value);
// Start the new activity.
startActivity(newActivityIntent);
}
In the next activity, you can retrieve the intent used to start it, so that you'll have access to the data you passed from the first activity, like so:
#Override
public void onCreate(Bundle savedInstanceState) {
// Get the intent that started this activity.
Intent startingIntent = getIntent();
// Retrieve the values.
String aString = startingIntent.getStringExtra("aString");
Integer anInteger = startingIntent.getIntExtra("anInteger", 0); // 2nd param is the default value, should "anInteger" not exist in the bundle.
// Use the values to your hearts content.
}
Hope that helps.
i have 5 layouts and buttons that navigate from one to another live this: a-c, a-b, c-d, d-e.
I only can navigate from a to b anc c. I put the same code to the others but application stops.
Assuming your layouts are Activities; All you need to do is set button click listeners and call corresponding activity in listener block.
For instance; in ActivityA (onCreate() method)
btnStartB = (Button)findViewById(R.id.btnStartB);
btnStartB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
}
});
you will need to do same for btnStartC in ActivityA, btnStartD in ActivityC and btnStartE in ActivityD. Try debugging and figure out where it stops.