Android Screen Navigation Example

Android Screen Navigation Example

Android Screen Navigation Example:

Android_Screen_navigation

download

Android screen Navigation is the very basic one which all the android developers should       know to develop any android application. We have done android screen navigation using     intent in android 4.0.

 

Understanding:
1. Need to create two MainActivity. [Number of mainactivity involves the number of screens, here we are going to do the android navigation using intent with two screens, so we are going to create only two activity.]
File Names:
MainActivity.java
MainActivity2.java

2. Create the two activity xmls, which is basically a screen design files.
File Names:
activity_main.xml
activity_main2.xml

3. Need to add the MainActivity2 into AndroidManifest.xml file. All the screens should be present in the AndroidManifest.xml file and we need to select any one screen or activity as main, where it begins its execution.

 

How the Android Screen Navigation flow work?

It begins its execution with MainActivity file. You can configure the default screen execution changes in AndroidManifest.xml file.

 <activity
 android:name="in.javadomain.screennavigation.MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>

We have used intent to navigate from screen1 to screen2.

As we mapped activity_main2.xml (design file) in MainActivity2, after the navigation activity_main2 will be displayed.

 

 

MainActivity.java
This is the home screen.

package in.javadomain.screennavigation;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;


public class MainActivity extends ActionBarActivity{
    Button navigateToScreen2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        navigateToScreen2 = (Button) findViewById(R.id.button);

        navigateToScreen2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                Intent intent = new Intent("in.javadomain.screennavigation.MainActivity2");
                startActivity(intent);
            }
        });
    }


}

MainActivity2.java
This is the second screen will be displayed after navigating from screen1 or home screen.

package in.javadomain.screennavigation;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

/**
 * Created by Naveen on 12/29/2014.
 */
public class MainActivity2 extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }
}

activity_main.xml:
Design file of the MainActivity.

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Android Screen Navigation"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_marginTop="59dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Welcome to Screen 1"
        android:id="@+id/textView2"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="69dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click to Navigate to Screen 2"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="www.Javadomain.in"
        android:id="@+id/textView3"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

activity_main2.xml:
Design file of the MainActivity2.

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Android Screen Navigation"
        android:id="@+id/textView4"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="82dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Welcome to Screen 2"
        android:id="@+id/textView5"
        android:layout_below="@+id/textView4"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="77dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="you are navigated to here onclick of button displayed on Sccreen 1"
        android:id="@+id/textView6"
        android:layout_below="@+id/textView5"
        android:layout_marginTop="82dp"
        android:layout_alignLeft="@+id/textView4"
        android:layout_alignStart="@+id/textView4"
        android:layout_alignRight="@+id/textView4"
        android:layout_alignEnd="@+id/textView4" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="www.Javadomain.in"
        android:id="@+id/textView7"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.javadomain.screennavigation" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="in.javadomain.screennavigation.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity
            android:name="in.javadomain.screennavigation.MainActivity2"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="in.javadomain.screennavigation.MainActivity2" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>


    </application>

</manifest>

Screenshots:

screen1
Android Screen Navigation – Screen1
Android Screen Navigation - Screen2
Android Screen Navigation – Screen2

 

Share your comments/feedbacks to improve our blogging!

640 total views, 1 views today

Leave a Reply

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