INTENT (Type-1) Android Example

Home/Android/INTENT (Type-1) Android Example

INTENT (Type-1) Android Example




Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers etc.

It is generally used with startActivity() method to invoke another activity, broadcast receivers etc.

Intents are asynchronous letters which acquiesce appliance apparatus to appeal functionality from added Android components. Intents acquiesce you to collaborate with apparatus from the aforementioned applications as able-bodied as with apparatus contributed by added applications. For example, an action can alpha an alien action for demography a picture.

Android intents are mainly used to:

  • Start the service
  • Launch an activity
  • Display a web page
  • Display a list of contacts
  • Broadcast a message
  • Dial a phone call etc.

For example, let’s accept that you accept an Activity that needs to barrage an email applicant and sends an email application your Android device. For this purpose, your Activity would forward an ACTION_SEND forth with adapted chooser, to the Android Intent Resolver. The defined chooser gives the able interface for the user to aces how to forward your email data.

NOTE :  This project is developed in Android Studio, and tested in Android Marshmallow 6.0 .

 

1. Android Layout Files

In your .xml file whose path is ” res/layout/activity_intent.xml ” , edit the following UIcomponents or you can choose yourself.

File : activity_intent.xml

<?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.cloudinfinity.intent.IntentActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name:"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/editText"
android:layout_alignTop="@+id/textView"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Password:"
android:id="@+id/textView2"
android:layout_below="@+id/editText"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText2"
android:layout_below="@+id/editText"
android:layout_alignEnd="@+id/editText"
android:layout_toEndOf="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="E-mail:"
android:id="@+id/textView3"
android:layout_below="@+id/editText2"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText3"
android:layout_alignTop="@+id/textView3"
android:layout_alignStart="@+id/editText2"
android:layout_alignEnd="@+id/editText2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Phone:"
android:id="@+id/textView4"
android:layout_below="@+id/editText3"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/editText4"
android:layout_below="@+id/editText3"
android:layout_alignStart="@+id/editText3"
android:layout_alignEnd="@+id/editText3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/button"
android:layout_below="@+id/editText4"
android:layout_marginTop="78dp"
android:layout_alignEnd="@+id/editText4"
android:layout_alignParentStart="true" />
</RelativeLayout>

Second xml file to which the intent is passed,
File : activity_second.xml

<?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.cloudinfinity.intent.Second">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView5"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="63dp"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView6"
        android:layout_below="@+id/textView5"
        android:layout_alignParentStart="true"
        android:layout_marginTop="39dp"
        android:layout_alignEnd="@+id/textView5" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView7"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/textView6" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView8"
        android:layout_below="@+id/textView7"
        android:layout_alignParentStart="true"
        android:layout_marginTop="52dp"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

 

2. Activity Code

File : IntentActivity.java

package com.example.cloudinfinity.intent;

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;
import android.widget.TextView;

public class IntentActivity extends AppCompatActivity {
    TextView t1,t2,t3,t4;
    EditText e1,e2,e3,e4;
    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent);

        t1=(TextView)findViewById(R.id.textView);
        t2=(TextView)findViewById(R.id.textView2);
        t3=(TextView)findViewById(R.id.textView3);
        t4=(TextView)findViewById(R.id.textView4);
        e1=(EditText)findViewById(R.id.editText);
        e2=(EditText)findViewById(R.id.editText2);
        e3=(EditText)findViewById(R.id.editText3);
        e4=(EditText)findViewById(R.id.editText4);
        b=(Button)findViewById(R.id.button);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=e1.getText().toString();
                String pass=e2.getText().toString();

                String email=e3.getText().toString();
                String phone=e4.getText().toString();

                Intent intent=new Intent(IntentActivity.this,Second.class);
                intent.putExtra("name",name);
                intent.putExtra("pass",pass);
                intent.putExtra("email",email);
                intent.putExtra("phone",phone);

                startActivity(intent);
            }
        });
    }
}

Second Activity

File : Second.java

package com.example.cloudinfinity.intent;

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

public class Second extends AppCompatActivity {

    TextView t1,t2,t3,t4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        t1=(TextView)findViewById(R.id.textView5);
        t2=(TextView)findViewById(R.id.textView6);
        t3=(TextView)findViewById(R.id.textView7);
        t4=(TextView)findViewById(R.id.textView8);

        Bundle bundle=getIntent().getExtras();

        String s1=bundle.getString("name");
        String s2=bundle.getString("pass");
        String s3=bundle.getString("email");
        String s4=bundle.getString("phone");

        t1.setText(s1);
        t2.setText(s2);
        t3.setText(s3);
        t4.setText(s4);
    }
}

 

3. Run the application

Run the app, enter the text in the edittext and click Submit.

Screenshot_20160402-232142 Screenshot_20160402-232151

By | 2017-05-22T20:00:26+00:00 April 2nd, 2016|Android|0 Comments

About the Author:

Leave A Comment

10 − one =

Avada WordPress Theme