TimePickerDialog

Hi Guys!

A view for selecting the time of day, in either 24 hour or AM/PM mode. The hour, each minute digit, and AM/PM (if applicable) can be conrolled by vertical spinners. The hour can be entered by keyboard input. Entering in two digit hours can be accomplished by hitting two digits within a timeout of about a second . The minutes can be entered by entering single digits.

For more details you can visit the android developer site Timer Picker
Now lets start the coding about the Time Picker android.

acivity_main.xml



    

    

MainActivity.java

package com.sunil.timepickerdialog;

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.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
  
 private int hour, minute;
 static final int ID_TIMEPICKER = 0;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
     
      Button startTimePicker = (Button)findViewById(R.id.starttimepicker);
      startTimePicker.setOnClickListener(new Button.OnClickListener(){
   @Override
   public void onClick(View arg0) {
   
    final Calendar c = Calendar.getInstance();
       hour = c.get(Calendar.HOUR_OF_DAY);
       minute = c.get(Calendar.MINUTE);
       showDialog(ID_TIMEPICKER);
   }});
  }
  @Override
 protected Dialog onCreateDialog(int id) {
 
   switch(id){
   case ID_TIMEPICKER:
    return new TimePickerDialog(this, timeSetListener, hour, minute, false);
   default:
    return null;
   }
 }
 private TimePickerDialog.OnTimeSetListener timeSetListener
  = new TimePickerDialog.OnTimeSetListener(){
  @Override
  public void onTimeSet(android.widget.TimePicker arg0, int arg1, int arg2) {
   // TODO Auto-generated method stub
   Toast.makeText(getBaseContext(), String.valueOf(arg1) + ":" + String.valueOf(arg2), Toast.LENGTH_LONG).show();
  }};
}


 You can download the source code Time Picker

2 comments:

Debug Database in Android

In my last tutorial, we have learned how we can use persistence database in android. We also understood which database wrapper we can use...

Contact Me

Name

Email *

Message *