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

Android 4c

Uploaded by

owaisscb127
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)
5 views

Android 4c

Uploaded by

owaisscb127
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/ 5

Owais Shaikh

TYCS B, 8881

Aim: Create an Android app that includes checkboxes and Radio buttons.
activity_main.xml

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


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="horizontal">
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editTextText"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:ems="10"
android:layout_marginLeft="110dp"
android:text="RATE THIS SECTION" />
<RadioButton
android:id="@+id/radioButton"
android:layout_width="94dp"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:text="Excellant" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="87dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="Good" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="Average" />
<RadioButton
android:id="@+id/radioButton5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Owais Shaikh
TYCS B, 8881

android:layout_marginRight="20dp"
android:text="Poor" />
</RadioGroup>
<EditText
android:id="@+id/editTextText2"
android:layout_width="500dp"
android:layout_height="50dp"
android:ems="10"
android:text="GIVE YOUR SUGGESTION HERE"
android:layout_marginTop="240dp"
android:layout_marginLeft="80dp"
android:layout_marginRight="60dp"/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBMIT"
android:layout_marginTop="520dp"
android:layout_marginLeft="150dp"/>

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I really enjoy this Section"
android:layout_marginTop="300dp"
android:layout_marginLeft="50dp"/>

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I will prefer this Section else also"
android:layout_marginTop="340dp"
android:layout_marginLeft="50dp"/>

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I will Like to hear more from you"
android:layout_marginTop="380dp"
android:layout_marginLeft="50dp"/>
Owais Shaikh
TYCS B, 8881

<CheckBox
android:id="@+id/checkBox4"
android:layout_width="280dp"
android:layout_height="60dp"
android:text="I am satisfied with the content and full description"
android:layout_marginTop="420dp"
android:layout_marginLeft="50dp"/>

</RelativeLayout>

MainActivity.java

package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button; import
android.widget.CheckBox; import
android.widget.RadioButton; import
android.widget.RadioGroup; import
android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; public

class MainActivity extends AppCompatActivity {

RadioGroup radioGroup; RadioButton


selectedRadioButton; Button
buttonSubmit;
CheckBox check,check2,check3,check4;

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

buttonSubmit = (Button) findViewById(R.id.button); radioGroup


= (RadioGroup) findViewById(R.id.radiogroup); check=
(CheckBox) findViewById(R.id.checkBox);
check2 = (CheckBox) findViewById(R.id.checkBox2);
check3 = (CheckBox) findViewById(R.id.checkBox3);
check4 = (CheckBox) findViewById(R.id.checkBox4);
Owais Shaikh
TYCS B, 8881

Submit Button
buttonSubmit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
//Get the selected RadioButton selectedRadioButton
= (RadioButton)
findViewById(radioGroup.getCheckedRadioButtonId());
// get RadioButton text
String yourVote = selectedRadioButton.getText().toString(); String

checkBoxChoices = "";

if (check.isChecked()) {
checkBoxChoices += check.getText().toString() + "\tYES";
} else {
checkBoxChoices += check.getText().toString() + "\tNO";
}

if (check2.isChecked()) {
checkBoxChoices += check2.getText().toString() + "\tYES";
} else {
checkBoxChoices += check2.getText().toString() + "\tNO";
}

if (check3.isChecked()) {
checkBoxChoices += check3.getText().toString() + "\tYES";
} else {
checkBoxChoices += check3.getText().toString() + "\tNO";
}

if (check4.isChecked()) {
checkBoxChoices += check4.getText().toString() + "\tYES";
} else {
checkBoxChoices += check4.getText().toString() + "\tNO";
}

// display it as Toast to the user

Toast.makeText(MainActivity.this, "Selected Radio Button is:" + yourVote


+ "\n CheckBox Choices: \n " + checkBoxChoices, Toast.LENGTH_LONG).show();

});}}
Owais Shaikh
TYCS B, 8881

You might also like