Today we are going to discuss about switching from one activity to another activity and pass data from one activity to another activity and get data in second activity by using intents in android
Intent:
1)This is one of Android Component.
2)Intents provide Communication between Activities
Create a Project:
create an android project in Eclipse
1) Layout files
Create a two layout files in res/layout folder
first.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" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="send" />
</RelativeLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<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" >
<TextView
android:id="@+id/datadisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="25sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
2) Java files
Create two java files
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText getedit;
Button sendbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
getedit=(EditText)findViewById(R.id.editText1);
sendbtn=(Button)findViewById(R.id.button1);
sendbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String getdata=getedit.getText().toString();
Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
intent.putExtra("senddata", getdata);
startActivity(intent);
}
});
}
}
SecondActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends Activity {
TextView displaydata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
displaydata=(TextView)findViewById(R.id.datadisplay);
Bundle extras = getIntent().getExtras();
String ddata=extras.getString("senddata");
displaydata.setText(Html.fromHtml("<b>Data from first Activity</b> "+ddata));
}
}
3) AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.forblog1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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="com.example.forblog1.SecondActivity"></activity>
</application>
</manifest>
* Switching from one activity to another activity
add the below code where you want navigation between activities
Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
startActivity(intent);
4)Demo
Finally,Run your application
OUTPUT:
Result of this application as like below
enter any data if you want in edittext like android then click on enter
data will be display in second activity like as shown below
Intent:
1)This is one of Android Component.
2)Intents provide Communication between Activities
Create a Project:
create an android project in Eclipse
1) Layout files
Create a two layout files in res/layout folder
first.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" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="send" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<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" >
<TextView
android:id="@+id/datadisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="25sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
Create two java files
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText getedit;
Button sendbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
getedit=(EditText)findViewById(R.id.editText1);
sendbtn=(Button)findViewById(R.id.button1);
sendbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String getdata=getedit.getText().toString();
Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
intent.putExtra("senddata", getdata);
startActivity(intent);
}
});
}
}
SecondActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends Activity {
TextView displaydata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
displaydata=(TextView)findViewById(R.id.datadisplay);
Bundle extras = getIntent().getExtras();
String ddata=extras.getString("senddata");
displaydata.setText(Html.fromHtml("<b>Data from first Activity</b> "+ddata));
}
}
3) AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.forblog1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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="com.example.forblog1.SecondActivity"></activity>
</application>
</manifest>
* Switching from one activity to another activity
add the below code where you want navigation between activities
Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
startActivity(intent);
4)Demo
Finally,Run your application
OUTPUT:
Result of this application as like below
enter any data if you want in edittext like android then click on enter
data will be display in second activity like as shown below