Time Picker in Android

Hello Guys !!


In Android it’s very easy to set the time using the android.widget.TimePickercomponent. In this tutorial we are going to see how the user can select the hour, and the minute using the android.app.TimePickerDialog which is an easy to use dialog box.

Android comes equipped with a widget named TimePicker which we can integrate in our application to provide this functionality to the end users. It has an elegant look and design. So, lets get started..


1. TimePickerActivity.java


package com.example.timepicker;

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TimePicker;

public class TimePickerActivity extends Activity implements OnClickListener {
private ImageButton mImgBT;
private Calendar calendar;
private int hour;
private int min;
private EditText TimeET;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timepicker);
mImgBT = (ImageButton) findViewById(R.id.clockImgBT);
calendar = Calendar.getInstance();
hour = calendar.get(Calendar.HOUR_OF_DAY);
min = calendar.get(Calendar.MINUTE);
 
TimeET = (EditText) findViewById(R.id.time_ET);
mImgBT.setOnClickListener(this);
}

@Override
public void onClick(View v) {
showDialog(0);

}

@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
return new TimePickerDialog(this, timePickerListener, hour, min, false);
}

private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
int hour;
String mAM_PM;
if (hourOfDay > 12) {
hour = hourOfDay - 12;
mAM_PM = "PM";
} else {
hour = hourOfDay;
mAM_PM = "AM";
}
TimeET.setText(hour + " : " + minute + " " + mAM_PM);
}
};

}


2. activity_timepicker.xml

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

    <EditText
        android:id="@+id/time_ET"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="50dp"
        android:editable="false" >
    </EditText>

    <ImageButton
        android:id="@+id/clockImgBT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/time_ET"
        android:layout_toRightOf="@+id/time_ET"
        android:contentDescription="Pick time"
        android:cropToPadding="true"
        android:src="@drawable/one" />


</RelativeLayout>


Here is the output : 






Happy Coding !!

Comments

Popular posts from this blog

Carousel view in Android

Get Phone Contacts and display in Listview in Android

Draw Pie Chart using Canvas and Third party Library in Android