Monday 21 January 2013

Time Picker Example in Android

activity_main.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_marginLeft="43dp"
        android:layout_marginTop="34dp"
        android:text="Set Time" />

</RelativeLayout>

MainActivity.java

package com.rajeshvijayakumar.timepicker;

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

import java.util.Calendar;

public class MainActivity extends Activity implements OnClickListener {

    private Button mTimeButton;

    private Calendar mCalen;
    private int hourOfDay;
    private int minute;
    private int ampm;

    private static final int Time_PICKER_ID = 0;

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

    @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);
                    ampm = mCalen.get(Calendar.AM_PM);
                    String ampmStr = (ampm == 0) ? "AM" : "PM";
                    // Set the Time String in Button
                    mTimeButton.setText(hour12format + " : " + minute + " / " + ampmStr);
                }
            };

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

Output :








Source Code : Download this Example Here




6 comments:

  1. Hi.Must all be the code be in the MainActivity? If I were to add and insert all the above code into a new class for this timepicker, will it work properly?

    ReplyDelete
  2. You can put it in separate class also..... Before you must visualize how to handle the listener and time value

    ReplyDelete
  3. Hello, I'm an Android newbie, is it possible to use timepicker to capture the date and time when a user plugged in/out earphones into the phone?

    ReplyDelete
    Replies
    1. You have to write Service using BroadCast Receiver and You must check whether headset is plugged... using the following line

      if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
      //Get Date and Time
      }

      Delete
  4. Hello..

    I'm newbie in android. In this example you are using one button.
    I want to ask, how do i want to create many buttons
    that have same functions (timePicker)?

    How do i want to differentiate each button so that when i click each
    buttons, it will give different output (string in button)

    Tahnk you

    ReplyDelete
  5. Use onClick() method and define custom view........

    ReplyDelete