Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to switch between Activities.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 142
    Comment on it

    In the below example I will discusss switching between activities in android. Here I have created two activities and named them Activity1 and Activity2 and used Intent and onclickListener in both activities. Also with I have created two xml layout, in both xml layout I have added one Textview and one button. See the below code example i have clearly describe you how to switch between Activities.

    Step(1)-main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffffff"  >
    
        <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="This is Activity 1" />
    
           <Button android:text="Next"
            android:id="@+id/Button01"
            android:layout_width="250px"
                android:textSize="18px"
            android:layout_height="55px">
        </Button>    
    
    </LinearLayout>
    

    Step(2)-Create main2.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffffff"  >
    
        <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="This is Activity 2" />
    
           <Button android:text="Previous"
            android:id="@+id/Button02"
            android:layout_width="250px"
                android:textSize="18px"
            android:layout_height="55px">
        </Button>    
    
    </LinearLayout>
    

    Step(3)-Add the second Activity to the main manifest file. Open AndroidManifest.xml and add:-

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.warriorpoint.taxman2"
          android:versionCode="1"
          android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".Activity1"
                      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=".Activity2"></activity>
        </application>
        <uses-sdk android:minSdkVersion="3" />
    </manifest>
    

    Step(4)-Activity1-

    public class Activity1 extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            Button next = (Button) findViewById(R.id.Button01);
            next.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Intent myIntent = new Intent(view.getContext(), Activity2.class);
                    startActivityForResult(myIntent, 0);
                }
    
            });
        }
    }
    

    Step(5)-Activity2-

    public class Activity2 extends Activity {
    
        /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main2);
    
            Button next = (Button) findViewById(R.id.Button02);
            next.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    finish();
                }
    
            });
        }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: