Tuesday 13 September 2011

Time Picker in Android

Android additionally supports widgets such as ,TimePicker as well as dialogs just like  TimePickerDialog for helping users enter  times.
Here we can see how to set Time To TextView using Time PickerDialog

 XML Layout (activity_time_picker.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <Button
        android:id="@+id/time_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="121dp"
        android:text="Set Time" />
    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/time_button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="31dp"
        android:text="Time"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>


Android Activity Class(TimePickerActivity .class)

package com.androidsurya.timepicker;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;

public class TimePickerActivity extends Activity implements OnClickListener {

    private Button mTimeButton;

    private Calendar mCalen;
    private int hourOfDay;
    private int minute;
    private int amorpm;

    private static final int Time_PICKER_ID = 0;
    TextView time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_picker);
        mTimeButton = (Button) findViewById(R.id.time_button);
        mCalen = Calendar.getInstance();
        hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
        minute = mCalen.get(Calendar.MINUTE);
        amorpm = mCalen.get(Calendar.AM_PM);
        mTimeButton.setOnClickListener(this);
        time = (TextView) findViewById(R.id.time);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {

        switch (id) {
        case Time_PICKER_ID:
            return new TimePickerDialog(this, TimePickerListener, hourOfDay,
                    minute, false);
        }
        return null;
    }

    private TimePickerDialog.OnTimeSetListener TimePickerListener = new TimePickerDialog.OnTimeSetListener() {

        // while dialog box is closed, below method is called.
        public void onTimeSet(TimePicker view, int hour, int minute) {

            mCalen.set(Calendar.HOUR_OF_DAY, hour);
            mCalen.set(Calendar.MINUTE, minute);

            int hour12format = mCalen.get(Calendar.HOUR);
            hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
            minute = mCalen.get(Calendar.MINUTE);
            amorpm = mCalen.get(Calendar.AM_PM);
            String ampmStr = (amorpm == 0) ? "AM" : "PM";
            // Set the Time String in Button
            time.setText("Your Setting Time is : " + hour12format + " : "
                    + minute + " / " + ampmStr);
        }
    };

    @Override
    public void onClick(View v) {
        showDialog(Time_PICKER_ID);
    }
}

Register Activity in Android Manifest File

 <activity
            android:name="com.androidsurya.timepicker.TimePickerActivity"
            android:label="@string/app_name" >

Output Screenshots:

 




No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...