0% found this document useful (0 votes)
6 views37 pages

Unit 4

The document covers various aspects of Android UI components, including attributes of check boxes, text views, and image buttons. It provides syntax examples for creating these components, along with programs demonstrating date and time pickers, temperature conversion, progress bars, and list/grid views. Additionally, it discusses the GridView and its attributes, highlighting its functionality in displaying items in a two-dimensional grid.

Uploaded by

ombhaltilak2006
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)
6 views37 pages

Unit 4

The document covers various aspects of Android UI components, including attributes of check boxes, text views, and image buttons. It provides syntax examples for creating these components, along with programs demonstrating date and time pickers, temperature conversion, progress bars, and list/grid views. Additionally, it discusses the GridView and its attributes, highlighting its functionality in displaying items in a two-dimensional grid.

Uploaded by

ombhaltilak2006
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/ 37

UNIT 4

Summer 2️02️2️
1. List any four attributes of check box. (2️marks)
Ans. 1)id 2️)checked 3)gravity 4)text 5)text color

2️. State syntax to create Text View and Image button with any two attributes of each.
(4marks)
Ans. Text View:
Syntax :
<TextView
android:id="@+id/textView1"
android:layout_width="<width value>”
android:layout_height="<height_value>"
android:text="<text to be displayed>"/>
Attributes/Properties of TextView:
● id: Supply an identifier name of this view, to later retrieve it with View.findViewByID()
or Activity.findViewById()
● alpha: alpha property of the view as a value between 0 (entirely transparent) and
1(Completely Opaque). [flag]
● auto link: Controls whether links such as urls and email addresses are automatically
found and converted to clickable links.[flag]
● gravity: The gravity attribute is an optional attribute which is used to control the
alignment of the text like left, right, center, top, bottom, center_vertical,
center_horizontal etc
● text: text attribute is used to set the text in a text view. We can set the text in xml as
well as in the java class.
● textColor: textColor attribute is used to set the text color of a text view. Color value is
in the form of “#argb”, “#rgb”, “#rrggbb”, or “#aarrggbb”.
● textSize: textSize attribute is used to set the size of text of a text view. We can set the
text size in sp(scale independent pixel) or dp(density pixel).

ImageButton:
Syntax :
<ImageButton
android:id="@+id/imageButton"
android:layout_width="<width value>"
android:layout_height="<height value>"
app:srcCompat="<image source from drawable folder "/>
Attributes/Properties of ImageButton:
● padding: padding attribute is used to set the padding from left, right, top or bottom of
the ImageButton.
● id: id is an attribute used to uniquely identify a image button. Below is the example
code in which we set the id of a image button.
● src: src is an attribute used to set a source file of image or you can say image in your
image button to make your layout look attractive.
● background: background attribute is used to set the background of an image button.
We can set a color or a drawable in the background of a Button.

3. Write a program to demonstrate Date and Time picker. (4marks)


Ans.
Activity_main.xml
<?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:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:id="@+id/t1"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="Select Date"/>

<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dpk1"
android:datePickerMode="calendar"/>

<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:id="@+id/t2️"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b2️"
android:text="Select Time"/>

<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tpk1"
android:timePickerMode="clock" />

</LinearLayout>
MainActivity.java
package com.example.slip2️0;
import …
public class MainActivity extends AppCompatActivity {
TextView t1,t2️;
Button b1,b2️;
TimePicker tpk1;
DatePicker dpk1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=findViewById(R.id.t1);
t2️=findViewById(R.id.t2️);
b1=findViewById(R.id.b1);
b2️=findViewById(R.id.b2️);
dpk1=findViewById(R.id.dpk1);
tpk1=findViewById(R.id.tpk1);

b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String
date=dpk1.getDayOfMonth()+"/"+dpk1.getMonth()+1+"/"+dpk1.getYear();
t1.setText(date);
}
});
b2️.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String time=tpk1.getCurrentHour()+":"+tpk1.getCurrentMinute();
t2️.setText(time);
}
});
}
}
4. Write a program to convert temperature from celcius to farenhite and vice versa using
Toggle button. (Design UI as per your choice. Write XML and java file) (6marks)
Ans.
<?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">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/e1"
android:hint="Enter the temp"/>

<ToggleButton
android:id="@+id/tb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" "
android:textOff="F to C"
android:textOn="C to F" />

<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convert" />
</LinearLayout>

