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

Mat Lab2

The document describes creating an Android application with multiple activities. It includes code snippets to build a login screen with inputs for username, password, address, gender, age and a submit button. On clicking submit, the data is displayed below the button.

Uploaded by

Shreya
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)
8 views

Mat Lab2

The document describes creating an Android application with multiple activities. It includes code snippets to build a login screen with inputs for username, password, address, gender, age and a submit button. On clicking submit, the data is displayed below the button.

Uploaded by

Shreya
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/ 9

MAT LAB-2

1) Create an application that takes the name from a


textbox and shows hello message along with the name
entered in textbox, when the user clicks OK button.

Code for 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/nameEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="Enter your name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
app:layout_constraintTop_toBottomOf="@id/nameEditText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="20dp"
android:onClick="showMessage" />

<TextView
android:id="@+id/greetingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@id/okButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:text=""
android:textSize="18sp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Code for MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

Button okButton = findViewById(R.id.okButton);


final EditText nameEditText = findViewById(R.id.nameEditText);
final TextView greetingTextView =
findViewById(R.id.greetingTextView);

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

if (!name.isEmpty()) {
String message = "Hello, " + name + "!";
greetingTextView.setText(message);
} else {
greetingTextView.setText("Please enter your name.");
}
}
});
}
}

2) Create a screen that has input boxes for Username,


Password, Address, Gender (radio buttons for male and
female), Age and a submit button. On clicking the submit
button, print the data bellow the submit button. (Use
any layout)

Layout: Used to define the user interface that will appear on the screen of an
android application.
Activity: An activity is one page application. An android application contains
multiple activities. Each activity contains multiple user interface components
and those components are called views.

View: A view is defined as the user interface which is used to create


interactive UI components.
Example: TextView, ImageView, EditText, RadioButton etc.

ViewGroup: Multiple views combine to form a ViewGroup.

Types of Android Layouts:

1)Linear Layout: A layout that organizes its children either horizontally or


vertical.

2)Relative Layout: Allows positioning of child elements relative to each


other or the parent.

3)Constraint Layout: A flexible layout that allows creating complex layouts


with a flat view hierarchy.

4)Grid Layout: Organizes children into a grid, where each child can span
multiple rows and columns.

Code for 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/usernameEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="User Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/passwordEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:inputType="textPassword"
android:hint="Password"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/usernameEditText"
/>

<EditText
android:id="@+id/addressEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Address"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/passwordEditText" />

<RadioGroup
android:id="@+id/genderRadioGroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/addressEditText">

<RadioButton
android:id="@+id/maleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />

<RadioButton
android:id="@+id/femaleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>

<EditText
android:id="@+id/ageEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:inputType="number"
android:hint="Age"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/genderRadioGroup"
/>

<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Submit"
app:layout_constraintTop_toBottomOf="@+id/ageEditText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:onClick="onSubmitClicked" />

<TextView
android:id="@+id/displayDataTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/submitButton"
android:text=""
android:textSize="18sp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Code for MainActivity.java:

package com.example.fromsubmission;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


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

final EditText usernameEditText =


findViewById(R.id.usernameEditText);
final EditText passwordEditText =
findViewById(R.id.passwordEditText);
final EditText addressEditText = findViewById(R.id.addressEditText);
final RadioGroup genderRadioGroup =
findViewById(R.id.genderRadioGroup);
final EditText ageEditText = findViewById(R.id.ageEditText);
final TextView displayDataTextView =
findViewById(R.id.displayDataTextView);
Button submitButton = findViewById(R.id.submitButton);

submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
String address = addressEditText.getText().toString();
String gender = getSelectedGender();
String age = ageEditText.getText().toString();

String data = "Username: " + username + "\n" +


"Password: " + password + "\n" +
"Address: " + address + "\n" +
"Gender: " + gender + "\n" +
"Age: " + age;

displayDataTextView.setText(data);
}
});
}
private String getSelectedGender() {
RadioButton maleRadioButton =
findViewById(R.id.maleRadioButton);
RadioButton femaleRadioButton =
findViewById(R.id.femaleRadioButton);

if (maleRadioButton.isChecked()) {
return "Male";
} else if (femaleRadioButton.isChecked()) {
return "Female";
} else {
return "";
}
}
}

You might also like