androidxmlkotlinsearchviewandroid-search

Difference between SearchView hint via XML and dynamically


What's the difference between the searchable.xml (required to use SearchView widget) and declaring its hint dynamically? Why is it that declaring the SearchView hint dynamically works but doing the same thing in the XML file doesn't show it?

via XML

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search" />

via Kotlin

searchView.queryHint = "Search"

Solution

  • Because android:hint is the hint for the EditText (EditText inherit from TextView which is the view implementing the hint, from the official documentation)

    The attribute you are looking for is android:queryHint, you can see it from the official documentation

    In the Kotlin way of doing it queryHint you are setting the variable directly because in Kotlin there is no explicit need for getters and setters. That variable name is the clue for figuring the problem, the field on the class is called queryHint not hint