0% found this document useful (0 votes)
1K views5 pages

Practical No 16

This document contains code for two Android programming exercises. The first displays the current time using a TimePicker widget. The second allows the user to select a date and time using DatePickerDialog and TimePickerDialog popups, and displays the results in EditText widgets. The code provides XML layouts and Java code to implement the functionality for both exercises.

Uploaded by

Aakash Chaudhari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views5 pages

Practical No 16

This document contains code for two Android programming exercises. The first displays the current time using a TimePicker widget. The second allows the user to select a date and time using DatePickerDialog and TimePickerDialog popups, and displays the results in EditText widgets. The code provides XML layouts and Java code to implement the functionality for both exercises.

Uploaded by

Aakash Chaudhari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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="http://schemas.android.com/apk/res/android"
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 com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends AppCompatActivity {
TimePicker t2;
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (TextView) findViewById(R.id.t1);
t2 = (TimePicker) findViewById(R.id.t2);
t2.setIs24HourView(true);
t1.setText(getCurrentTime());
}
public String getCurrentTime()
{
String currentTime = " Current Time:" +
t2.getCurrentHour() + ":" +t2.getCurrentMinute();
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="http://schemas.android.com/apk/res/android"
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 com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button b1, b2;
EditText t1, t2;
private int yr, mn, day, hr, min;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (EditText) findViewById(R.id.date1);
t2 = (EditText) findViewById(R.id.time1);
b1 = (Button) findViewById(R.id.date2);
b2 = (Button) findViewById(R.id.time2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if (v == b1) {
final Calendar c = Calendar.getInstance();
yr = c.get(Calendar.YEAR);
mn = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new
DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
t1.setText(dayOfMonth + "-" + (month + 1) + "-" + year);
}
}, yr, mn, day);
datePickerDialog.show();
}
if (v == b2) {
final Calendar c = Calendar.getInstance();
hr = c.get(Calendar.HOUR_OF_DAY);
min = c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new
TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
t2.setText(hourOfDay + "-" + minute);
}
}, hr, min, false);
timePickerDialog.show();
}
}
}

OUTPUT:

You might also like