Activity - Main - XML Mainactivity - Java
Activity - Main - XML Mainactivity - Java
Activity - Main - XML Mainactivity - Java
java
<?xml version="1.0" encoding="utf-8"?> package com.example.myapplication;// InputActivity.java
<RelativeLayout import android.content.Intent;
xmlns:android="http://schemas. import android.os.Bundle;
android.com/apk/res/android" import android.view.View;
xmlns:tools="http://schemas. import android.widget.Button;
android.com/tools" import android.widget.EditText;
android:layout_width="match_parent"
android:layout_height="match_parent" import androidx.appcompat.app.AppCompatActivity;
tools:context=".MainActivity">
public class MainActivity extends AppCompatActivity {
<EditText
android:id="@+id/editTextName" @Override
android:layout_width="wrap_content" protected void onCreate(Bundle savedInstanceState) {
android:layout_height="wrap_content" super.onCreate(savedInstanceState);
android:hint="Enter your name" setContentView(R.layout.activity_main);
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" /> final EditText nameInput =
findViewById(R.id.editTextName);
<Button Button submitButton =
android:id="@+id/buttonSubmit" findViewById(R.id.buttonSubmit);
android:layout_width="wrap_content"
android:layout_height="wrap_content" submitButton.setOnClickListener(new
android:text="Submit" View.OnClickListener() {
@Override
android:layout_below="@id/editTextName" public void onClick(View v) {
android:layout_centerHorizontal="true" String name = nameInput.getText().toString();
android:layout_marginTop="16dp" /> Intent intent = new Intent(MainActivity.this,
DisplayActivity.class);
</RelativeLayout> intent.putExtra("NAME", name);
startActivity(intent);
}
});
}
}
Activity_display.xml DisplayActivity.java
<?xml version="1.0" encoding="utf-8"?> package com.example.myapplication;// DisplayActivity.java
<RelativeLayout import android.os.Bundle;
xmlns:android="http://schemas. import android.widget.TextView;
android.com/apk/res/android"
xmlns:tools="http://schemas. import androidx.appcompat.app.AppCompatActivity;
android.com/tools"
android:layout_width="match_parent" public class DisplayActivity extends AppCompatActivity {
android:layout_height="match_parent"
tools:context=".DisplayActivity"> @Override
protected void onCreate(Bundle savedInstanceState) {
<TextView super.onCreate(savedInstanceState);
android:id="@+id/textViewName" setContentView(R.layout.activity_display);
android:layout_width="wrap_content"
android:layout_height="wrap_content" TextView nameTextView =
android:textSize="24sp" findViewById(R.id.textViewName);
android:layout_centerInParent="true"/>
// Retrieve the name from the intent
</RelativeLayout> String name = getIntent().getStringExtra("NAME");