0% found this document useful (0 votes)
10 views10 pages

Exp 5

Uploaded by

Sidharth Thakur
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)
10 views10 pages

Exp 5

Uploaded by

Sidharth Thakur
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/ 10

DEPARTMENTOF

COMPUTERSCIENCE&ENGI
NEERING
EXPERIMENT–5

STUDENT NAME:
Sidharth Kumar UID: 20BCS1997
BRANCH: BE-CSE SECTION/GROUP: 20BCS-DM-602/B
SEMESTER: 6TH SUBJECT CODE: 20CSP356
SUBJECT NAME: MAD LAB DATE: 06-03-2023

AIM/OVERVIEWOFTHEPRACTICAL:-

Create an Android App using various controls such as;


TexEdit.
CheckBox.
RadioButton.
RadioGroup.

OBJECTIVE:-

Create an Android App using CheckBox.

STEPS:

1. CreateaNewProject
2. Click on the new project then empty activity and specify the android version and
language.
3. After the project has been created then write the code for creating the textbox to
show the message.
4. Write code for the main activity and the xml file.
5. Selectyourdeviceandrunthecode.
6. You can either run on virtual device or a physical device as according to your
preference.
DEPARTMENTOF
COMPUTERSCIENCE&ENGI
NEERING
CODE:-
XML:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayoutxmlns: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="#ffffff"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose your hobbies:"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Painting"
android:layout_marginTop="16dp"
android:textSize="18sp" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reading"
android:layout_marginTop="16dp"
android:textSize="18sp" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Singing"
android:textSize="18sp"
DEPARTMENTOF
COMPUTERSCIENCE&ENGI
NEERING
app:layout_constraintTop_toTopOf="@+id/textView"
tools:layout_editor_absoluteX="382dp" />

<CheckBox
android:id="@+id/checkBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cooking"
android:layout_marginTop="16dp"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="@+id/checkBox"
tools:layout_editor_absoluteX="386dp" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="Check"
android:text="submit" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MAINACTIVITY.JAVA

package com.example.exp5;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


CheckBoxch, ch1, ch2, ch3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Binding MainActivity.java with activity_main.xml file


setContentView(R.layout.activity_main);

// Finding CheckBox by its unique ID


DEPARTMENTOF
COMPUTERSCIENCE&ENGI
NEERING
ch=(CheckBox)findViewById(R.id.checkBox);
ch1=(CheckBox)findViewById(R.id.checkBox2);
ch2=(CheckBox)findViewById(R.id.checkBox3);
ch3=(CheckBox)findViewById(R.id.checkBox4);
}
// This function is invoked when the button is pressed.
public void Check(View v)
{
String msg="";

// Concatenation of the checked options in if

// isChecked() is used to check whether


// the CheckBox is in true state or not.

if(ch.isChecked())
msg = msg + " Painting ";
if(ch1.isChecked())
msg = msg + " Reading ";
if(ch2.isChecked())
msg = msg + " Singing ";
if(ch3.isChecked())
msg = msg + " Cooking ";

// Toast is created to display the


// message using show() method.
Toast.makeText(this, msg + "are selected",
Toast.LENGTH_LONG).show();
}
}
DEPARTMENTOF
COMPUTERSCIENCE&ENGI
NEERING
OUTPUT:
DEPARTMENTOF
COMPUTERSCIENCE&ENGI
NEERING
AIM:

Creating the Application Choosing Options Radio Button:

CODE:
MAINACTIVITY.JAVA

package com.example.exp5;

import androidx.appcompat.app.AppCompatActivity;

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 MainActivity extends AppCompatActivity {

// Define the object for Radio Group,


// Submit and Clear buttons
private RadioGroupradioGroup;
Button submit, clear;

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

// Bind the components to their respective objects


// by assigning their IDs
// with the help of findViewById() method
submit = (Button) findViewById(R.id.submit);
clear = (Button) findViewById(R.id.clear);
radioGroup = (RadioGroup) findViewById(R.id.groupradio);

// Uncheck or reset the radio buttons initially


radioGroup.clearCheck();

// Add the Listener to the RadioGroup


radioGroup.setOnCheckedChangeListener(
new RadioGroup
.OnCheckedChangeListener() {
@Override
DEPARTMENTOF
COMPUTERSCIENCE&ENGI
NEERING
// The flow will come here when
// any of the radio buttons in the radioGroup
// has been clicked

// Check which radio button has been clicked


public void onCheckedChanged(RadioGroup group,
int checkedId) {

// Get the selected Radio Button


RadioButton
radioButton
= (RadioButton) group
.findViewById(checkedId);
}
});

// Add the Listener to the Submit Button


submit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

// When submit button is clicked,


// Ge the Radio Button which is set
// If no Radio Button is set, -1 will be returned
int selectedId = radioGroup.getCheckedRadioButtonId();
if (selectedId == -1) {
Toast.makeText(MainActivity.this,
"No answer has been selected",
Toast.LENGTH_SHORT)
.show();
} else {

RadioButtonradioButton
= (RadioButton) radioGroup
.findViewById(selectedId);

// Now display the value of selected item


// by the Toast message
Toast.makeText(MainActivity.this,
radioButton.getText(),
Toast.LENGTH_SHORT)
.show();
}
}
});
DEPARTMENTOF
COMPUTERSCIENCE&ENGI
NEERING
// Add the Listener to the Submit Button
clear.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

// Clear RadioGroup
// i.e. reset all the Radio Buttons
radioGroup.clearCheck();
}
});
}
}

ACTIVITY_MAIN.XML

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
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">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your Subject ?"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:textSize="20sp"/>

<RadioGroup
android:layout_marginTop="50dp"
android:id="@+id/groupradio"
android:layout_marginLeft="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radia_id1"
android:text="DBMS"
android:textSize="20sp"/>
DEPARTMENTOF
COMPUTERSCIENCE&ENGI
NEERING
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radia_id2"
android:text="C/C++ Programming"
android:textSize="20sp"/>

<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radia_id3"
android:text="Data Structure"
android:textSize="20sp"/>

<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radia_id4"
android:text="Algorithms"
android:textSize="20sp"/>
</RadioGroup>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/submit"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginTop="200dp"
android:layout_marginLeft="180dp"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="@+id/clear"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginTop="200dp"
android:layout_marginLeft="20dp"
/>

</RelativeLayout>
DEPARTMENTOF
COMPUTERSCIENCE&ENGI
NEERING

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy