Android Intent Tutorial

Android intent is use for linking the form with other form. An intent is use to sending action or performing action one activity to another activity.

Step 1.

Create new application with any name from file menu option.

File → New Project → Application name → select API → Add Blank Activity → Activity Name → finish.

Step 2.

In this application we are creating two activity file and two XML layout file.

Right Click on java →New →Java Class and Create new class as shown in picture.

intent tutorial intent tutorial

Step 3.

Import Package and write following coding in MainActivity.java class file.

Now Create another XML file as shown in picture.
Go to app →res →layout →right click on layout then select New →XML →Layout XML File.

intent tutorial intent tutorial

Step 4.

Now add some code in AndroidManifest.xml file as highlighted in below section.

Go to app → manifest → AndroidManifest.xml file.


    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sumit.passdata" >
    <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=".AnotherActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN2" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        </application>
</manifest>
    

Step 5.

Import Package and write following coding in MainActivity.java class file.

	
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

   public class MainActivity extends ActionBarActivity {
   Button btn1;
    EditText txt1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1=(Button)findViewById(R.id.btnPassData);
        txt1=(EditText)findViewById(R.id.editText);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            // 1. create an intent pass class name or intent action name
            Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
            // 2. put key/value data
            //intent.putExtra("message", "Hello From MainActivity");

            // 3. or you can add data to a bundle
            Bundle extras = new Bundle();
            extras.putString("a", txt1.getText().toString());
            extras.putString("b","hello");

            // 4. add bundle to intent
            intent.putExtras(extras);

            // 5. start the activity
            startActivity(intent);
            }
        });
    }
    

Step 6.

Import Package and write following coding in AnotherActivity.java class file.


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

   /**
   * Created by sumit on 2/10/2015.
   */
  public class AnotherActivity extends Activity{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another);
        {
            Intent intent = getIntent();
            // 2. get message value from intent
            String message = intent.getStringExtra("a");
            // 3. show message on textView
            ((TextView)findViewById(R.id.tvMessage)).setText(message);
        }
     }
  }

Step 7.

Go to activity_another.xml page and write following code.

Go to app → manifest → activity_another.xml file.


  <?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/tvMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

Step 8. Final Output

intent tutorial

Post Your Comment