What is the best practice to automate time picker on android?

I need to automate the function where it will select the time(hour and minutes) either by parameters or randomly. The elements only have an accessibility id which goes for each hour.

What is the best way to automate this function and reduce code redundancy?

I really appreciate any help you can provide.

Do you got any solutions?, please let me know.
I need to automate same UI, any one help on this?