MainActivity.java
package com.example.p1;
import...
public class MainActivity extends AppCompatActivity {
Button b1;
EditText et;
ToggleButton tb;
Double a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=findViewById(R.id.e1);
b1=findViewById(R.id.b1);
tb=findViewById(R.id.tb1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(tb.isChecked())
{
a=Double.parseDouble(String.valueOf(et.getText()));
Double b=a*9/5+32️;
String r=String.valueOf(b);
Toast.makeText(MainActivity.this,r+"°F",Toast.LENGTH_SHORT).show();
}
else
{
a=Double.parseDouble(String.valueOf(et.getText()));
Double b=a-32️*5/9;
String r=String.valueOf(b);

Toast.makeText(MainActivity.this,r+"°C",Toast.LENGTH_SHORT).show();
}
}
});
}
}
Winter 2️02️2️
1. Enlist the elements of UI.. (2️marks)

Ans. • EditText • TextView • ListView • GridView • ScrollView • ImageView • ToggleButton


2️. Explain data and time picker with its method. (4marks)
Ans.
Date Picker:
In Android, DatePicker is a widget used to select a date. It allows to select date by day, month
and year in our custom UI (user interface). If we need to show this view as a dialog then we
have to use a DatePickerDialog class.
Methods of DatePicker:
1.getDayOfMonth(): This method is used to get the selected day of the month from a date
picker.
This method returns an integer value.
DatePicker simpleDatePicker = (DatePicker) findViewById(R.id.simpleDatePicker);
int day = simpleDatePicker.getDayOfMonth();
2️.getMonth(): This method is used to get the selected month from a date picker. This method
returns an integer value.
DatePicker simpleDatePicker = (DatePicker)findViewById(R.id.simpleDatePicker);
int month = simpleDatePicker.getMonth();
3. getYear(): This method is used to get the selected year from a date picker. This method
returns an integer value.
DatePicker simpleDatePicker = (DatePicker)findViewById(R.id.simpleDatePicker);
int year = simpleDatePicker.getYear();

TimePicker:
In Android, TimePicker is a widget used for selecting the time of the day in either AM/PM
mode or 2️4 hours mode. The displayed time consist of hours, minutes and clock format. If we
need to show this view as a Dialog then we have to use a TimePickerDialog class.
Methods of TimePicker:
1. setCurrentHour(Integer currentHour): This method is used to set the current hours in a time
picker.
setHour(Integer hour): setCurrentHour() method was deprecated in API level 2️3. From api level
2️3 we have to use setHour(Integer hour). In this method there is only one parameter of integer
type which is used to set the value for hours.
TimePicker simpleTimePicker=(TimePicker)findViewById(R.id.simpleTimePicker);
simpleTimePicker.setCurrentHour(5);
simpleTimePicker.setHour(5);
2️. setCurrentMinute(Integer currentMinute): This method is used to set the current minutes in
a time picker. setMinute(Integer minute): setCurrentMinute() method was deprecated in API
level 2️3. From api level 2️3 we have to use setMinute(Integer minute). In this method there is
only one parameter of integer type which set the value for minutes.
TimePicker simpleTimePicker=(TimePicker)findViewById(R.id.simpleTimePicker);
simpleTimePicker.setCurrentMinute(35);
simpleTimePicker.setMinute(35);

3. Write a program to display a rectangular progress bar. (4marks)


Ans.
Activity_main.xml
<?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:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="Download"/>

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pb1"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:progress="50"
android:visibility="invisible"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/t1"/>

</LinearLayout>
MainActivity.java
package com.example.progressbar;
import …
public class MainActivity extends AppCompatActivity {
TextView t1;
Button b1;
ProgressBar pb1;
int status=0;
Handler h=new Handler();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=findViewById(R.id.t1);
b1=findViewById(R.id.b1);
pb1=findViewById(R.id.pb1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
while (status<100){
status++;
try{
Thread.sleep(100);
}catch (InterruptedException e){
e.printStackTrace();
}
h.post(new Runnable() {
public void run() {
pb1.setProgress(status);
t1.setText(status);
}
});
}
}
}) .start();
}
});
}
}
4. Develop a program to implement (6marks) i) List view of 5 items ii) Grid view of 4 x 4 items
iii) Image view.
Ans.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<ListView
android:id="@+id/sample_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

<GridView
android:id="@+id/gridview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="50dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>

<ImageView
android:id="@+id/full_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/android_logo" />

</LinearLayout>

package in.msbte.controls_exam_ques;
import ...

