Intent in Android with Example

Android Intent: In this tutorial, we will learn about the implementation of Intent with the help of an example and codes in Android. By Manu Jemini Last updated : June 06, 2023

What is Intent in Android?

Intent is a very easy way to move the control from one activity to another. Intent provides very simple techniques to set it. For this tutorial to work, just create a new project with any name. You will get a default activity namely 'MainActivity', you can make use of it. Also, create another activity ‘SecondActivity’ and layout with the choice of your name.

How to Use Intent in Android?

Starting with your XML file - 1, drag and drop a TextView and Button from the widget section. In your XML file - 2, drag and drop a TextView from the widget section. There is no implication about the parent layout type. Make your layout the way you want it.

The second thing is your JAVA file -1. To begin with, you should import TextView and Button from the widget, this is done by adding this line import android.widget.TextView;. After that, in your activity, there would a default onCreate() function, this would be pre-set to use quickly. In your JAVA file–2 you should import TextView from the widget, this is done by adding this line import android.widget.TextView;.

Now, you would need to have a variable of type Intent, Button, TextView and set it with the reference of the corresponding layout objects in corresponding JAVA files with the layout file.

Your app with start with MainActivity and in this activity an event listener has been set (look at the code below) up. To start the SecondActivity use the following line.

  • Intent I=new Intent(MainActivity.this, SecondActivity.class);
  • startActivity(I);

Above line would do it. After doing everything up, your code should look like this. Connect an android device or run an ADV and then build you project to see the result.

Android Intent Example 1

Java file

package com.example.hp.demo;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity
{
    TextView textview;
    Button B;
    Context ctx=this;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        B = (Button) findViewById(R.id.button);
        textview=(TextView)findViewById(R.id.textView);
        B.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent I=new Intent(MainActivity.this, SecondActivity.class);

            startActivity(I);

        }});
    }
}

XML file(1)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="Activity-1"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true" />
</RelativeLayout>

XML file(2)

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.hp.demo.SecondActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="Activity-2"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity"></activity>
    </application>

</manifest>

Output

Intent in Android 1
Intent in Android 1

Android Intent Example 2

Java file(1)

package com.example.hp.demo;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity
{
    EditText name,mobile;
    Button B;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        name=(EditText)findViewById(R.id.name);
        mobile=(EditText)findViewById(R.id.mobile);
        B = (Button) findViewById(R.id.button);
        B.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String data1=name.getText().toString();
            String data2=mobile.getText().toString();

            Intent I=new Intent(MainActivity.this, SecondActivity.class);

            Bundle b = new Bundle();
            b.putString("dataone",data1);
            b.putString("datatwo",data2);

            I.putExtras(b);

            startActivity(I);

        }});
    }
}

Java file(2)

package com.example.hp.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {
TextView textview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        textview=(TextView)findViewById(R.id.textView);
        Bundle b= getIntent().getExtras();

        String data1= b.getString("dataone");
        String data2= b.getString("datatwo");

        textview.setText("Name : "+data1+"\nMobile : "+data2);
    }
}

XML file(1)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:hint="Name" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:ems="10"
        android:id="@+id/mobile"
        android:layout_below="@+id/name"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:hint="Mobile" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click"
        android:id="@+id/button"
        android:layout_below="@+id/mobile"
        android:layout_alignParentStart="true" />
</RelativeLayout>

XML file(2)

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.hp.demo.SecondActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity"></activity>
    </application>

</manifest>

Output

Intent in Android 1
Intent in Android 1


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.