0% found this document useful (0 votes)
60 views

Explicit Intent

The document contains code for an Android application with two activities. The main activity contains an edit text for user input and a submit button. When clicked, the button starts a second activity, passing the user input as an extra in the intent. The second activity receives this extra and displays it in a text view on screen.

Uploaded by

Deelip Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Explicit Intent

The document contains code for an Android application with two activities. The main activity contains an edit text for user input and a submit button. When clicked, the button starts a second activity, passing the user input as an extra in the intent. The second activity receives this extra and displays it in a text view on screen.

Uploaded by

Deelip Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

activity_main.

xml

<?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=".MainActivity">

<EditText
android:id="@+id/txtname"
android:layout_width="287dp"
android:layout_height="55dp"
android:ems="10"
android:hint="Enter Name"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.09" />

<Button
android:id="@+id/btnsubmit"
android:layout_width="156dp"
android:layout_height="53dp"
android:text="Submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.251" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.intentapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

Button submit=findViewById(R.id.btnsubmit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText ename=findViewById(R.id.txtname);
String name=ename.getText().toString();

Intent intent=new Intent(MainActivity.this,


SecondActivity.class);

intent.putExtra("fname",name);
startActivity(intent);
}
});
}
}

SecondActivity.java
<?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=".SecondActivity">

<TextView
android:id="@+id/textView"
android:layout_width="267dp"
android:layout_height="39dp"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.396"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.167" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity_second.xml
package com.example.intentapp;

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

public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView t=findViewById(R.id.textView);

Intent intent=getIntent();
String fname=intent.getStringExtra("fname");
t.setText("Hello "+fname);
}
}

You might also like