Creating a TimePicker in Android
Android gives controls for the user to pick a time as ready-to-use dialogs.
Each picker gives controls for selecting each part of the time (hour, minute, AM/PM).
Using these pickers helps ensure that your users can pick a time is valid, formatted correctly, and adjusted to the user's locale.
We can get TimePickerDialog through DialogFragment and TimePickerDialog.OnTimeSetListener.
Create Android Project with name as "TimePickerDemo" with package name "com.etr.timepickerdemo". Create Activity with name as "MainActivity" and it will fetch time. This is the code for MainActivity.
Paste below code to "activity_main.xml"
Paste below code to "AndroidManifest.xml"
You can download source code here.
Each picker gives controls for selecting each part of the time (hour, minute, AM/PM).
Using these pickers helps ensure that your users can pick a time is valid, formatted correctly, and adjusted to the user's locale.
We can get TimePickerDialog through DialogFragment and TimePickerDialog.OnTimeSetListener.
Create Android Project with name as "TimePickerDemo" with package name "com.etr.timepickerdemo". Create Activity with name as "MainActivity" and it will fetch time. This is the code for MainActivity.
package com.etr.timepickerdemo;
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.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends FragmentActivity {
Button show;
static TextView time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (Button) findViewById(R.id.ids);
time = (TextView) findViewById(R.id.time);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void show(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(this.getSupportFragmentManager(), "timePicker");
}
public static class TimePickerFragment extends DialogFragment implements
TimePickerDialog.OnTimeSetListener {
public TimePickerFragment() {
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
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()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time.setText("Selected Time is " + hourOfDay + ":" + minute);
}
}
}
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.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends FragmentActivity {
Button show;
static TextView time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (Button) findViewById(R.id.ids);
time = (TextView) findViewById(R.id.time);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void show(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(this.getSupportFragmentManager(), "timePicker");
}
public static class TimePickerFragment extends DialogFragment implements
TimePickerDialog.OnTimeSetListener {
public TimePickerFragment() {
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
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()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time.setText("Selected Time is " + hourOfDay + ":" + minute);
}
}
}
Paste below code to "activity_main.xml"
<RelativeLayout 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">
<Button
android:id="@+id/ids"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="137dp"
android:onClick="show"
android:text="show Time" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ids"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="Please select time" />
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/ids"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="137dp"
android:onClick="show"
android:text="show Time" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ids"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="Please select time" />
</RelativeLayout>
Paste below code to "AndroidManifest.xml"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.etr.timepickerdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.etr.timepickerdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.etr.timepickerdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.etr.timepickerdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You can download source code here.
Comments
Post a Comment