Saturday, April 26, 2014

One Activity to Another Activity Android Application

-          -          Launch Eclipse environment as shown below (name it OneActivityToAnotherActivity)

XML layout 1(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"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="40dp"
        android:text="Hello friend!" />
   
    <Button
        android:id="@+id/link_button"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textStyle="bold"
         android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Click to Enter Another Activity" />

</RelativeLayout>

Screen 1(MainActivity.java)
package com.skotsolutions.oneactivitytoanotheractivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
           
            Button link_btn = (Button) findViewById(R.id.link_button);
            link_btn.setOnClickListener(new View.OnClickListener() {
                 
                  @Override
                  public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                 
            Intent i = new Intent (getApplicationContext(),SecondActivity.class);
            startActivity(i);
                  }
            });
      }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
      }

}

XML layout 2(second_activity.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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="20dp"
        android:text="Welcome to Second Activity,friend!" />

</LinearLayout>



Screen 2(SecondActivity.java)

package com.skotsolutions.oneactivitytoanotheractivity;

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


public class SecondActivity extends Activity{
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second_activity);
           
           
      }
}



Android Manifest(AndroidManifest.xml)

Register the second activity (SecondActivity.java) into your android manifest

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.skotsolutions.oneactivitytoanotheractivity.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=".SecondActivity" />
    </application>

</manifest>









download the source code OneActivityToAnotherActivity.zip

No comments:

Post a Comment