Third Case of Intent in Android

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.   
    3. <LinearLayout  
    4.     xmlns:android="http://schemas.android.com/apk/res/android"  
    5.     android:layout_width="match_parent"  
    6.     android:layout_height="match_parent"  
    7.     android:orientation="vertical" >  
    8.       
    9.     <TextView  
    10.       android:id="@+id/textView1"  
    11.       android:layout_width="wrap_content"  
    12.       android:layout_height="wrap_content"  
    13.       android:text="Large Text"  
    14.       android:textAppearance="?android:attr/textAppearanceLarge" />  
    15.         
    16.     <Button  
    17.       android:id="@+id/button1"  
    18.       android:layout_width="wrap_content"  
    19.       android:layout_height="wrap_content"  
    20.       android:text="Ok" />  
    21.         
    22. </LinearLayout>  
       

    intentcase3rd Manifest

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


Similar Articles