public class MainActivity extends AppCompatActivity {


String[] sampleArray = {"Item 1","Item 2️","Item 3","Item 4", "Item 5"};
GridView gridView;
static final String[] alphabets = new String[]{
"A", "B", "C", "D", "E",
"F", "G", "H", "I", "J",
"K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z"
};
ArrayAdapter adapter, adapter1;

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

adapter = new ArrayAdapter<String>(this, R.layout.simple_item, sampleArray);


ListView listView = (ListView) findViewById(R.id.sample_list);
listView.setAdapter(adapter);

gridView = (GridView) findViewById(R.id.gridview1);


adapter1 = new ArrayAdapter<String>(this, R.layout.simple_item, alphabets);
gridView.setAdapter(adapter1);
}
}
Summer 2️02️3️
1. Name any four attributes of Edit Text control. (2️marks)
Ans.
i. android:id
ii. android: gravity
iii. android: text
iv. android: hint
v. android: textColor
vi. android: textSize
vii. android: textStyle
viii. android: background
2️. Explain Gridview with its attributes with suitable example. (4marks)
Ans.
GridView :
Android GridView shows items in two-dimensional scrolling grid (rows & columns) and the grid
items are not necessarily predetermined but they automatically inserted to the layout using a
ListAdapter.
GridView Attributes
Following are the important attributes specific to GridView –

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView 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:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

</GridView>

Activity_main2️.xml
<?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="horizontal">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"/>

</LinearLayout>

MainActivity.java
package com.example.slip15;
import …
public class MainActivity extends AppCompatActivity {
GridView g1;
String arr[]= new String[15];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
g1=findViewById(R.id.main);
for (int i=0;i<15;i++){
arr[i]=Integer.toString(i+1);
}
ArrayAdapter <String> adapter= new
ArrayAdapter<String>(this,R.layout.activity_main2️,R.id.btn,arr);
g1.setAdapter(adapter);
}
}

3. Develop an android application for Date and Time Picker. (4marks)


Ans. Same as Q3 2️02️2️ Summer
4. Design an employee registration form using UI component. (6marks)
Ans.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/e1"
android:layout_width="wrap_cnontent"
android:layout_height="wrap_content"
android:hint="ID"/>

<EditText
android:id="@+id/e2️"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name"/>

<EditText
android:id="@+id/e3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Mobile No."/>

<EditText
android:id="@+id/e4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Address"/>
<EditText
android:id="@+id/e5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Pin Code"/>

<Button
android:id="@+id/b1"
android:text="Submit Details"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>
Winter 2️02️3️
1. Write difference between toggle button and radio button. (2️marks)
Ans.

2️. Develop an application to display analog Time Picker. Also display the selected time. (Write
only . java file). (4marks)
Ans. Same as Q3 Summer 2️02️2️
3. Develop an android application using radio button (4marks)
Ans.
Activity_main.xml
<?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:id="@+id/main"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="Male"/>

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b2️"
android:text="Female"/>

</RadioGroup>

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

</LinearLayout>

MainActivity.java
package com.example.p3;
import …
public class MainActivity extends AppCompatActivity {
RadioButton b1,b2️,b3,b4;
Button btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=findViewById(R.id.b1);
b2️=findViewById(R.id.b2️);
btn=findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String str="";
if(b1.isChecked()){
str=str+b1.getText();
}
if(b2️.isChecked()){
str=str+b2️.getText();
}
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_LONG).show();
}
});
}
}
4. Develop a program to perform addition, subtraction, division, multiplication of two numbers
and display the result. (Use appropriate UI controls) (6marks)
Ans.
Activity_main.xml
<?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:id="@+id/main"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/e1"
android:hint="Enter 1st Number"/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/e2️"
android:hint="Enter 2️nd Number"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="Addition"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b2️"
android:text="Subtraction"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b3"
android:text="Multiplication"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b4"
android:text="Division"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/t1"/>

</LinearLayout>
MainActivity.java
package com.example.p3;
import …
public class MainActivity extends AppCompatActivity {
EditText e1,e2️;
Button b1,b2️,b3,b4;
TextView t1;
int no1,no2️,result;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=findViewById(R.id.e1);
e2️=findViewById(R.id.e2️);
b1=findViewById(R.id.b1);
t1=findViewById(R.id.t1);
b2️=findViewById(R.id.b2️);
b3=findViewById(R.id.b3);
b4=findViewById(R.id.b4);

b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
no1=Integer.parseInt(e1.getText().toString());
no2️=Integer.parseInt(e2️.getText().toString());
result=no1+no2️;
t1.setText("Result="+Integer.toString(result));
}
});

b2️.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
no1=Integer.parseInt(e1.getText().toString());
no2️=Integer.parseInt(e2️.getText().toString());
result=no1-no2️;
t1.setText("Result="+Integer.toString(result));
}
});

b3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
no1=Integer.parseInt(e1.getText().toString());
no2️=Integer.parseInt(e2️.getText().toString());
result=no1*no2️;
t1.setText("Result="+Integer.toString(result));
}
});

b4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
no1=Integer.parseInt(e1.getText().toString());
no2️=Integer.parseInt(e2️.getText().toString());
result=no1/no2️;
t1.setText("Result="+Integer.toString(result));
}
});
}
}
Summer 2️02️4
1. List any two attributes of Toggle Button. (2️marks)
Ans.
1. android:textOff
2️. android:textOn
3. android:id
4. android:checked
5. android:gravity
6. android:textColor

