0% found this document useful (0 votes)
2 views

pr16

Practical

Uploaded by

gayatriksh25
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)
2 views

pr16

Practical

Uploaded by

gayatriksh25
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/ 6

ANDROID PRACTICAL NO : 16

<?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">

<TimePicker

android:layout_width="286dp"

android:layout_height="198dp"

android:timePickerMode="spinner" />

<TimePicker

android:id="@+id/timepicker1"

android:layout_width="296dp"

android:layout_height="153dp"

android:timePickerMode="spinner"/>

<TimePicker

android:layout_width="344dp"

android:layout_height="314dp"

android:timePickerMode="clock" />

</LinearLayout>

package com.example.timepickerexample;

import androidx.annotation.RequiresApi;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;

import android.os.Bundle;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity {

TimePicker TP;

protected void onCreate(Bundle b) {

super.onCreate(b);

setContentView(R.layout.activity_main);

TP =findViewById(R.id. timepicker1);

TP.setIs24HourView(true);

}}

PR16_2

<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout

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" tools:context=".MainActivity">

<EditText

android:id="@+id/in_date"

android:layout_width="200dp"

android:layout_height="wrap_content"

android:layout_alignParentStart="true"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"
android:layout_marginTop="82dp"

android:layout_x="26dp" android:layout_y="295dp"

/>

<Button

android:id="@+id/btn_date"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBottom="@+id/in_date"

android:layout_toEndOf="@+id/in_date"

android:layout_toRightOf="@+id/in_date"

android:layout_x="245dp"

android:layout_y="290dp" android:text="SELECT DATE" />

<EditText

android:id="@+id/in_time"

android:layout_width="200dp"

android:layout_height="wrap_content"

android:layout_below="@+id/in_date"

android:layout_alignParentStart="true"

android:layout_alignParentLeft="true"

android:layout_x="28dp" android:layout_y="369dp"/>

<Button

android:id="@+id/btn_time"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/btn_date"

android:layout_alignStart="@+id/btn_date"

android:layout_alignLeft="@+id/btn_date"
android:layout_x="248dp"

android:layout_y="367dp" android:text="SELECT TIME" />

</AbsoluteLayout>

Java Code:

package com.example.timedatepickerdialog;

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 {

Button btnDP,btnTP;

EditText txtDate, txtTime;

private int mYear, mMonth, mDay, mHour, mMinute;

@Override

protected void onCreate(Bundle b) {

super.onCreate(b);

setContentView(R.layout.activity_main);

btnDP=findViewById(R.id.btn_date);

btnTP=findViewById(R.id.btn_time);

txtDate=findViewById(R.id.in_date);
txtTime=findViewById(R.id.in_time);

btnDP.setOnClickListener(this::onClick);

btnTP.setOnClickListener(this::onClick);

public void onClick(View v) {

if (v == btnDP) {

final Calendar c = Calendar.getInstance();

mYear = c.get(Calendar.YEAR);

mMonth =c.get(Calendar.MONTH);

mDay =c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog DPDialog = new DPDialog (this, new DatePickerDialog.OnDateSetListener() {

@Override

public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth)

txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

}, mYear, mMonth, mDay);

DPDialog.show();

if (v ==btnTP) {

final Calendar c = Calendar.getInstance();

mHour = c.get(Calendar.HOUR_OF_DAY);

mMinute= c.get(Calendar.MINUTE);

TimePickerDialog tpDialog = new TimePickerDialog(this,new TimePickerDialog.OnTimeSetListener() {

@Override

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

txtTime.setText(hourOfDay + ":" + minute}


},mHour, mMinute,false);

tpDialog.show();

You might also like