How to make time picker dialog for timer

Sajan Jossan

Can anyone help me make timepicker dialog look like this? I maked full layout like this and just need to know how to make selection area in the center of listview also i set infinite loop in the listview.

TimePickerDialog

here is code->

class TimePicker : AppCompatActivity(){

    private val timeSecondsAndMinutes = arrayOf("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "10", "11", "12", "13", "14", "15" , "16", "17", "18", "19", "20", "21", "22", "23",
            "24", "25", "26", "27", "28", "29" , "30", "31", "32", "33", "34", "35", "36", "37",
            "38", "39", "40", "41", "42", "43" , "44", "45", "46", "47", "48", "49", "50", "51",
            "52", "53", "54", "55", "56", "57" , "58", "59")

    private val timeHours = arrayOf("0", "1", "2", "3", "4", "5", "6", "7", "8", "9","10","11",
            "12", "13", "14", "15" ,"16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
            "26", "27", "28", "29" ,"30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
            "40", "41", "42", "43" ,"44", "45", "46", "47", "48", "49", "50", "51", "52", "53",
            "54", "55", "56", "57" ,"58", "59","60","61","62","63","64","65","66","67","68","69",
            "70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86",
            "87","88","89","90")

    private var hoursListView   : ListView? = null
    private var minutesListView : ListView? = null
    private var secondsListView : ListView? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_time_picker)


        InitializeListView()
    }

    private fun InitializeListView()
    {
        hoursListView = findViewById(R.id.HoursListView) as ListView
        minutesListView = findViewById(R.id.MinutesListView) as ListView
        secondsListView = findViewById(R.id.SecondsListView) as ListView

        hoursListView!!.setSelector(R.color.ListViewItemsColor)
        minutesListView!!.setSelector(R.color.ListViewItemsColor)
        secondsListView!!.setSelector(R.color.ListViewItemsColor)



        val hoursAdapter = ArrayAdapter(this, R.layout.layout_for_listview_elements, timeHours)
        val secMinAdapter = ArrayAdapter(this, R.layout.layout_for_listview_elements, timeSecondsAndMinutes)

        val circularHoursAdapter = CircularListAdapter(hoursAdapter)
        val circularMinSecAdapter = CircularListAdapter(secMinAdapter)

        hoursListView!!.adapter = circularHoursAdapter
        minutesListView!!.adapter = circularMinSecAdapter
        secondsListView!!.adapter = circularMinSecAdapter

        hoursListView!!.setSelection(544)
        minutesListView!!.setSelection(358)
        secondsListView!!.setSelection(358)

    }
Sajan Jossan

Thanks to all of you guys i find the solution.

class TimePicker : AppCompatActivity(), AbsListView.OnScrollListener {

    private val timeSecondsAndMinutes = arrayOf("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "10", "11", "12", "13", "14", "15" , "16", "17", "18", "19", "20", "21", "22", "23",
            "24", "25", "26", "27", "28", "29" , "30", "31", "32", "33", "34", "35", "36", "37",
            "38", "39", "40", "41", "42", "43" , "44", "45", "46", "47", "48", "49", "50", "51",
            "52", "53", "54", "55", "56", "57" , "58", "59")

    private val timeHours = arrayOf("0", "1", "2", "3", "4", "5", "6", "7", "8", "9","10","11",
            "12", "13", "14", "15" ,"16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
            "26", "27", "28", "29" ,"30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
            "40", "41", "42", "43" ,"44", "45", "46", "47", "48", "49", "50", "51", "52", "53",
            "54", "55", "56", "57" ,"58", "59","60","61","62","63","64","65","66","67","68","69",
            "70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86",
            "87","88","89","90")

    private var hoursListView   : ListView? = null
    private var minutesListView : ListView? = null
    private var secondsListView : ListView? = null

    private var selectedItemHoursL : View? = null
    private var selectedItemMinutesL : View? = null
    private var selectedItemSecondsL : View? = null
    private var linearLayout : LinearLayout? = null

