📝
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 explicit intent to send text to second activity and display there in text view.
  • Code
  • Output
  1. Android

Q3

Question 3

PreviousQ2NextQ4

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 explicit intent to send text to second activity and display there in text view.

App Insights:

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

Code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
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:id="@+id/linearlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/editText"
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:layout_weight="10"
        android:ems="10"
        android:inputType="textPersonName"
        android:paddingLeft="50dp"
        android:paddingTop="50dp"
        android:text="Enter any String" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sendtext"
        android:text="send"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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=".SecondaryActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="376dp"
        android:text="@string/textview"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<resources>
    <string name="app_name">Q3_Explicit_Intent</string>
    <string name="textview">TextView</string>
</resources>
package com.example.q3_explicit_intent;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
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 sendtext(View v) {
        EditText edit =(EditText)findViewById(R.id.editText);
        String str = edit.getText().toString();
        Intent i = new Intent(this,SecondaryActivity.class);

        i.putExtra("abc",str);
        startActivity(i);

    }
}
package com.example.q3_explicit_intent;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SecondaryActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondary);
        TextView text =(TextView)findViewById(R.id.textView);
        Intent intent = getIntent();
        String str = intent.getStringExtra("abc");
        text.setText(str);

    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest 
xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.q3_explicit_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.Q3_Explicit_Intent">
        <activity android:name=".SecondaryActivity">
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action 
                android:name="android.intent.action.MAIN"/>
                <category 
                android:
                name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

Output

Download to Run and Test Locally

Q3_Explicit_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 Displaying Desired Output (Secondary Activity) (5/6)
App Screenshot Displaying Desired Output (Secondary Activity) (6/6)