Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off keyboard icon at TimePicker?

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: screenshot

I need to hide this keyboard icon under the buttons. How can I do it? It's just a prototype, so here's only its xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:tag="ww">

  <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Time"
    android:textAppearance="?android:attr/textAppearanceLarge" />

  <TimePicker
    android:id="@+id/timePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:amPmBackgroundColor="#6400AA"
    android:numbersSelectorColor="#6400AA" />

  <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:orientation="horizontal">

    <Button
      android:id="@+id/button2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="Cancel"
      android:textColor="#6400AA" />

    <Button
      android:id="@+id/button1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="OK"
      android:textColor="#6400AA" />

  </LinearLayout>

</LinearLayout>
like image 427
VolodymyrH Avatar asked Jan 04 '23 07:01

VolodymyrH


1 Answers

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)
like image 129
Swathi Avatar answered Jan 19 '23 01:01

Swathi