0% found this document useful (0 votes)
13 views13 pages

Mad2 2

MAD experiment 2.2 Chandigarh University

Uploaded by

aryan20031112
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)
13 views13 pages

Mad2 2

MAD experiment 2.2 Chandigarh University

Uploaded by

aryan20031112
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/ 13

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 2.2
Student Name: Yash Jain UID: 21BCS11823
Branch: CSE Section/Group: FL-602-A
Semester: 6th Date of Performance:21-02-2024
Subject Name: Mobile Application Development with Lab
Subject Code:21CSH-355

Aim: Create an Android App using various controls such TexEdit, CheckBox, RadioButton,
RadioGroup, etc.

Objective: To create a user interface that involves user input, selection, and interaction with
different types of controls. This type of app aims to showcase the usage and functionalities of
these UI elements to enhance the user experience.

Introduction:-
 Radio Buttons: RadioButton is a UI element that allows users to select one option from a
set of mutually exclusive options.
 Edit Text: Edit Text is a user interface element that allows users to input text data. It is a
subclass of the TextView class and provides a field where users can enter and
edit text.
 CheckBox: In Android, a CheckBox is a UI element that represents a two-state selection
control. It allows users to toggle between checked (selected) and unchecked
(deselected) states.
 RadioGroup: In Android, a RadioGroup is a layout manager used to group RadioButton
widgets. RadioGroup is used when you want to present users with a set of
mutually exclusive options, meaning that only one option can be selected at a
time.

Script and Output:

 Create an app using sample configurations of the buttons.


 Car comparison application with CheckBox, RadioButton, RadioGroup in android with java
and XML code
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Step 1: Select Empty Views Activity and create a new project.


Step 2: Add the add button to the layout.

Step 3: Create the checkbox using default configuration.


1. Create a sample checkbox

Step 1:- Create a new project and select an empty view activity as we have done previous
activities.

Step 2:- Design the layout. This is where you design your app's layout using XML.

Step 3:- Write control in mainactivity.java.

Step 4:- Open the emulator to see how it works.

SCRIPT

<?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"
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List of Subjects"
android:textSize="30dp"
app:layout_constraintBottom_toTopOf="@+id/checkBox"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

</TextView>

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.834" />

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAD Lab"
app:layout_constraintBottom_toTopOf="@+id/checkBox2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.359"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.94" />

<CheckBox
android:id="@+id/checkBox2"

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Project Learning Java"
app:layout_constraintBottom_toTopOf="@+id/checkBox3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.483"
app:layout_constraintStart_toStartOf="parent" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Theory of Computation"
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
app:layout_constraintBottom_toTopOf="@+id/checkBox4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Cloud Computing"
app:layout_constraintBottom_toTopOf="@+id/checkBox5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.43"
app:layout_constraintStart_toStartOf="parent" />

<CheckBox
android:id="@+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="28dp"
android:text="Quantum Mech."
app:layout_constraintBottom_toTopOf="@+id/btn1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.415"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java Code
package com.example.exp5;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


Button btn1;
CheckBox ch,ch1,ch2,ch3,ch4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ch=(CheckBox)findViewById(R.id.checkBox);
ch1=(CheckBox)findViewById(R.id.checkBox2);
ch2=(CheckBox)findViewById(R.id.checkBox3);
ch3=(CheckBox)findViewById(R.id.checkBox4);
ch4=(CheckBox)findViewById(R.id.checkBox5);

btn1 = findViewById(R.id.btn1);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StringBuilder msgBuilder = new StringBuilder("Subjects: ");
if(ch.isChecked())
msgBuilder.append(" MAD ");
if(ch1.isChecked())
msgBuilder.append(" Java ");
if(ch2.isChecked())
msgBuilder.append(" TOC ");
if(ch3.isChecked())
msgBuilder.append(" CC ");
if(ch4.isChecked())
msgBuilder.append(" Quantum Mech. ");
Toast.makeText(getApplicationContext(), msgBuilder, Toast.LENGTH_LONG).show();

}
});
}

OUTPUT
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Create a Car Buying application


Script:-
Main Activity
<?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">

<ImageButton
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="129dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.477"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:srcCompat="@drawable/images" />

<ImageButton
android:id="@+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="129dp"
android:layout_marginTop="28dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.48"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:srcCompat="@drawable/mini" />

