0% found this document useful (0 votes)
6 views3 pages

Activity and Intents

The document contains XML layout files and Java code for an Android application with two activities: MainActivity and DisplayNameActivity. The MainActivity includes a button, text view, and edit text for user input, while the DisplayNameActivity displays a greeting message using the entered name. The application uses intents to pass data between activities and employs a constraint layout for UI design.

Uploaded by

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

Activity and Intents

The document contains XML layout files and Java code for an Android application with two activities: MainActivity and DisplayNameActivity. The MainActivity includes a button, text view, and edit text for user input, while the DisplayNameActivity displays a greeting message using the entered name. The application uses intents to pass data between activities and employs a constraint layout for UI design.

Uploaded by

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

Main Activity XML

<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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/btn_toast"
android:layout_width="180dp"
android:layout_height="70dp"
android:text="@string/btn_toast"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.649">

</Button>

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome"
app:layout_constraintBottom_toTopOf="@+id/btn_toast"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/enterName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="@string/enterName"
app:layout_constraintBottom_toTopOf="@+id/btn_toast"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.293" />

</androidx.constraintlayout.widget.ConstraintLayout>

Main Activity .java

public class MainActivity extends AppCompatActivity {


Button btn_next;
EditText name;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v,
insets) -> {
Insets systemBars =
insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right,
systemBars.bottom);
btn_next =findViewById(R.id.btn_toast);
name =findViewById(R.id.enterName1);

btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String enteredName = name.getText().toString();

// Create Intent to start DisplayNameActivity


Intent intent = new Intent(MainActivity.this,
DisplayNameActivity.class);
intent.putExtra("enteredName", enteredName); // Add extra data

// Start DisplayNameActivity
startActivity(intent);
}
});

return insets;

});
}
}

Display .xml

<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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/displayNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias=".6"
app:layout_constraintVertical_bias=".7"
android:textSize="24sp"
android:textStyle="bold" />

</androidx.constraintlayout.widget.ConstraintLayout>

display .java

public class DisplayNameActivity extends AppCompatActivity {


TextView displayName;

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

// Initialize TextView
displayName = findViewById(R.id.displayNameTextView);

// Get extra data from Intent


String enteredName = getIntent().getStringExtra("enteredName");

// Display received data in TextView


displayName.setText("Hello, " + enteredName);
}
}

You might also like