How to Use Intent in Android

In this tutorial how to transfer data from one page to another and how to move from one page to another using Intent in Android is shown.

Intent is used for transferring the data from one page to another and also used to move from one page to another in Android.

Intent is used to communicate between several pages in Android Application.

Android Intent is the message that is passed between components such as activities, content provider, broadcast receiver, service etc.

Intent is generally used with startActivity() method to invoke Intent.

Android Intent are mainly used for

  • Starting a Service
  • Launching an Activity
  • Display Webpage
  • Display list of Contacts
  • Broadcast Message
  • Dial of Phone

Types of Intent:

  1. Explicit Intent: It is used to transfer the data or communicate between pages of same Android Application.
  2. Implicit Intent: It is used to transfer the data or communicate between pages of different Android Applications.

Code:

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”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” >

<EditText
android:id=”@+id/nm”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentRight=”true”
android:layout_alignParentTop=”true”
android:layout_marginRight=”42dp”
android:layout_marginTop=”108dp”
android:ems=”10″
android:hint=”Enter your name” >
<requestFocus />
</EditText>

<Button
android:id=”@+id/submit”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/nm”
android:layout_below=”@+id/cno”
android:layout_marginTop=”78dp”
android:text=”Submit” />

<Button
android:id=”@+id/reset”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBottom=”@+id/submit”
android:layout_alignRight=”@+id/cno”
android:text=”Reset” />

<EditText
android:id=”@+id/cno”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/nm”
android:layout_centerVertical=”true”
android:ems=”10″
android:hint=”Enter contact no” />

</RelativeLayout>

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {

EditText nm,cno;
Button submit,reset;
String n,c;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nm=(EditText)findViewById(R.id.nm);
cno=(EditText)findViewById(R.id.cno);
submit=(Button)findViewById(R.id.submit);
reset=(Button)findViewById(R.id.reset);

submit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
n=nm.getText().toString();
c=cno.getText().toString();

Intent in=new Intent(MainActivity.this,Second.class);
in.putExtra(“NAME”, n);
in.putExtra(“CNO”,c);
startActivity(in);
}
});

reset.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
nm.setText(” “);
cno.setText(” “);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

second_layout.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent” >

<TextView
android:id=”@+id/nm1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”108dp”
android:text=”TextView” />

<TextView
android:id=”@+id/cno1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/nm1″
android:layout_below=”@+id/nm1″
android:layout_marginTop=”62dp”
android:text=”TextView” />

<Button
android:id=”@+id/back”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/cno1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”72dp”
android:onClick=”onBack”
android:text=”Back” />

</RelativeLayout>

Second.java

package com.example.demo;

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

public class Second extends Activity{

TextView nm1,cno1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);

nm1=(TextView)findViewById(R.id.nm1);
cno1=(TextView)findViewById(R.id.cno1);

String n1=getIntent().getStringExtra(“NAME”);
String c1=getIntent().getStringExtra(“CNO”);

nm1.setText(n1);
cno1.setText(c1);
}

public void onBack(View v)
{
Intent in=new Intent(this,MainActivity.class);
startActivity(in);
}
}

AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.demo”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”17″ />

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.example.demo.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>
<service android:name=”.Secondjava”></service>
</application>

</manifest>

Add a Comment

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