How to connect two actions

I made one screen with these two imgs, and I would like to add a button to the bottom of the page that will move to the second page when I press the it.do button do you know how I will write it? I know how to create a button, but I don’t know how to connect two screens! thanks

+4
source share
6 answers

This task is performed using the startActivity () function; method using intentions.

Intent i = new Intent(FromActivity.this, ToActivity.class); startActivity(i); 

In this case, Intent uses your current action as the context in the first parameter, and the target action in the second parameter.

Make sure you add the second action to the manifest (it is in the tag)!

 <activity android:name=".ToActivity" android:label="@string/app_name"> </activity> 
+5
source

Summarizing:

 ImageView myImage = (ImageView) findViewById(R.id.image); myImage.setOnClickListener(new OnClickListener() { @Override onClick(View v) { Intent intent = new Intent(FromActivity.this, ToActivity.class); startActivity(intent); } } ); 
+2
source
 Intent intent = new Intent(currentActivity.this,nextActivity.class); this.finish(); startActivity(intent); 
+1
source
 Button start_button=(Button)findViewById(R.id.btnsend); start_button.setonClickListener(new onClickListener( ){ @override onClick(View view){ Intent i = new Intent(MainActivity.this, NewActivity.class); startActivity(i); } } ); 
0
source

It allows you to split the answer into two parts: XML and the JAVA part, since each action has each of these two. Assuming we have only two actions, "Activity1" is the one that has a button that redirects the user to "Activity2". Since we have 2 activities, we will have 4 files associated with these 2 actions.

XML

so first we’ll make a simple way, as soon as you open the XML file Activity1, you should go to the design tab.

After reaching the constructive part, you can insert the button from the pallet, now you can select the button that is inside your layout. After selecting, you can see the properties of the button on the right side of the screen, where you can effectively change several properties of the button.

Here you will find the option "onClick", fill in the field next to it with something very simple or something that you can remember. For example, enter "nextAct"

or

The hard way is to manually enter the onClick property by entering the string follwing into the button code in XML

 android:onClick="nextAct" 

This is all part of XML.

Java

Open the .java Activity1 file, here you have to create a new method. This method should be called the same as in the "onClick" property of the button. Here I would take "nextAct" as this is what I used in XML. You can put this new method anywhere in the class of the java file, I prefer to store it at the end of the class, since I could easily find it if there is a problem in the future.

Now you need to write the body of the nextAct method. This can be summarized in these two lines.

 public void nextAct(View v){ Intent i = new Intent(this, Activity2.class); startActivity(i); } 

After that, both should be connected and working properly.

0
source

pass id to your button and mention it in your MainActivity.class. You can then call OnClickListener to listen to your click.

 Button mButton = (Button)findViewById(R.id.buttonid); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //you can use anything in place of i Intent i = new Intent(MainActivity.this, NextActivity.class); startActivity(i); } }); 
0
source

All Articles