Switching between activities in Android with example


             Its quite simple to invoke/switch from one activity to another activity, just follow the below steps and get this done, its explained quite clearly with example

Prerequisite
Create an eclipse project as explained here.

Once you’re done, you can see a main activity (activity_main.xml) being created and displays Hello World on execution.

Step 1: Create New Activity

Create another activity (activity2.xml), which will be invoked from activity 1(I will be referring activity_main.xml as activity1)
  • Open AndroidManifest.xml from your solution explorer and select Application tab.
  •  Select Add button, a pop up might open – select first check box(Create a new element at the top    level) which now displays a list, select activity there and click OK.
  •  Now go to Attributes section found nearby and click ‘Name’ there. It will open a pop-up to create a  new Java class, name the class as activity2 and click FINISH. 

You can see activity2.java in Solution explorer as shown below.


Step2: Create New Layout for new Activity.
  • Right click layout folder in solution explorer go to New – Android XML File, I have selected root element as ‘Relative Layout’, name the file as ‘activity2’ and FINISH.
  • Another important point is to make sure we add the below statement to activity2.java under onCreate() method.
setContentView(R.layout.activity2);
This statement ensures corresponding layout gets invoked when the class is called. 
At the end, we have two layouts (activity_main.xml, activity2.xml) and with corresponding java classes under layout and src folder respectively.



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"
    android:background="#5C4033"        
    >  
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        android:textColor="#ffffff"       
        />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="42dp"
        android:text="Click here" />
</RelativeLayout>

Explanation:
  • android:background="#5C4033"   - Change the background color of the layout here.
  • android:text="@string/hello_world"   - hello_world is a string name which can be modified under values- string.xml.  I have changed the name to Activity1. Now add a Button to this activity with Button tag and design it as you wish.
  • Text for the button is ‘click here’
Activity2.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"
    android:background="#F4E6CC"    
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/a2" />
</RelativeLayout>

Explanation
  • Set a different color to this activity and add a Textview tag to display a text and set the valoe of a2 in strings.xml

Values- strings.xml

xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Sample</string>
    <string name="hello_world">Activity1</string>
    <string name="menu_settings">Settings</string>   
    <string name="a2">Activity2</string>
</resources>


Step 3: OnClickListener for Button

MainActivity.java
 protected void onCreate(Bundle savedInstanceState)
 {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);              
     
  final Button click=(Button)findViewById(R.id.button1);       
                   click.setOnClickListener(new View.OnClickListener() {                                            
                                public void onClick(View v) 
                                  {             
Intent launchactivity= new Intent(MainActivity.this,activity2.class);                               
startActivity(launchactivity);                          
                                    }
                                     });      
    }

Explanation
                The code marked above will add listener to the button. We need to invoke the activity through intent. Activity2 is invoked from the activity1 and hence both these are parameters for the Intent launchactivity. Once these are done invoke the Intent with startActivity method.


Now execute the Project and you could see the below screens.








Reactions:

0 comments :

Post a Comment