Practical 11 13
Practical 11 13
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;
@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();
}
}
</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"/>
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;
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.
<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;
@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;
@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);
}
});
}
}
return 100;
}
}
</LinearLayout>