I have a timepicker, everything works well but the problem is that I can\'t make it look like designer wants. For now it looks like this:
I need to hide this keybo
Android N :
Date/Time picker come with rolling clock and that improved interaction with users to make entry but that made minute selection
more tedious .Rolling clock sucks big time in 24h mode and while using thumb it was impossible to see exact time .
Android O :
Date/Time picker (in Calendar, Clock, etc) gets one small icon(manual entry) at the bottom left: a keyboard. Tapping it switches to text-based entry where you can manually type in the exact precise time.
Okay this is my solution. After inspecting the Layout I found that the keyboard is AppCompatImageButton
. So you just do a recursion over the TimePicker
which is a Viewgroup
. So you can call it for example like this:
private fun initTimePicker() {
timePicker.setIs24HourView(true)
setImageButtonToGone(timePicker)
}
private fun setImageButtonToGone(viewGroup: ViewGroup) {
for (i in 0 until viewGroup.childCount) {
val child = viewGroup.getChildAt(i)
if (child is LinearLayout) {
setImageButtonToGone(child)
} else if (child is AppCompatImageButton) {
child.visibility = View.GONE
}
}
}
And you will get the desired layout.
WARNING!!! This depends heavely that the keyboard button is the only AppCompatImageButton
if they change the XML of TimePicker this will not work properly anymore.
I just inspected the hierarchy in layout inspector and found that keyboard icon. For portrait and landscape hierarchy is different, so we have a orientation check for this and also have OS check from Oreo(from Oreo only this icon is availble), I tried this code and it is working fine. PS: Kotlin Code
fun hideKeyboardInputInTimePicker(orientation: Int, timePicker: TimePicker)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
try
{
if (orientation == Configuration.ORIENTATION_PORTRAIT)
{
((timePicker.getChildAt(0) as LinearLayout).getChildAt(4) as LinearLayout).getChildAt(0).visibility = View.GONE
}
else
{
(((timePicker.getChildAt(0) as LinearLayout).getChildAt(2) as LinearLayout).getChildAt(2) as LinearLayout).getChildAt(0).visibility = View.GONE
}
}
catch (ex: Exception)
{
}
}
}
Call like this
hideKeyboardInputInTimePicker(this.resources.configuration.orientation, startTimePicker)
As far as I can tell you cannot remove this icon, it is part of the UI on Android O.
The icon is there to make the manual editing of the time more apparent because in previous versions it was not obvious that you could click on the time and change it via keyboard
Try to use:
timePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
You also can use android:descendantFocusability="blocksDescendants"
in xml.