Studyon Minte9.com
Android




Pickers



To display a TimePickerDialog using DialogFragment, you need to define a fragment class that extends 
DialogFragment and return a TimePickerDialog from the fragment's onCreateDialog() method.

When compiling the source code, you need to make setButton class "static".


	%--- activity_main.xml

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    android:orientation="vertical"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content">
	    
	    <Button 
		android:id="@+id/picker_button"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="15:35"
		android:onClick="showTimePickerDialog"
		/>

	</LinearLayout>


	%--- MainActivity.java

	package com.example.myapp;
	import java.util.Calendar;
	import android.app.Dialog;
	import android.app.TimePickerDialog;
	import android.os.Bundle;
	import android.support.v4.app.DialogFragment;
	import android.support.v4.app.FragmentActivity;
	import android.text.format.DateFormat;
	import android.view.View;
	import android.widget.Button;
	import android.widget.TimePicker;

	public class MainActivity extends FragmentActivity {
		
	    private static Button button;
		
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		button = (Button) findViewById(R.id.picker_button);
	    }
	    
	    /*-- STEP 1: show time picker --*/
	    
	    public void showTimePickerDialog (View view) {
		
		DialogFragment f = new MyPicker();
		f.show(getSupportFragmentManager(), "timePicker");
	    }
	    
	    public static class MyPicker extends DialogFragment 
			implements TimePickerDialog.OnTimeSetListener {
		
			@Override
			public Dialog onCreateDialog(Bundle saveInstanceState) {
				
				//Current time as default value
				final Calendar c = Calendar.getInstance();
				int hour = c.get(Calendar.HOUR_OF_DAY);
				int minute = c.get(Calendar.MINUTE);
				
				return new TimePickerDialog(getActivity(), this, hour, minute,
						 DateFormat.is24HourFormat(getActivity()));
			}
			
			/*-- STEP 2: do something with selection --*/
			
			@Override
			public void onTimeSet(TimePicker view, int hour, int minute) {
				
				setButton(hour, minute);
				
			}
		}
	    
	    /*-- STEP 3: apply the changes --*/
	    
	    public static void setButton(int hour, int minute) {
		
		String H = hour < 10 ? 0 + "" + hour : hour +"";
		String M = minute < 10 ? 0 + "" + minute : minute +"";
		
		button.setText(H + ":" + M);
	    }

	}


http://developer.android.com/guide/topics/ui/controls/pickers.html
http://stackoverflow.com/questions/15291112/how-to-get-date-from-date-picker-in-android