0% found this document useful (0 votes)
40 views2 pages

Activity - Main - XML Mainactivity - Java

The document contains code and layout files for an Android application with two activities - a main activity with a name input and submit button, and a display activity that receives the name from the main activity's intent and displays it. The main activity gets the user's name on button click and starts the display activity, passing the name in an intent extra.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views2 pages

Activity - Main - XML Mainactivity - Java

The document contains code and layout files for an Android application with two activities - a main activity with a name input and submit button, and a display activity that receives the name from the main activity's intent and displays it. The main activity gets the user's name on button click and starts the display activity, passing the name in an intent extra.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Activity_Main.xml MainActivity.

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");

// Display the name


nameTextView.setText("Your Name Is:"+name);
}
}

You might also like