2️. Explain scrollview with its attributes and with suitable example. (4marks)
Ans.
ScrollView
ScrollView is used to scroll the child elements of palette inside ScrollView. Android supports
vertical scroll view as default scroll view. Vertical ScrollView scrolls elements vertically.Android
uses HorizontalScrollView for horizontal ScrollView.

Attributes of ScrollView
• android:fillViewport - Defines whether the scrollview should stretch its content to fill the
viewport.
• android:scrollbars - scrollbars attribute is used to show the scrollbars in horizontal or vertical
direction
• android:layout_width – Define the width
• android:layout_height – Define height
• android:id – Define id

Activity_main.xml
<?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:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ScrollView
android:id="@+id/sv1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/t1"
android:text="pcp college.............."/> // Place a paragraph here
</ScrollView>
</LinearLayout>

3. Write a program to show five checkboxes and toast selected checkboxes using linear layout.
(6marks)
Ans.
activity_main.xml
<?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">

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/c1"
android:text="Swimmning" />

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/c2️"
android:text="Running "/>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/c3"
android:text="Cycling "/>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/c4"
android:text="Reading " />

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/c5"
android:text="Football " />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/b1" />

</LinearLayout>

MainActivity.java

package com.example.checkbox;
import ...
public class MainActivity extends AppCompatActivity {
Button b1;
CheckBox c1,c2️,c3,c4,c5;
String s;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s ="Your Hobbies are:- ";
b1=(Button) findViewById(R.id.b1);
c1=(CheckBox) findViewById(R.id.c1);
c2️=(CheckBox) findViewById(R.id.c2️);
c3=(CheckBox) findViewById(R.id.c3);
c4=(CheckBox) findViewById(R.id.c4);
c5=(CheckBox) findViewById(R.id.c5);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { if(c1.isChecked())
{
s = s + c1.getText()+" ";
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
}
if(c2️.isChecked())
{
s = s + c2️.getText()+" ";
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
}
if(c3.isChecked())
{
s = s + c3.getText()+" ";
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
}
if(c4.isChecked())
{
s = s + c4.getText()+" ";
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
}
if(c5.isChecked())
{
s = s + c5.getText()+" ";
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
}
}
});
}
}
4. Develop android application to enter one number and display factorial of a number once
click on button. (6marks)
Ans.
<?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">

<EditText
android:layout_width="wrap_content"
android:layout_height=" wrap_content "
android:hint="Enter a number"
android:id="@+id/number" />

<Button
android:layout_width=" wrap_content "
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:text="Calculate Factorial" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv" />

</LinearLayout>

MainActivity.java
package com.example.factorial;
import ...
public class MainActivity extends AppCompatActivity {
EditText number;
Button btn1;
TextView tv;
int num;
int factor;
String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number=(EditText)findViewById(R.id.number);
btn1=(Button)findViewById(R.id.btn1);
tv=(TextView)findViewById(R.id.tv);
btn1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
String strnum;
strnum = number.getText().toString();
num = Integer.parseInt(strnum);
int i = 1;
factor = i;
while (i <= num)
{
factor = factor * i;
i++;
}
s = ”Factorial of Number is : ”+ factor;
tv.setText(s);
}
});
}
Winter 2️02️4
1. List attributes of radio button. (2️marks)
Ans. Same as Q1 Summer 2️02️2️
2️. Design a student registration form . (4marks)
Ans.
<?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"
android:padding="16dp">

<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Full Name" />

<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Email"/>

<RadioGroup
android:id="@+id/radioGroupGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"

<RadioButton
android:id="@+id/rbMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"/>

<RadioButton
android:id="@+id/rbFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"/>
</RadioGroup>

<Button
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"/>
</LinearLayout>

3. Develop a program to implement – List View of 6 items. (4marks)


Ans.
<?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"
android:padding="16dp">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Java Code (MainActivity.java)


package com.example.listviewdemo;
import ...
public class MainActivity extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.listView);
String[] items = {"Item 1", "Item 2️", "Item 3", "Item 4", "Item 5", "Item 6"};
ArrayAdapter<String> adapter = new ArrayAdapter<>( this,
android.R.layout.simple_list_item_1,items );
listView.setAdapter(adapter);
}
}
4. Explain Grid view and image view with suitable example. (6marks)
Ans.
Grid View same as Q2️ Summer 2️02️3
Image View:
Image view or a view group which has both an image and some text.
Example:
<ImageView
android:id="@+id/simpleImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/lion" />

5. Explain data picker with suitable example. (6marks)


Ans. Same as Q2️ Winter 2️02️2️

You might also like