📝
Android Programming
  • Introduction
  • Contents
  • Android
    • Q1
    • Q2
    • Q3
    • Q4
    • Q5
    • Q6
    • Q7
    • Q8
    • Q9
    • Q10
    • Q11
    • Q12
    • End
Powered by GitBook
On this page
  • Create an application with first activity with an editText and send button. On click of send button, make use of implicit intent that uses a SEND ACTION and let user select app chooser and navigate to that application.
  • Code
  • Output
  1. Android

Q4

Question 4

PreviousQ3NextQ5

Last updated 4 years ago

Create an application with first activity with an editText and send button. On click of send button, make use of implicit intent that uses a SEND ACTION and let user select app chooser and navigate to that application.

App Insights:

Made layout for default main activity and added onCreate method, used EditText field and TextView to display the result using Implicit Intent for Share

Code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/editText"
        android:layout_marginTop="40dp"
        android:onClick="share"
        android:text="Send"
        android:textColorHighlight="@color/black"/>

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_marginTop="250dp"
        android:inputType="textPersonName"
        android:text="Some Text to send"
        android:textAlignment="center"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest 
xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.q4_implicit_intent">

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

            <intent-filter>
                <action android:
                name="android.intent.action.SEND"/>
                <category android:
                name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain"/>
            </intent-filter>
        </activity>
    </application>

</manifest>
package com.example.q4_implicit_intent;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void share(View v){
        EditText editText = 
        (EditText)findViewById(R.id.editText);
        
        Button btn = (Button)findViewById(R.id.button);
        String str = editText.getText().toString();
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT,str);
        startActivity(Intent.createChooser(intent,"Share"));

    }
}

Output

Download to Run and Test Locally

Q4_Implicit_Intent.apk
Browse Source Code
Studio Screenshot (Main Activity) (1/6)
App Screenshot (Main Activity) (2/6)
Studio Screenshot Entering String Input (3/6)
App Screenshot Entering String Input (4/6)
Studio Screenshot on Tapping Send Button (5/6)
App Screenshot on Tapping Send Button (6/6)