Navigation from one screen to another screen in android HelloWorld by Manasi Dharne - March 27, 2019April 11, 20190 Contents Functionality: How to interact with activity,when a button is clicked,navigate from current screen(current activity) to another screen(another activity). Activity: In android,an activity is representing a single screen.Most applications have multiple activities to represent different screens.for example,one activity to display a list of the application settings,another to display application status. To Create any application in android we have to deal with three files: AndroidManifest.xmlactivity_main.xmlMainActivity.java AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.nav.navigation" > <application android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="Second_screen"></activity> </application> </manifest> Strings.xml: <resources> <string name="app_name">Navigation</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">MainActivity</string> <string name="button_name">Go to second screen</string> <string name="title1">Welcome to Navigation (First screen)</string> <string name="title2">Welcome to Navigation (second screen)</string> </resources> In this example,we have to navigate from one to another screen so we need to create two layout files and two java files. activity_main.xml: This is layout file which represents first screen.First screen contains one textview and button.Textview is texted as “Welcome to Navigation” and button texted as “Go To second screen”. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/head" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="26dp" android:text="@string/title1" /> <Button android:id="@+id/first_screen_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/head" android:layout_marginTop="16dp" android:text="@string/button_name" /> </RelativeLayout> MainActivity.java: This java file contains code to navigate to second screen.file has OnClickListener which make Button click to go to second screen. package com.nav.navigation; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button nextScreen; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nextScreen = (Button) findViewById(R.id.first_screen_button); nextScreen.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent agn = new Intent(MainActivity.this, Second_screen.class); startActivity(agn); } }); } public void onClick(View v) { } } second_screen.xml: This is layout file which represents second screen.second screen contains one textview.Textview is texted as “Welcome to Second Screen”. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/head" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/title2"/> </RelativeLayout> Second_screen.java This java file contains code of action to be performed after clicking on button in first screen. package com.nav.navigation; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; public class Second_screen extends Activity implements OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_screen); TextView tv = new TextView(this); tv.setText("Welcome to Second screen"); setContentView(tv); } public void onClick(View v) { } } Output: First screen Second screen Share this:Click to share on Twitter (Opens in new window)Click to share on Facebook (Opens in new window)MoreClick to share on LinkedIn (Opens in new window)Click to share on WhatsApp (Opens in new window)Click to email a link to a friend (Opens in new window)Like this:Like Loading... Related