First 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 about the occurrence of an event.
 
Procedure
 
Use the following procedure:
  1. Start Eclipse IDE.
  2. Create a new project.
  3. Create two Java files, one is MainActivity.java and the other is a.java.
  4. Create two XML files, one is activity_main.xml file and the second is other.xml for layout design.
  5. In the first case of intent, we add an activity with an intent filter in the manifest file.
     
    For instance
    1. <activity    
    2. android:name="A"    
    3. android:label="@string/app_name" >    
    4. <intent-filter>    
    5. <action android:name="abhijeet" />    
    6. <category android:name="android.intent.category.DEFAULT" />    
    7. </intent-filter>    
    8. </activity>   
       
  6. Then look up the button by its id in the MainActivity.java file and declare a string in it.
  7. Then in the onclick function declare an intent.
     
    For example
    1. public void onClick(View v) {    
    2. Intent i=new Intent(str);    
    3. startActivity(i);    
    4. }   
       
The code is given below.
 
MainActivity.java
  1. package com.example.intentistcaseeeee;  
  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 implements OnClickListener {  
  10.  Button b1;  
  11.  String str = "abhijeet";  
  12.  @Override  
  13.  protected void onCreate(Bundle savedInstanceState) {  
  14.   super.onCreate(savedInstanceState);  
  15.   setContentView(R.layout.activity_main);  
  16.   b1 = (Button) findViewById(R.id.button1);  
  17.   b1.setOnClickListener(this);  
  18.  }  
  19.  public void onClick(View v) {  
  20.   Intent i = new Intent(str);  
  21.   startActivity(i);  
  22.  }  
  23.  
  1. <RelativeLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.   
  5. android:layout_width="match_parent"  
  6. android:layout_height="match_parent"  
  7. android:paddingBottom="@dimen/activity_vertical_margin"  
  8. android:paddingLeft="@dimen/activity_horizontal_margin"  
  9. android:paddingRight="@dimen/activity_horizontal_margin"  
  10. android:paddingTop="@dimen/activity_vertical_margin"  
  11. tools:context=".MainActivity" >  
  12.       
  13. <TextView  
  14.   android:id="@+id/textView1"  
  15.   android:layout_width="wrap_content"  
  16.   android:layout_height="wrap_content"  
  17.   android:text="@string/hello_world" />  
  18.     
  19. <Button  
  20.   android:id="@+id/button1"  
  21.   android:layout_width="wrap_content"  
  22.   android:layout_height="wrap_content"  
  23.   android:layout_below="@+id/textView1"  
  24.   android:layout_marginTop="52dp"  
  25.   android:layout_toRightOf="@+id/textView1"  
  26.   android:text="Button" />  
  27.     
  28. </RelativeLayout> 
     
A.java
  1. package com.example.intentistcaseeeee;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. public class A extends Activity {  
  5.  @Override  
  6.  protected void onCreate(Bundle savedInstanceState) {  
  7.   super.onCreate(savedInstanceState);  
  8.   setContentView(R.layout.other);  
  9.  }  
  10.  
Other.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. <Spinner  
  10.   android:id="@+id/spinner1"  
  11.   android:layout_width="match_parent"  
  12.   android:layout_height="wrap_content" />  
  13.     
  14. <RadioButton  
  15.   android:id="@+id/radioButton1"  
  16.   android:layout_width="wrap_content"  
  17.   android:layout_height="wrap_content"  
  18.   android:text="RadioButton" />  
  19.     
  20. <CheckBox  
  21.   android:id="@+id/checkBox1"  
  22.   android:layout_width="wrap_content"  
  23.   android:layout_height="wrap_content"  
  24.   android:text="CheckBox" />  
  25.     
  26. </LinearLayout> 
     
AndroidManifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest  
  3.   
  4. xmlns:android="http://schemas.android.com/apk/res/android"  
  5. package="com.example.intentistcaseeeee"  
  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="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.         </activity>  
  28.         <activity  
  29.   
  30.     android:name="A"  
  31.   
  32.     android:label="@string/app_name" >  
  33.             <intent-filter>  
  34.                 <action android:name="abhijeet" />  
  35.                 <category android:name="android.intent.category.DEFAULT" />  
  36.             </intent-filter>  
  37.         </activity>  
  38.     </application>  
  39. </manifest> 
     
Output
 
Output
 
By clicking on the Button:
 
Output


Similar Articles