Practical 14 16
Practical 14 16
X.Exercise
package com.example.myapplication14_1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
</TextView>
2. Write a program to display an image using ImageView and a Button named
as “Change image”. Once you click on button another image should get
displayed
package com.example.myapplication14_2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.myButton);
b1.setOnClickListener(this);
}
public void onClick(View v) {
ImageView im = (ImageView) findViewById(R.id.myImage);
im.setImageResource(R.drawable.img_green);
}
}
<?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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myImage"
android:src="@drawable/img_red"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myButton"
android:text="Change Image"/>
</LinearLayout>
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
</LinearLayout>
/layout/activity_gridview.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</Button>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sc_view">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Text"/>
</ScrollView>
</LinearLayout>
package com.example.myapplication14_4;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Practical 15
IX. Practical related questions
1. List all predefined constants to specify the positioning of the toast. Which
method is used to change the position of a toast on the screen.
setGravity(int gravity, int x, int y) values-Gravity.TOP,Gravity.LEFT
2. List two constants of Toast
LENGTH_LONG
LENGTH_SHORT
X. Exercise
1. write a program to display the following toast message
package com.example.myapplication15_1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
}
}
</LinearLayout>
2. Write a program to display three checkbox and one button named order as
shown below. Once the user click on button it should toast different
selected checkboxes along with items individual and total price
package com.example.myapplication15_2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
@Override
public void onClick(View v) {
StringBuffer str = new StringBuffer("Selected items:\n");
CheckBox p,c,b;
int total = 0;
p = (CheckBox) findViewById(R.id.pizza);
c = (CheckBox) findViewById(R.id.coffee);
b = (CheckBox) findViewById(R.id.burger);
if(p.isChecked()) {
str.append("Pizza : "+250);
total=total+250;
}
if(c.isChecked()) {
str.append("\nCoffee : "+100);
total = total+100;
}
if(b.isChecked()) {
str.append("\nBurger : "+150);
total = total+150;
}
str.append("\nTotal : "+total);
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_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "@+id/pizza"
android:text="Pizza"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/coffee"
android:text="Coffee"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/burger"
android:text="Burger"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "@+id/b1"
android:text="Order"/>
</LinearLayout>
Practical 16
IX. Practical related questions
1. Write an xml TimePicker tag
<TimePicker android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”myTimePicker”
android:timePickerMode=”spinner”/>
2. List and explain all methods of TimePicker class
void autofill(AutofillValue) - Automatically fills the content of this view
with the value
Integer getCurrentHour()returns the current hour
Integer getCurrentMinute()-returns the current minute
int getHour()-Returns the currently selected hour using 24-hour time.
int getMinute()-Returns the currently selected minute
boolean is24HourView()
boolean isEnabled()-Returns the enabled status for this view
void setCurrentHour(Integer)
void setCurrentMinute(Integer)
void setHour()-Sets the currently selected hour using 24-hour time
void setIs24HourView(Boolean)- Sets whether this widget displays time
in 24-hour mode or 12-hour mode with an AM/PM picker.
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TimePicker;
</LinearLayout>
ii.
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="24 Hours"/>
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myTimePicker1"
android:timePickerMode="spinner"/>
</LinearLayout>
package com.example.myapplication16_1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TimePicker;
}
}
iii.
package com.example.myapplication16_1_3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TimePicker t1 = (TimePicker) findViewById(R.id.myTimePicker1);
t.setIs24HourView(true);
}
}
<?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">
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myTimePicker1"
android:timePickerMode="clock"/>
</LinearLayout>
2.Write program to display following output. Select date and time.
package com.example.myapplication16_2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TimePicker;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener{
Button b1,b2;
EditText d,t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
d = (EditText) findViewById(R.id.selectDate);
t = (EditText) findViewById(R.id.selectTime);
b1 = (Button) findViewById(R.id.dateButton);
b2 = (Button) findViewById(R.id.timeButton);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
SimpleDateFormat dfmt = new SimpleDateFormat("dd-MM-yyyy");
String str = dfmt.format(c.getTime());
SimpleDateFormat dfmt1 = new SimpleDateFormat("hh:mm:ss");
String str1 = dfmt1.format(c.getTime());
switch(v.getId()) {
case R.id.dateButton:
d.setText(str);
break;
case R.id.timeButton:
t.setText(str1);
}
}
}
<Button
android:id="@+id/dateButton"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_toRightOf="@+id/selectDate"
android:text="Select Date" />
<Button
android:layout_width="120dp"
android:id="@+id/timeButton"
android:layout_below="@+id/dateButton"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/selectTime"
android:text="Select Time" />
</RelativeLayout>