Friday 19 October 2012

ANDROID - Time Picker Dialog

Demo:


 


1)main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#9932CC"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonTimePickerDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="62dp"
        android:text="Show TimePickerDialog" />

</RelativeLayout>


2)Activity:
 
package com.venky.timepickerdialogdemo;

import java.util.Calendar;

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

public class TimePickerDialogDemoActivity extends Activity {
      /** Called when the activity is first created. */

      Button showTimePickerDialog;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            // set the references
            showTimePickerDialog = (Button) findViewById(R.id.buttonTimePickerDialog);

            // set the Listener
            showTimePickerDialog.setOnClickListener(new OnClickListener() {

                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        // get the Calendar
                        Calendar c = Calendar.getInstance();

                        // get the Hour and Minute
                        int cHour = c.get(Calendar.HOUR_OF_DAY);
                        int cMinute = c.get(Calendar.MINUTE);

                        // create TimePicker Dialog
                        TimePickerDialog timePickerDialog = new TimePickerDialog(
                                    TimePickerDialogDemoActivity.this, timeListener, cHour,
                                    cMinute, false);

                        // show TimePicker Dialog
                        timePickerDialog.show();
                  }

                  // set the Listener for TimePickerDialog
                  OnTimeSetListener timeListener = new OnTimeSetListener() {

                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                              // TODO Auto-generated method stub

                              Toast.makeText(TimePickerDialogDemoActivity.this,
                                          "Setted Time =>" + hourOfDay + "-" + minute,
                                          Toast.LENGTH_LONG).show();
                        }
                  };
            });
      }
}


3)Download this Project Click Here
 

No comments:

Post a Comment