2

I have created the following EditText with ImageButton:

enter image description here

By using the following code:

<RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_percent="0.12">

    <EditText
        android:id="@+id/txt_search"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginBottom="8dp"
        android:paddingLeft="24dp"
        android:textSize="14sp"
        android:background="@drawable/et_rounded"
        android:hint="Search for book or author"
        android:textColorHint="@color/colorGray"
        app:layout_constraintTop_toTopOf="parent"/>

    <ImageButton
        android:id="@+id/btn_search"
        android:layout_width="48dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="24dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="8dp"
        android:layout_alignRight="@id/txt_search"
        android:background="@android:color/transparent"
        android:src="@drawable/ic_search_black_24dp"/>

</RelativeLayout>

There are two things that im trying to get:

1) Is there an option to add into the keyboard a search button that once I click on this button it will search for results just like when I search in some site for example:

enter image description here

2) Is there a way to make something like popup menu that will offer few searching results once I type in the edit text?

Thank you

Ben
  • 1,737
  • 2
  • 30
  • 61
  • 1
    1) Check https://stackoverflow.com/a/3205405/10883621 .. 2) Do you mean something like autoCompleteTextView(https://developer.android.com/reference/android/widget/AutoCompleteTextView) , or do you want to search in real time from somewhere (i.E. from database)? – isaaaaame Oct 01 '19 at 15:18
  • First question is done, thank you. About second, I do want kind of autocomplete however not based on some data that I insert manually, but by some google search or something like this that has many results. For example if I type in harry p I want it to offer result for harry potter – Ben Oct 01 '19 at 15:28

2 Answers2

0

First one

Add the following property in the edittex (my code example is kotlin)

android:imeOptions="actionSearch"

and for catch the event do the following code:

yourTextView.setOnEditorActionListener{ _,action,_->
            if (action == EditorInfo.IME_ACTION_SEARCH){
                //put your logic here
                return@setOnEditorActionListener true
            }
            return@setOnEditorActionListener false
        }

and for the second, take a look in autoCompleteExample

0

Answering your both questions:

First specify inputType and ime options:

 <AutoCompleteTextView
        android:id="@+id/autocomplete"
        android:imeOptions="actionSearch"
        android:inputType="text" />

Then register adapter:

// field
val adapter by lazy { ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line) }

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    autocomplete.setAdapter(adapter)
    autocomplete.doOnTextChanged { text, _, _, _ ->
        // Call api to find search results for your text:
        callApiFor(text.toString())
    }

    autocomplete.setOnEditorActionListener { textView, actionId, _ ->
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            // call an API to find your results
            searchResults(textView.text.toString())
            return@setOnEditorActionListener true
        }

        false
    }
}

After you received data:

adapter.clear()
adapter.addAll(resultsList)
adapter.notifyDataSetChanged()

Search suggestions link: http://suggestqueries.google.com/complete/search?client=firefox&q=some%20query

Here are some docs (unofficial, because I couldn't find any official docs): https://shreyaschand.com/blog/2013/01/03/google-autocomplete-api/

You'll get results in this format:

[
  "some query",
  [
    "some query",
    "some query in sql",
    "some query in mysql",
    "some query in oracle",
    "some_queryset",
    "queryselectorall some",
    "querylist some",
    "query some information",
    "query some data",
    "query some questions"
  ]
]

Same as google:

GV_FiQst
  • 1,487
  • 14
  • 30