    private var oneTimeH : Boolean = true
    private var oneTimeM : Boolean = true
    private var oneTimeS : Boolean = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_time_picker)

        AdjustLayoutSize()
        InitializeListView()
    }

    private fun InitializeListView()
    {
        hoursListView = findViewById(R.id.HoursListView) as ListView
        minutesListView = findViewById(R.id.MinutesListView) as ListView
        secondsListView = findViewById(R.id.SecondsListView) as ListView

        hoursListView!!.setSelector(R.color.ListViewItemsColor)
        minutesListView!!.setSelector(R.color.ListViewItemsColor)
        secondsListView!!.setSelector(R.color.ListViewItemsColor)

        hoursListView!!.setOnScrollListener(this)
        minutesListView!!.setOnScrollListener(this)
        secondsListView!!.setOnScrollListener(this)

        val hoursAdapter = ArrayAdapter(this, R.layout.layout_for_listview_elements, timeHours)
        val secMinAdapter = ArrayAdapter(this, R.layout.layout_for_listview_elements, timeSecondsAndMinutes)

        val circularHoursAdapter = CircularListAdapter(hoursAdapter)
        val circularMinSecAdapter = CircularListAdapter(secMinAdapter)

        hoursListView!!.adapter = circularHoursAdapter
        minutesListView!!.adapter = circularMinSecAdapter
        secondsListView!!.adapter = circularMinSecAdapter

        hoursListView!!.setSelection(544)
        minutesListView!!.setSelection(358)
        secondsListView!!.setSelection(358)
    }

    override fun onScroll(listViewSelected: AbsListView?, firstVisibleItem: Int, visibleItemCount: Int, totalItemCount: Int)
    {
        if(firstVisibleItem != 0 && visibleItemCount != 0)
        {
            if (listViewSelected!!.id == hoursListView!!.id) {
                 if(oneTimeH){
                     SelectionNumberBackground(listViewSelected, hoursListView, R.drawable.selecteditem_hhss_bg)
                     oneTimeH = false
                }
            } else if (listViewSelected.id == minutesListView!!.id) {
                if(oneTimeM) {
                    SelectionNumberBackground(listViewSelected, minutesListView, R.drawable.selecteditem_mm_bg)
                    oneTimeM = false
                }
            } else if (listViewSelected.id == secondsListView!!.id) {
                if(oneTimeS) {
                    SelectionNumberBackground(listViewSelected, secondsListView, R.drawable.selecteditem_hhss_bg)
                    oneTimeS = false
                }
            }
        }
    }

    override fun onScrollStateChanged(p0: AbsListView?, p1: Int)
    {
        ChangeSelectionInList(p0)

        if (p1 == AbsListView.OnScrollListener.SCROLL_STATE_IDLE)
        {
            if(p0!!.id == hoursListView!!.id) {
                SelectionNumberBackground(p0, hoursListView,R.drawable.selecteditem_hhss_bg)
            }
            else if(p0.id == minutesListView!!.id){
                SelectionNumberBackground(p0,minutesListView,R.drawable.selecteditem_mm_bg)
            }
            else if(p0.id == secondsListView!!.id){
                SelectionNumberBackground(p0,secondsListView,R.drawable.selecteditem_hhss_bg)
            }
        }
    }

    private fun SelectionNumberBackground(p0 : AbsListView?, MainListView : ListView?, background : Int)
    {
        p0!!.setSelection(p0.firstVisiblePosition)
        val middleView = p0.firstVisiblePosition + 2
        val firstView = p0.firstVisiblePosition - MainListView!!.headerViewsCount
        val middleItem = middleView - firstView
        if (middleItem < 0 || middleItem >= p0.childCount) {
            return
        }
        val thirdView = p0.getChildAt(middleItem)
        thirdView.setBackgroundResource(background)
        if(p0.id == hoursListView!!.id){
            selectedItemHoursL = thirdView
        }
        else if(p0.id == minutesListView!!.id){
            selectedItemMinutesL = thirdView
        }
        else if(p0.id == secondsListView!!.id){
            selectedItemSecondsL = thirdView
        }
    }

    private fun ChangeSelectionInList(p0: AbsListView?)
    {
        if(p0!!.id == hoursListView!!.id) {
            if (selectedItemHoursL != null) {
                selectedItemHoursL!!.setBackgroundResource(R.color.ListViewItemsColor)
            }
        }
        else if(p0.id == minutesListView!!.id){
            if(selectedItemMinutesL != null){
                selectedItemMinutesL!!.setBackgroundResource(R.color.ListViewItemsColor)
            }
        }
        else if(p0.id == secondsListView!!.id){
            if(selectedItemSecondsL != null){
                selectedItemSecondsL!!.setBackgroundResource(R.color.ListViewItemsColor)
            }
        }
    }

    private fun AdjustLayoutSize()
    {
        //For resolution
        val display = windowManager.defaultDisplay
        val size = Point()
        display.getSize(size)
        //Get DPI
        val displayMetrics = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(displayMetrics)
        //Initialize LinearLayout for changing TimePicker Dialog Size;
        linearLayout = findViewById(R.id.TimePickerLayout) as LinearLayout
        val layoutParams = linearLayout!!.layoutParams as ViewGroup.MarginLayoutParams
        //Nexus 5X device 420dpi 1080x1920 == 1794
        if(size.y >= 1794 && size.y < 1920){
            layoutParams.topMargin = 656
            layoutParams.rightMargin = 80
            layoutParams.leftMargin = 80
        }
        //Redmi note 3 DPI 480 1080x1920
        else if(size.y == 1920){
            layoutParams.topMargin = 630
            layoutParams.rightMargin = 80
            layoutParams.leftMargin = 80
        }
        //Pixel XL device 480dpi 1440x2560 == 2416
        else if(size.y > 1920 && displayMetrics.densityDpi >= 480){
            layoutParams.topMargin = 1098
            layoutParams.rightMargin = 125
            layoutParams.leftMargin = 125
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
        finish()
        overridePendingTransition(R.anim.top_to_bottom1,R.anim.top_to_bottom2)
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Setting a countdown timer with time picker dialog android

From Dev

How to set time from time picker dialog?

From Dev

Dialog on time picker

From Dev

time picker dialog not working

From Dev

How to get date and time from Date/Time Picker dialog and show it?

From Dev

How to make a Time Picker in jQuery mobile 1.4.0?

From Dev

how can i put date and time picker in a dialog in android

From Dev

how can i put date and time picker in a dialog in android

From Dev

how to use number picker with dialog

From Dev

How to create a number picker dialog?

From Dev

how to use number picker with dialog

From Dev

time-picker in bootstrap modal dialog

From Dev

time-picker in bootstrap modal dialog

From Dev

How to make this BATch file have a timer (time left to shutdown or reset)?

From Dev

How to make a C timer expire at a particular system time in Linux

From Dev

How to make run real time and faster refresh method with timer

From Dev

How to make Countdown timer to show same time in all Activities

From Dev

How to make this BATch file have a timer (time left to shutdown or reset)?

From Dev

How to make a C timer expire at a particular system time in Linux

From Dev

How to put a timer in an android dialog

From Dev

How to make a Wheel picker

From Dev

Start countdown timer when time is chosen from Time Picker in android

From Dev

How can I make timer2 and timer3 to work at the same time but asynchrony?

From Dev

how to make date and time picker in angularjs ? date and time should be in same interface?

From Dev

How to Make a VBScript Timer

From Dev

How to make a timer in C?

From Dev

How to set the limit on date in Date picker dialog

From Dev

How to make live dialog?

From Dev

How to make live dialog?

Related Related

  1. 1

    Setting a countdown timer with time picker dialog android

  2. 2

    How to set time from time picker dialog?

  3. 3

    Dialog on time picker

  4. 4

    time picker dialog not working

  5. 5

    How to get date and time from Date/Time Picker dialog and show it?

  6. 6

    How to make a Time Picker in jQuery mobile 1.4.0?

  7. 7

    how can i put date and time picker in a dialog in android

  8. 8

    how can i put date and time picker in a dialog in android

  9. 9

    how to use number picker with dialog

  10. 10

    How to create a number picker dialog?

  11. 11

    how to use number picker with dialog

  12. 12

    time-picker in bootstrap modal dialog

  13. 13

    time-picker in bootstrap modal dialog

  14. 14

    How to make this BATch file have a timer (time left to shutdown or reset)?

  15. 15

    How to make a C timer expire at a particular system time in Linux

  16. 16

    How to make run real time and faster refresh method with timer

  17. 17

    How to make Countdown timer to show same time in all Activities

  18. 18

    How to make this BATch file have a timer (time left to shutdown or reset)?

  19. 19

    How to make a C timer expire at a particular system time in Linux

  20. 20

    How to put a timer in an android dialog

  21. 21

    How to make a Wheel picker

  22. 22

    Start countdown timer when time is chosen from Time Picker in android

  23. 23

    How can I make timer2 and timer3 to work at the same time but asynchrony?

  24. 24

    how to make date and time picker in angularjs ? date and time should be in same interface?

  25. 25

    How to Make a VBScript Timer

  26. 26

    How to make a timer in C?

  27. 27

    How to set the limit on date in Date picker dialog

  28. 28

    How to make live dialog?

  29. 29

    How to make live dialog?

HotTag

Archive