How to switch between screens?

I am new to Android development world. I created a simple application and created a simple one-button GUI. If the user clicks this button, I want to change the screen to show a different GUI.

How can i do this?

+4
source share
6 answers

Run this code:

public class Activity1 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button next = (Button) findViewById(R.id.Button01); next.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent myIntent = new Intent(view.getContext(), Activity2.class); startActivityForResult(myIntent, 0); } }); } } 

activity 2:

 public class Activity2 extends Activity { /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); Button next = (Button) findViewById(R.id.Button02); next.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent intent = new Intent(); setResult(RESULT_OK, intent); finish(); } }); } 

Also make sure to create 2 different xml in the layout folder and add as an action in the manifest file , e.g.

 <activity android:name=".Activity2"></activity> 

As shown in the example.

Hope this helps you.

+12
source

You can do something like this:

 import android.view.View; /** Called when the activity is first created. */ public class YourActivity extends Activity implements View.OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.your_button_id); button.setOnClickListener(this); } public void onClick(View v) { Intent intent = new Intent(your_present_activity.this, target_activity.class); startActivity(intent); } } 
+5
source

To move from one Activity to another action you need to use Intent .

For example, you have one action "A" contains a button and a second action "B", and you want to switch from "A" to "B", and then write:

 Intent intent = new Intent(A.this, B.class); startActivity(intent); 

Practical and informative examples for intent: Android Intents - Tutorial

+4
source

Set the onClick button to a method that uses Intent.

XML:

 <Button android:onClick="to_different_page"/> </Button> 

Java Method:

 public void to_different_page(View view) { Intent intent = new Intent(this, browse_post_activity.class); startActivity(intent); } 
+3
source

In another way, Button in the XML graphic layout and embedding the click listener button along with the onclick method. The onclick method will start a new action using the intent.

 <Button android:id="@+id/buttonClick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Button" /> 

Now go to your class

 // Locate the button in activity_main.xml button = (Button) findViewById(R.id.buttonClick); // Capture button clicks button.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // Start NewActivity.class Intent myIntent = new Intent(getApplicationContext(),AddYourNewActivityName.class); startActivity(myIntent); } }); 

Hope this helps.

Read more http://www.vogella.com/tutorials/AndroidIntent/article.html

+3
source

A better word than β€œScreen” is β€œ Activity ”, you switch between β€œActions in android”.

A very simple way: create a button for one action (Allows you to call this first action) and assign it a method like onClick = startSecondActivity in the XML file ,

Open FirstActivity.java, Add the startSecondActivity method inside the main method, as shown below. It will work!

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set the content of the activity to use the activity_main.xml layout file setContentView(R.layout.activity_main); } public void startSecondActivity(View view){ Intent i = new Intent(this, SecondActivity.class); startActivity(i); } } 
+2
source

Source: https://habr.com/ru/post/1379432/


All Articles