Tuesday, 6 May 2014

Actionbar Tutorials Part 07----Navigating with the App Icon

ScreenShots:
-->Here I'm creating two activity called MainActivity and AnotherActivity

-->From MainActivity we are navigating to AnotherActivity and from AnotherActivity we are           
     navigation back with action bar app icon. Below you can find that two activities and you need
    to do some code in AndroidManifest file under AnotherActivity tag.

activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ram.navigatingupwiththeappicon.MainActivity"
    tools:ignore="MergeRootFrame" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="108dp"
        android:onClick="openAnotherActivity"
        android:text="Open AnotherActivity" />

</RelativeLayout>

anotheractivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Another Activity"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

MainActivity.java:
package com.ram.navigatingupwiththeappicon;

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

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void openAnotherActivity(View v) {

        startActivity(new Intent(getApplicationContext(), AnotherActivity.class));
    }
}

AnotherActivity.java:
 package com.ram.navigatingupwiththeappicon;

import android.app.Activity;
import android.os.Bundle;

public class AnotherActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.anotheractivity);

    }

}


AndroidManifest.xml:
  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ram.navigatingupwiththeappicon"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ram.navigatingupwiththeappicon.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="AnotherActivity"
            android:parentActivityName="com.ram.navigatingupwiththeappicon.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myfirstapp.MainActivity" />
        </activity>

    </application>

</manifest>