Home Android INTENT (TYPE-2) ANDROID EXAMPLE

INTENT (TYPE-2) ANDROID EXAMPLE

58
0
SHARE

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_intent2.xml ” , edit the following UIcomponents or you can choose yourself.

File : activity_intent2.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.intent2.Intent2Activity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="81dp"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Message"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/textView" />
</RelativeLayout>

 

Second xml file to which the intent is passed,

File : activity_intent3.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.intent2.Intent3Activity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Enter any Text:"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="47dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignTop="@+id/textView2"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@+id/textView2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send Message"
        android:id="@+id/button2"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/editText" />
</RelativeLayout>

 

2. Activity Code

File : Intent2Activity.java

package com.example.cloudinfinity.intent2;

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

import org.w3c.dom.Text;

public class Intent2Activity extends AppCompatActivity
{
    TextView t;
    Button b;
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == 5) {
            String msg = data.getStringExtra("Message");
            t.setText(msg);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent2);

        t=(TextView)findViewById(R.id.textView);
        b=(Button)findViewById(R.id.button);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(Intent2Activity.this,Intent3Activity.class);
                startActivityForResult(i,5);
            }
        });
    }
}

 

Second Activity

File : Intent3Activity.java

package com.example.sachin.intent2;

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 Intent3Activity extends AppCompatActivity
{
    TextView t1;
    EditText e;
    Button b1;

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

        t1=(TextView)findViewById(R.id.textView2);
        e=(EditText)findViewById(R.id.editText);
        b1=(Button)findViewById(R.id.button2);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String msg=e.getText().toString();
                Intent i=new Intent();
                i.putExtra("Message",msg);
                setResult(5,i);
                finish();
            }
        });
    }
}

 

3. Run the application

Run the app, enter the text in the edittext and click Send Message.

Screenshot_20160403-225415 Screenshot_20160403-225424

LEAVE A REPLY

Please enter your comment!
Please enter your name here

15 − one =