0% found this document useful (0 votes)
31 views7 pages

Practical 11 13

The document discusses checkboxes and radio buttons in Android. It provides the XML tags to create checkboxes and radio buttons, lists their attributes and methods. It also includes an example program to display five checkboxes and toast the selected ones, and another example to display radio buttons with and without a radio group and toast the selected option.

Uploaded by

Anish Dawkhar
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)
31 views7 pages

Practical 11 13

The document discusses checkboxes and radio buttons in Android. It provides the XML tags to create checkboxes and radio buttons, lists their attributes and methods. It also includes an example program to display five checkboxes and toast the selected ones, and another example to display radio buttons with and without a radio group and toast the selected option.

Uploaded by

Anish Dawkhar
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/ 7

Practial 11

IX. Practical related questions


1. Name the different methods of checkbox
public boolean isChecked()
public void setChecked(boolean status)
2. List the different attributes of check box
android:id, android:checked, android:onClick, android:text
3. Write xml tag to create checkbox named android
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=”Android"
android:id="@+id/android"/>

X. Exercise
1. write a program to show five checkboxes and toast selected ones
package com.example.myapplication11;

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 implements


View.OnClickListener{
CheckBox c1,c2,c3,c4,c5;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c1 = (CheckBox)findViewById(R.id.football);
c2 = (CheckBox) findViewById(R.id.cricket);
c3 = (CheckBox) findViewById(R.id.badminton);
c4 = (CheckBox) findViewById(R.id.basketBall);
c5 = (CheckBox) findViewById(R.id.volleyball);
c1.setOnClickListener(this);
c2.setOnClickListener(this);
c3.setOnClickListener(this);
c4.setOnClickListener(this);
c5.setOnClickListener(this);
}
public void onClick(View v) {
StringBuffer str = new StringBuffer("Clicked: ");

if(c1.isChecked()){

str.append("Football ");
}
if(c2.isChecked()) {

str.append("Cricket ");
}if(c3.isChecked()) {

str.append("Badminton ");
}
if(c4.isChecked()) {

str.append("Basketball ");
}if(c5.isChecked()) {
str.append("Volley ball ");
}
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();

}
}

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Football"
android:id="@+id/football"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cricket"
android:id="@+id/cricket"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Volleyball"
android:id="@+id/volleyball"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/badminton"
android:text="Badminton"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/basketBall"
android:text="Basketball"/>

</LinearLayout>
Practical 12
IX. Practical related questions
1. Write XML tag to create a radio button
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button1"
android:id="@+id/rad1"/>

2. Write the purpose of radio button


Radio buttons allow the user to select one option from a set .RadioButton are mainly used
together in a RadioGroup. In RadioGroup, checking one radio button out of several
radio buttons added in it will automatically uncheck all the others. It means at a time one
can check only one radio button from a group of radio buttons which belong to same radio
group. RadioButton is a two state button that can be checked or unchecked. If a radio
button is unchecked then a user can check it by simply clicking on it.

3. List the different methods of radio button


getText(), setChecked(boolean b)
X. Exercise
Write a program to show the following output. First two radio buttons are without using radio
button group and next two radio buttons are using radio group. Also use toast which radio button is
selected

package com.example.myapplication12_1;

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 implements View.OnClickListener


{
Button b1;
RadioButton r1;
RadioGroup radGrp;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);
radGrp = (RadioGroup) findViewById(R.id.radioGrp);
b1.setOnClickListener(this);
}
public void onClick(View v) {
StringBuffer str= new StringBuffer("Selected ");
int selected = radGrp.getCheckedRadioButtonId();
if(selected==-1){

str.append("nothing");
}else {
r1 = (RadioButton)findViewById(selected);
str.append(r1.getText().toString());
}
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();

}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Single Radio Buttons"
android:gravity="center_horizontal"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button1"
android:id="@+id/rad1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button1"
android:id="@+id/rad2"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="20dp"
android:background="#B8B894"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Radio Button inside RadioGroup"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioGrp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/male"
android:text="Male"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/female"
android:text="Female"/>

</RadioGroup>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/button1"
android:text="Show Selected"/>

</LinearLayout>

Practical 13
X. Practical related questions
1. state method to update the percentage of progress displayed
setProgress(int)
2. write an xml tag for the determinate progress bar.

3.list the different progress bar styles


determinate and indeterminate
X. Exercise
1. write a program to display circular progress bar
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

<ProgressBar
android:id="@+id/indeterminateBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</LinearLayout>

package com.example.myapplication13_cir;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
2. write a program to display the following output

package com.example.myapplication13_2;

import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener{
Button b1;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
private long fileSize = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(this);
}
public void onClick(View v) {
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("File downloading ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
progressBarStatus = 0;
fileSize = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
progressBarStatus = doOperation();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}

if (progressBarStatus >= 100) {


try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBar.dismiss();
}
}
}).start();
}

public int doOperation() {


while (fileSize <= 10000) {
fileSize++;
if (fileSize == 1000) {
return 10;
} else if (fileSize == 2000) {
return 20;
} else if (fileSize == 3000) {
return 30;
} else if (fileSize == 4000) {
return 40;
}

}
return 100;
}
}

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/b1"
android:text="Download File"/>

</LinearLayout>

You might also like