Wednesday, July 11, 2012

TimePickerDialog in Android



Click Here to download source code 

Package Name  :  selva.timepicker

Project Name    :  TimePickerDialog

Version             :  1.5 ( Support 1.5 and above versions) 



main.xml 


 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ShowTimepickerDialog" />

</LinearLayout>



 TimePickerDialogActivity.java



package selva.timepicker;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;
import android.app.Dialog;
import android.app.TimePickerDialog;
public class TimePickerDialogActivity extends Activity
{
   
    int hour, minute;
    static final int TIME_DIALOG_ID = 0;
    TimePicker timePicker;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
   
   
        Button btnOpen=(Button) findViewById(R.id.button1);
      
        btnOpen.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                showDialog(TIME_DIALOG_ID);
                }
        });
    }
   
@Override
protected Dialog onCreateDialog(int id)
{
    switch (id)
    {
    case TIME_DIALOG_ID:
        return new TimePickerDialog(
                this, mTimeSetListener, hour, minute, false);
    }
    return null;
}
private TimePickerDialog.OnTimeSetListener mTimeSetListener =new TimePickerDialog.OnTimeSetListener()
{
    public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour)
    {
        hour = hourOfDay;
        minute = minuteOfHour;
        Toast.makeText(getBaseContext(),"You have selected : " + hour + ":" + minute,Toast.LENGTH_SHORT).show();
    }
};
}

 OUTPUT:

























ClickTimepickerDialog







































select time and click set button















































 Click Here to download source code


No comments:

Post a Comment