Time Picker in Android

Just like Number Picker provides a neat way to select values from a given range, Android also provides a Time Picker tool. This, as the name suggest is used to pick time. Now, you can use the Time Picker the way you use Number Picker, but this post shows how to use it with the help of a TimePickerDialog.

  • Create a layout for an activity and insert a button. This button will trigger a dialog which will contain the TimePicker.
  • Define the button in the Java file.
  • Set the onClick() method for the button.[java]
    b.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    showDialog(9999);
    }
    });
    [/java]

  • Create a new method onCreateDialog(). This comes into play when we call a Dialog to appear quite like the one we did above.
    [java]
    protected Dialog onCreateDialog(int id){
    switch(id){
    case 9999:
    return new TimePickerDialog(this, timePickerListener, Calendar.HOUR_OF_DAY, Calendar.MINUTE, false);
    }
    return null;
    }
    [/java]
  • We are setting the current time of the device as the default time every time the dialog appears.
  • Now, we need to define what happens when a time is selected. Here, I am just creating a Toast to show the selected time. Remember that you can do whatever you want. We will override the method onTimeSetListener()
    [java]
    private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int newhour, int newminute) {
    Toast.makeText(getApplicationContext(), newhour+":"+newminute, Toast.LENGTH_LONG).show();
    }
    };
    [/java]

  • Save and run the application on an emulator or device.

Tpick1  Tpick2

Remember that the Time is always set in 24 Hour format.

COMPLETE SOURCE CODE

MainActivity.java

[java]
package com.nero.mysecondapp;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {

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

protected Dialog onCreateDialog(int id){
switch(id){
case 9999:
return new TimePickerDialog(this, timePickerListener, Calendar.HOUR_OF_DAY, Calendar.MINUTE, false);
}
return null;
}

private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int newhour, int newminute) {
Toast.makeText(getApplicationContext(), newhour+":"+newminute, Toast.LENGTH_LONG).show();
}
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

[/java]

Leave a Comment

Your email address will not be published. Required fields are marked *