PRACTICAL NO 16
X. Exercise
1. Write a program to display following output. Use TimePicker with Spinnermode.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<TimePicker
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textSize="30sp"
android:layout_gravity="center"
android:gravity="center"
android:layout_below="@+id/t2"/>
</RelativeLayout>
JAVA Code:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
TimePicker t2;
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
t1 = (TextView) findViewById([Link].t1);
t2 = (TimePicker) findViewById([Link].t2);
t2.setIs24HourView(true);
[Link](getCurrentTime());
}
public String getCurrentTime()
{
String currentTime = " Current Time:" +
[Link]() + ":" +[Link]();
return currentTime;
}
}
OUTPUT:
2. Write a program to display following output. Select and display date and time on click
of “select date”, “select time” buttons respectively.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/date1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="23dp"
android:layout_marginLeft="23dp"
android:layout_marginTop="86dp"
android:paddingLeft="100dp" />
<Button
android:id="@+id/date2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/date1"
android:layout_marginStart="-1dp"
android:layout_marginLeft="-1dp"
android:layout_marginBottom="0dp"
android:layout_toEndOf="@+id/date1"
android:layout_toRightOf="@+id/date1"
android:text="SELECT DATE" />
<EditText
android:id="@+id/time1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/date1"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="21dp"
android:layout_marginLeft="21dp"
android:layout_marginTop="1dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT TIME"
android:id="@+id/time2"
android:layout_below="@+id/date2"
android:layout_alignLeft="@+id/date2"
android:layout_alignStart="@+id/date2" />
</RelativeLayout>
JAVA Code:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity implements [Link] {
Button b1, b2;
EditText t1, t2;
private int yr, mn, day, hr, min;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
t1 = (EditText) findViewById([Link].date1);
t2 = (EditText) findViewById([Link].time1);
b1 = (Button) findViewById([Link].date2);
b2 = (Button) findViewById([Link].time2);
[Link](this);
[Link](this);
}
@Override
public void onClick(View v) {
if (v == b1) {
final Calendar c = [Link]();
yr = [Link]([Link]);
mn = [Link]([Link]);
day = [Link](Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new
[Link]() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
[Link](dayOfMonth + "-" + (month + 1) + "-" + year);
}
}, yr, mn, day);
[Link]();
}
if (v == b2) {
final Calendar c = [Link]();
hr = [Link](Calendar.HOUR_OF_DAY);
min = [Link]([Link]);
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new
[Link]() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
[Link](hourOfDay + "-" + minute);
}
}, hr, min, false);
[Link]();
}
}
}
OUTPUT: