LOGO1_03

Switching One activity to another activity in Android by intent

Hi friends the last tutorials we have seen single screen Activity but maximum project need multiple screen
so we need to learn how android handle multiple screen, go one screen to another by Intent calling.
Now create an android project and take two activity, take two different screen and take one button
in each screen.here two activity class

1.IntentCallingFirstActivity.java

2.IntentCallingSecondActivity.java
and two different xml

1.first_activity.xml

2.second_activity.xml

Now follow the code


IntentCallingFirstActivity.java


package com.androidfromhome.intentcalling;

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

public class IntentCallingFirstActivity extends Activity implements OnClickListener {

    private Button btn_press;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_activity);
        
        btn_press=(Button)findViewById(R.id.btn_press);
        btn_press.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        
        switch(v.getId()){
        
        case R.id.btn_press:
            
            //Creation of Intent Class Object
            Intent intent_obj=new Intent(IntentCallingFirstActivity.this,IntentCallingSecondActivity.class);
            
            startActivity(intent_obj);
            
            
            break;
        
        }
        
    }

}


IntentCallingSecondActivity.java


package com.androidfromhome.intentcalling;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class IntentCallingSecondActivity extends Activity implements OnClickListener {

    
    private Button btn_second;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        
        btn_second=(Button)findViewById(R.id.btn_second);
        btn_second.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        
        switch(v.getId()){
        
        case R.id.btn_second:
            finish();
            break;
        
        }
        
    }

    
}


first_activity.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#dfdfdf"
    android:orientation="vertical"
    tools:context="com.androidfromhome.intentcalling.IntentCallingFirstActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Intent Calling Example"
        android:textColor="#000000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_press"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#6FB35C"
            android:padding="10dp"
            android:text="Go Other Activity"
            android:textColor="#ffffff"
            android:textSize="18dp"
            android:textStyle="bold" />

    </LinearLayout>

</LinearLayout>


second_activity.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#dfdfdf"
    android:orientation="vertical"
    tools:context="com.androidfromhome.intentcalling.IntentCallingFirstActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Intent Calling Example"
        android:textColor="#000000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_second"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#6FB35C"
            android:padding="10dp"
            android:text="Go Back To First Activity"
            android:textColor="#ffffff"
            android:textSize="18dp"
            android:textStyle="bold" />

    </LinearLayout>

</LinearLayout>


Download Source Code

Leave a Reply

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

Get Widget