How to create Time Picker with FragmentDialog in Android

In this android programming source code example, we are going to create Time Picker with FragmentDialog in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to create Time Picker with FragmentDialog in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ededed">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:layout_centerInParent="true"
        android:onClick="ShowTimePicker"
        android:text="ShowTimePicker"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:textStyle="bold" />

</RelativeLayout>

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;


public class DatePickerAndTimePickerActivity15 extends AppCompatActivity implements ChooseTimeFragment.TimeListner{
    private int hour, minute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_picker_and_time_picker15);
    }

    public void ShowTimePicker(View view){
        ChooseTimeFragment chooseTimeFragment = new ChooseTimeFragment();
        chooseTimeFragment.show(getSupportFragmentManager(), "");
    }


    @Override
    public void onTimeSelected(int hour, int minute){
        this.hour = hour;
        this.minute = minute;
    }
}

fragment_choose_time.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ededed">

    <TimePicker
        android:id="@+id/timepicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />


    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:text="Accept"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:textStyle="bold"
        android:layout_below="@id/timepicker"/>

</RelativeLayout>

ChooseTimeFragment.java

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TimePicker;


public class ChooseTimeFragment extends DialogFragment implements View.OnClickListener {

    private TimePicker timePicker;
    private Button button;
    private boolean isTimesetted = false;
    private int hour;
    private int minute;
    private TimeListner listner;
    private TimePicker timepicker;

    public interface TimeListner{
        public void onTimeSelected(int hour, int minute);
    }

    public ChooseTimeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootview = inflater.inflate(R.layout.fragment_choose_time, container, false);
        getDialog().setTitle("TimePicker");
        timepicker = (TimePicker) rootview.findViewById(R.id.timepicker);
        button = (Button) rootview.findViewById(R.id.button);
        button.setOnClickListener(this);
        if(isTimesetted){
            timepicker.setHour(hour);
            timePicker.setMinute(minute);
        }
        return rootview;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
                int hour = timepicker.getHour();
                int minute = timepicker.getMinute();
                listner.onTimeSelected(hour, minute);
                break;
        }
        this.dismiss();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        listner = (TimeListner) context;
    }

    public void setTime(int hour, int minute){
        this.hour = hour;
        this.minute = minute;
        this.isTimesetted = true;
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment