Intent

An intent is an abstract description of an operation to be performed. In simple words, we can say that intent is basically used to notify the Android system of the occurrence of an event.
Procedure
  1. Start the Eclipse IDE.
  2. Create a new project.
  3. Create two Java files, one is MainActivity.java file and the other one is B.java.
  4. Create two XML files, one is the activity_main.xml file and the second is ll.xml for layout design.
  5. In the third case of intent we add an activity with an intent filter to the manifest file like this:
    <activity android:name="B"></activity>

  6. In the MainActivity.java file make an intent with an extra string like this:
    Intent i=new Intent(getApplicationContext(),B.class);
    i.putExtra("money", e1.getText().toString());
    startActivity(i);
  7. In the B.java file within the onCreate function use a getIntent with getStringExtra like this:
    Intent i=getIntent();
    tv.setText(str);
    String str=i.getStringExtra("money");
    Code is given below
    1. MainActivity.java: package com.example.intentcase3rd;
    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. import android.widget.EditText;
    10. public class MainActivity extends Activity implements OnClickListener {
    11. EditText e1;
    12. Button b1;
    13. @Override
    14. protected void onCreate(Bundle savedInstanceState) {
    15. super.onCreate(savedInstanceState);
    16. setContentView(R.layout.activity_main);
    17. e1 = (EditText) findViewById(R.id.editText1);
    18. b1 = (Button) findViewById(R.id.button1);
    19. b1.setOnClickListener(this);
    20. }
    21. public void onClick(View v) {
    22. Intent i = new Intent(getApplicationContext(), B.class);
    23. i.putExtra("money", e1.getText().toString());
    24. startActivity(i);
    25. }
    26. }
    B.java
    1. package com.example.intentcase3rd;
    2. import android.app.Activity;
    3. import android.content.Intent;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.view.View.OnClickListener;
    7. import android.widget.Button;
    8. import android.widget.TextView;
    9. public class B extends Activity implements OnClickListener {
    10. TextView tv;
    11. Button b1;
    12. @Override
    13. protected void onCreate(Bundle savedInstanceState) {
    14. super.onCreate(savedInstanceState);
    15. setContentView(R.layout.ll);
    16. tv = (TextView) findViewById(R.id.textView1);
    17. Intent i = getIntent();
    18. String str = i.getStringExtra("money");
    19. tv.setText(str);
    20. b1 = (Button) findViewById(R.id.button1);
    21. b1.setOnClickListener(this);
    22. }
    23. public void onClick(View v) {}
    24. }
    activity_main.xml
    1. <RelativeLayout
    2. 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. android:paddingBottom="@dimen/activity_vertical_margin"
    7. android:paddingLeft="@dimen/activity_horizontal_margin"
    8. android:paddingRight="@dimen/activity_horizontal_margin"
    9. android:paddingTop="@dimen/activity_vertical_margin"
    10. tools:context=".MainActivity">
    11. <TextView
    12. android:id="@+id/textView1"
    13. android:layout_width="wrap_content"
    14. android:layout_height="wrap_content"
    15. android:text="Intent3 Demo"/>
    16. <EditText
    17. android:id="@+id/editText1"
    18. android:layout_width="wrap_content"
    19. android:layout_height="wrap_content"
    20. android:layout_alignParentRight="true"
    21. android:layout_below="@+id/textView1"
    22. android:layout_marginRight="54dp"
    23. android:layout_marginTop="53dp"
    24. android:ems="10">
    25. <requestFocus/>
    26. </EditText>
    27. <Button
    28. android:id="@+id/button1"
    29. android:layout_width="wrap_content"
    30. android:layout_height="wrap_content"
    31. android:layout_below="@+id/editText1"
    32. android:layout_marginLeft="18dp"
    33. android:layout_marginTop="31dp"
    34. android:layout_toRightOf="@+id/textView1"
    35. android:text="Send"/>
    36. </RelativeLayout>
    ll.xml
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout
    3. xmlns:android="http://schemas.android.com/apk/res/android"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent"
    6. android:orientation="vertical" >
    7. <TextView
    8. android:id="@+id/textView1"
    9. android:layout_width="wrap_content"
    10. android:layout_height="wrap_content"
    11. android:text="Large Text"
    12. android:textAppearance="?android:attr/textAppearanceLarge" />
    13. <Button
    14. android:id="@+id/button1"
    15. android:layout_width="wrap_content"
    16. android:layout_height="wrap_content"
    17. android:text="Ok" />
    18. </LinearLayout>

    intentcase3rd Manifest

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest
    3. xmlns:android="http://schemas.android.com/apk/res/android"
    4. package="com.example.intentcase3rd"
    5. android:versionCode="1"
    6. android:versionName="1.0" >
    7. <uses-sdk
    8. android:minSdkVersion="8"
    9. android:targetSdkVersion="17" />
    10. <application
    11. android:allowBackup="true"
    12. android:icon="@drawable/ic_launcher"
    13. android:label="@string/app_name"
    14. android:theme="@style/AppTheme" >
    15. <activity
    16. android:name="com.example.intentcase3rd.MainActivity"
    17. android:label="@string/app_name" >
    18. <intent-filter>
    19. <action android:name="android.intent.action.MAIN" />
    20. <category android:name="android.intent.category.LAUNCHER" />
    21. </intent-filter>
    22. </activity>
    23. <activity android:name="B"></activity>
    24. </application>
    25. </manifest>
Output
Output
By clicking on the Send button it is intended to another activity as in the following:
Intented to other activity