<ImageButton
android:id="@+id/imageButton3"
android:layout_width="match_parent"
android:layout_height="129dp"
app:layout_constraintBottom_toTopOf="@+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"
app:srcCompat="@drawable/suv" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CarsAreYours"
android:textSize="20dp"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.16" />
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="200dp"
android:text="Sedan"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="192dp"
android:text="Mini"
app:layout_constraintBottom_toTopOf="@+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="48dp"
android:text="SUV"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

JAVA code
package com.example.carproject;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {


ImageButton i,i2,i3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

i = findViewById(R.id.imageButton);
i2 = findViewById(R.id.imageButton2);
i3 = findViewById(R.id.imageButton3);

i.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,sedanActivity.class);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
startActivity(intent);
}
});

}
}

Activity_sedan.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"
android:background="@color/material_dynamic_primary95"
tools:context=".sedanActivity">

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose your Sedan"
android:textSize="25dp"
app:layout_constraintBottom_toTopOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="150dp"
android:layout_height="109dp"
android:layout_marginStart="12dp"
android:layout_marginTop="80dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/radioButton"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/lexussedan" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="150dp"
android:layout_height="109dp"
android:layout_marginTop="36dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.543"
app:layout_constraintStart_toEndOf="@+id/radioButton3"
app:layout_constraintTop_toBottomOf="@+id/imageView"
app:srcCompat="@drawable/hondased" />

<ImageView
android:id="@+id/imageView3"
android:layout_width="150dp"
android:layout_height="109dp"
android:layout_marginTop="36dp"
app:layout_constraintEnd_toEndOf="parent"
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
app:layout_constraintHorizontal_bias="0.565"
app:layout_constraintStart_toEndOf="@+id/radioButton2"
app:layout_constraintTop_toBottomOf="@+id/imageView2"
app:srcCompat="@drawable/volvosedan" />

<ImageView
android:id="@+id/imageView4"
android:layout_width="150dp"
android:layout_height="109dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toEndOf="@+id/radioButton4"
app:layout_constraintTop_toBottomOf="@+id/imageView3"
app:layout_constraintVertical_bias="0.29"
app:srcCompat="@drawable/images" />

<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Book"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView4" />

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="577dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="2dp" />
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lexus"
app:layout_constraintBottom_toTopOf="@+id/radioButton3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.127"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.558" />

<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="124dp"
android:text="NewGen"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.132"
app:layout_constraintStart_toStartOf="parent" />

<RadioButton
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="108dp"
android:text="Volvo"
app:layout_constraintBottom_toTopOf="@+id/radioButton4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.127"
app:layout_constraintStart_toStartOf="parent" />

<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:text="Honda"
app:layout_constraintBottom_toTopOf="@+id/radioButton2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.127"
app:layout_constraintStart_toStartOf="parent" />
/>
</androidx.constraintlayout.widget.ConstraintLayout>

Java Code
package com.example.carproject;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class sedanActivity extends AppCompatActivity {


Button sub;
RadioButton r,r2,r3,r4;
String carSelected;
// RadioGroup group;

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

sub = findViewById(R.id.submitButton);
r = findViewById(R.id.radioButton);
r2 = findViewById(R.id.radioButton2);
r3= findViewById(R.id.radioButton3);
r4 = findViewById(R.id.radioButton4);
// group = findViewById(R.id.radioGroup);

sub.setOnClickListener(new View.OnClickListener() {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
@Override
public void onClick(View view) {
if (r.isChecked()) carSelected = r.getText().toString();
else if(r2.isChecked()) carSelected = r2.getText().toString();
else if(r3.isChecked()) carSelected = r3.getText().toString();
else if(r4.isChecked()) carSelected = r4.getText().toString();

Toast.makeText(sedanActivity.this, carSelected +"is selected", Toast.LENGTH_SHORT).show();

}
});

}
}
Comparison.java

Activity_comparison.xml
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:-
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Learning Outcomes:
 Learnt about EditText, CheckBox, RadioButton, and RadioGroup.
 Learnt about the working ofEditText, CheckBox, RadioButton, and RadioGroup.
 Learnt how to create an Android app with various controls like EditText, CheckBox,
RadioButton, and RadioGroup.

You might also like