October 25, 2013

Android TimePickerDialog

In this tutorial I want to explain about creating a  TimePickerDialog like,



In this I am having One Button and One EditText. On this example first it will load the current time to the EditText, While Tap on the button it will display the TimePickerDialog with the current time after tap on Done button TimePickerDialog it will update in the EditText.
 


activity_main.xml

This is a Layout of main page. This layout contain One Button and one EditText.



<LinearLayout 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"
    android:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Time" />

    <EditText
         android:id="@+id/EdtTxtTime"
         android:layout_width="wrap_content"
          android:layout_height="wrap_content"
         android:ems="10"
         android:focusableInTouchMode="false"
         android:inputType="text" />

</LinearLayout>



MainActivity.java
 
public class MainActivity extends Activity {
       Button BtnSelectTime;
   private EditText EdtTxtTime;
   private static Context context;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

           context=MainActivity.this;
           EdtTxtTime = (EditText) findViewById(R.id.EdtTxtTime);

            String Time = getCurrentTime();
            if(Integer.parseInt(Time.substring(0, 2).toString())>12){
                Time = (Integer.parseInt(Time.substring(0, 2).toString())-12)+Time.substring(2);              
            }
            EdtTxtTime.setText(Time);
       
        BtnSelectTime = (Button) findViewById(R.id.BtnSelectTime);
        BtnSelectTime.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
              
                             Calendar mcurrentTime = Calendar.getInstance();
                              int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                              int minute = mcurrentTime.get(Calendar.MINUTE);
                              TimePickerDialog mTimePicker;
                              mTimePicker = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
                                  @Override
                                  public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                                      EdtTxtTime.setText( selectedHour + ":" + selectedMinute);
                                  }
                              }, hour, minute, true);//Yes 24 hour time
                              mTimePicker.setTitle("Select Time");
                              mTimePicker.show();

            }
        });
       
    }

//Function to get current time
    public static String getCurrentTime() {
            String DATE_FORMAT_NOW = "HH:mm";
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            return sdf.format(new Date());
     }
   
}

No comments:

Post a Comment