Post Top Ad

Post Top Ad

Saturday 8 July 2017

Implicit intents

Implicit intents. Android supports explicit and implicit intents. An application can define the target component directly in the intent (explicit intent) or ask the Android system to evaluate registered components based on the intent data(implicit intents).

1.create MainActivity: 
activitymain.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:id="@+id/activity_main"
    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.kiran.lesson4_implicitintent.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Button to Share"
        android:id="@+id/textView"
        android:layout_marginTop="18dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:text="Share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:onClick="shareData"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="Name"
        android:ems="10"
        android:id="@+id/msg_et"
        android:layout_marginTop="88dp"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="88dp"
        android:id="@+id/age_tv"
        tools:text="Age"
        android:textSize="36sp"
        android:textStyle="normal|bold" />
</RelativeLayout>



2.MainActivity.java:
package com.example.kiran.lesson4_implicitintent;

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

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    private EditText mMsgET;
    private TextView mAgeTv;

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

        mMsgET = (EditText) findViewById(R.id.msg_et);
    }

    public void shareData(View view) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        String msg = mMsgET.getText().toString();
        intent.putExtra(Intent.EXTRA_TEXT,msg);
        intent.setType("text/plain");
        startActivityForResult(intent,200);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        mAgeTv = (TextView) findViewById(R.id.age_tv);
        if(requestCode==200 && resultCode==RESULT_OK){
            mAgeTv.setText(data.getStringExtra(Intent.EXTRA_TEXT));
        }

    }
}
 

No comments:

Post a Comment