How to Open New Activity on Click Button by Existing Activity In Android

Start new Activity by existing Activity on a button click
 
Hi, guys. In this article we will see how to start a new Activity on a button click in the current activity.
 
There are two types of ways to open a new activity.
 
Using the "startactivity(Intent intent)" method and "startActivityforResult(Intent intent, requestCode requestCode)" method.
 
When we open a folder or file using another for importing or save some data then we use startActivityforResult(),
 
Because by calling this second Activity the open is only for some time and provides some output.
 
Whereas startActivity() opens a separate new activity.
 
Step 1
 
Create  a new  project: "DemoActivity File" --> "New" --> "Android Application Project".
 
new prjt.jpg
 
Step 2
 
Open "DemoActivity /res/values/string.xml" and update it as in the following code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="app_name">MyDemo</string>  
  4.     <string name="hello_world">Hello world!, this is my android application </string>  
  5.       <string name="lblBtn">Open Activity Demo</string>  
  6.       <string name="btn"></string>  
  7.     <string name="lblTxt">This is another activity sarting by main activity</string>  
  8.     <string name="menu_settings">Settings</string>  
  9. </resources> 
Step 3
 
Open "DemoActivity/AndroidManifest.xml" and update it as in the following code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.mydemo"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.  <uses-sdk  
  7.         android:minSdkVersion="8"  
  8.         android:targetSdkVersion="17" />  
  9.     <application  
  10.         android:allowBackup="true"  
  11.         android:icon="@drawable/ic_launcher"  
  12.         android:label="@string/app_name"  
  13.         android:theme="@style/AppTheme" >  
  14.         <activity  
  15.             android:name="com.example.mydemo.MainActivity"  
  16.             android:label="@string/app_name" >  
  17.             <intent-filter>  
  18.                 <action android:name="android.intent.action.MAIN" />  
  19.                 <category android:name="android.intent.category.LAUNCHER" />  
  20.             </intent-filter>  
  21.         </activity>  
  22.         <activity  
  23.             android:name=".AnotherActivity"  
  24.             android:label="@string/app_name">  
  25.             <intent-filter >  
  26.                 <action  android:name="in.wptrafficanalyzer.AnotherActivity"/>  
  27.                 <category  android:name="android.intent.category.DEFAULT"/>  
  28.             </intent-filter>  
  29.         </activity>  
  30.     </application>  
  31. </manifest> 
Step 4
 
Create a new Java file in the "com.example.mydemo" package named "AnotherActivity.java" with the following code:
  1. package com.example.mydemo;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. public class AnotherActivity extends Activity{  
  5.       @Override  
  6.       protected void onCreate(Bundle savedInstanceState) {  
  7.             // TODO Auto-generated method stub  
  8.             super.onCreate(savedInstanceState);  
  9.             setContentView(R.layout.another);  
  10.       }  
Step 5
 
Create a new XML file in the "Demo/res/layout/" directory as "activity_another.xml" with the following code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.     <TextView  
  7.         android:id="@+id/txt"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/lblTxt"  
  11.         />  
  12. </LinearLayout> 
Step 6
 
Open "Demo/src/com.example.mydemo/MainActivity.java" and update it as in the following code:
  1. package com.example.mydemo;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. public class MainActivity extends Activity {  
  10.       @Override  
  11.       protected void onCreate(Bundle savedInstanceState) {  
  12.             super.onCreate(savedInstanceState);  
  13.             setContentView(R.layout.activity_main);  
  14.             OnClickListener listnr=new OnClickListener() {  
  15.                   @Override  
  16.                   public void onClick(View v) {  
  17.                         Intent i= new Intent("AnotherActivity");  
  18.                         startActivity(i);  
  19.                   }  
  20.             };  
  21.             Button btn =(Button) findViewById(R.id.btn);  
  22.             btn.setOnClickListener(listnr);  
  23.       }  
Step 7
 
Open "Demo/res/layout/activity_main.xml" and update it with the following code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     tools:context=".MainActivity" >  
  7.     <TextView  
  8.         android:layout_width="125dp"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello_world" />  
  11.     <Button  
  12.         android:id="@+id/btn"  
  13.         android:layout_width="134dp"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="@string/lblBtn" />  
  16. </LinearLayout> 
Run the Application and see the output.
 
Current Activity:
 
out1.jpg
 
New Activity:
 
2nd otpt.jpg