Showing posts with label Custom Time Picker with Time Interval of 15 minute.. Show all posts
Showing posts with label Custom Time Picker with Time Interval of 15 minute.. Show all posts

Friday, September 27, 2013

Custom Time Picker with Time Interval of 15 minute.

Android it�s very easy to set the time using the android.widget.TimePicker component. 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 the dialog box.

In this tutorial I am creating the custom Time Picker with minute interval 00-15-30-45.

So lets start the coding to create the custom time picker in android.



activity_main.xml











MainActivity.java

package com.example.customtimepicker;

import java.util.Calendar;

import android.app.Activity;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TimePicker;

public class MainActivity extends Activity implements OnClickListener{

private EditText txt_time=null;
private Button btn_time=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txt_time=(EditText)findViewById(R.id.editText_time);
btn_time=(Button)findViewById(R.id.button_time);
btn_time.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
CustomTimePickerDialog timePickerDialog = new CustomTimePickerDialog(MainActivity.this, timeSetListener,
Calendar.getInstance().get(Calendar.HOUR),
CustomTimePickerDialog.getRoundedMinute(Calendar.getInstance().get(Calendar.MINUTE) + CustomTimePickerDialog.TIME_PICKER_INTERVAL), true);
timePickerDialog.setTitle("Set hours and minutes");
timePickerDialog.show();
}

public static class CustomTimePickerDialog extends TimePickerDialog{

public static final int TIME_PICKER_INTERVAL=15;
private boolean mIgnoreEvent=false;

public CustomTimePickerDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView) {
super(context, callBack, hourOfDay, minute, is24HourView);
}

@Override
public void onTimeChanged(TimePicker timePicker, int hourOfDay, int minute) {
super.onTimeChanged(timePicker, hourOfDay, minute);
if (!mIgnoreEvent){
minute = getRoundedMinute(minute);
mIgnoreEvent=true;
timePicker.setCurrentMinute(minute);
mIgnoreEvent=false;
}
}

public static int getRoundedMinute(int minute){
if(minute % TIME_PICKER_INTERVAL != 0){
int minuteFloor = minute - (minute % TIME_PICKER_INTERVAL);
minute = minuteFloor + (minute == minuteFloor + 1 ? TIME_PICKER_INTERVAL : 0);
if (minute == 60) minute=0;
}

return minute;
}
}

private CustomTimePickerDialog.OnTimeSetListener timeSetListener = new CustomTimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
txt_time.setText(String.format("%02d", hourOfDay) + ":" +String.format("%02d", minute));
}
};
}







You can download the source code CustomTimePicker.

Cheers Guys!

 

Copyright @ 2013 Android Developers Tipss.