How to Navigate from one screen to another screen in android ?

Hello Android Developers,

Herewith I have explained step by step procedure to navigate from one screen to another screen in android with code. I request you to work with below code to understand clearly.

Step 1: Create a MainActivity.java and paste the below code,

package com.agn.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) {
// TODO Auto-generated method stub

}
}

Step 2: Create a another class called Second_screen.java and paste the below code,

package com.agn.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 Agn’s Second screen”);
setContentView(tv);
}

public void onClick(View v) {
// TODO Auto-generated method stub

}

}

Step 3: Paste the below code in the activity_main.xml,

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
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_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>

Step 4: Create second_screen.xml and paste the below code,

<?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>

Step 5: paste the below code in 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 Agn\’s Navigation (First screen)</string>
<string name=”title2″>Welcome to Agn\’s Navigation (second screen)</string>
</resources>

Step 6: In the Android.Manifest.xml file we need to add our second activity,

paste the below code in Android.Manifest.xml,

<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.agn.navigation”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”15″ />

<application
android:icon=”@drawable/ic_launcher”
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>

Note:

1. Bold letters indicates necessary part for navigation. so that you can take only this part and use in your project.

2. List of files we created and its use,

MainActivity.java (for first screen customization)

secondActivity.java (by button navigation we go to second screen from first screen)

activity_main.xml (textview, edittext, button configurations of mainactivity(first screen))

second_screen.xml (all the configurations of second activity)

strings.xml (its like property file in android)

AndroidManifest.xml (by default only mainactivity.java had been available, once the new activity created we should add in the AndroidManifest.xml file).

Output:

First screen:

After clicking the Go to second screen button,

second screen,

Thanks for reading this post….!!!

454 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *