Custom Time Picker in Android

Example

MainActivity.Java

package com.abhay.myapplication;

import android.app.Dialog;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.NumberPicker;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    

    Button b;

    static Dialog d ;

    final Calendar c = Calendar.getInstance();

    final int hour = c.get(Calendar.HOUR_OF_DAY);

    final int minute = c.get(Calendar.MINUTE);

    int year = Calendar.getInstance().get(Calendar.YEAR);

    @Override    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

       

        b = (Button) findViewById(R.id.year);

        b.setText(hour+":"+minute);

        b.setOnClickListener(new View.OnClickListener()

        {

            

            @Override            public void onClick(View v) {

                showYearDialog();

            }

        });

    }

    

    

    public void showYearDialog()

    {

        

        final Dialog d = new Dialog(MainActivity.this);

        d.setTitle("Year Picker");

        d.setContentView(R.layout.timedialog);

        Button set = (Button) d.findViewById(R.id.button1);

        Button cancel = (Button) d.findViewById(R.id.button2);

          TextView year_text=(TextView)d.findViewById(R.id.title_text);

         title_text.setText(hour+":"+minute);

final NumberPicker nopicker = (NumberPicker) d.findViewById(R.id.numberPicker1);

        final NumberPicker nopicker1 = (NumberPicker) d.findViewById(R.id.numberPicker2);

        nopicker.setMaxValue(24);

        nopicker.setMinValue(hour);

        nopicker.setWrapSelectorWheel(false);

        nopicker.setValue(hour);

        nopicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

    

    

        nopicker1.setMaxValue(60);

        nopicker1.setMinValue(minute);

        nopicker1.setWrapSelectorWheel(false);

        nopicker1.setValue(minute);

        nopicker1.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

    

    

        nopicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {

            @Override       

     public void onValueChange(NumberPicker picker, int oldVal, int newVal){

                //Display the newly selected number from picker      

        //  tv.setText("Selected Number : " + newVal);             

                if(nopicker.getValue()==hour)

                {

                    nopicker1.setMinValue(minute);

                }else {

                    nopicker1.setMinValue(0);

                }

            }

        });

        

        set.setOnClickListener(new View.OnClickListener()

        {

            @Override            public void onClick(View v) {

                b.setText(String.valueOf(nopicker.getValue()+":"+nopicker1.getValue()));

                d.dismiss();

            }

        });

        cancel.setOnClickListener(new View.OnClickListener()

        {

            @Override            public void onClick(View v) {

                d.dismiss();

            }

        });

        d.show();

    }

    

}

activity_main.xml

<LinearLayout 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"

              tools:context=".MainActivity" >

    <Button    

    android:id="@+id/year"    

    android:layout_width="match_parent"   

     android:layout_height="wrap_content"   

     android:layout_gravity="center_vertical"    

    android:text="Year" />

</LinearLayout>

/res/layout/timedialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     

         android:layout_width="match_parent"      

        android:orientation="vertical"       

       android:layout_height="match_parent" >

<LinearLayout

    android:layout_width="match_parent"

    android:orientation="horizontal"

    android:background="@color/colorAccent"

    android:layout_height="wrap_content" >

    <TextView

        android:id="@+id/title_text"

        android:layout_margin="10dp"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textStyle="bold"

        android:textColor="@android:color/white"

        android:text="demo"/>

</LinearLayout>

    <LinearLayout    

    android:layout_width="match_parent"   

     android:orientation="horizontal"    

    android:layout_gravity="center"   

     android:gravity="center"   

     android:layout_height="wrap_content" >

    <NumberPicker   

     android:id="@+id/numberPicker1"    

    android:layout_width="wrap_content" 

      android:layout_height="wrap_content"     

   android:layout_gravity="center_horizontal"        />

    <NumberPicker     

   android:id="@+id/numberPicker2" 

      android:layout_width="wrap_content" 

      android:layout_height="wrap_content"   

     android:layout_gravity="center_horizontal"        />

        </LinearLayout>

    <LinearLayout    

    android:layout_width="match_parent" 

       android:orientation="horizontal"   

     android:layout_height="wrap_content" >

        <Button        

    android:id="@+id/button2"   

         android:layout_width="0dp"    

        android:layout_height="wrap_content"      

      android:layout_weight="1"    

        android:background="@color/colorAccent"      

      android:text="Cancel" />

        <Button      

      android:id="@+id/button1"    

        android:layout_width="0dp"    

        android:layout_height="wrap_content"     

       android:layout_weight="1"     

       android:background="@color/colorAccent"       

     android:text="Set" />

    </LinearLayout>

</LinearLayout>