Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - searchview with auto complete feature inside action bar

I want to build an app that has sherlock action bar and a search view inside it. However, i want this searchview to have autocomplete feature like what autocompleteTextView has. Is there any straight forward implementation to do that using searchview alone? or should i use autocompleteTextView inside action bar to do this? I found several posts out there, but none of them are helping me. Those posts talk only about creating a autocompleteTextView , but i want search view to have this functionality. Any idea how can it be done?

like image 918
suresh cheemalamudi Avatar asked Apr 04 '13 07:04

suresh cheemalamudi


People also ask

How to set SearchView in ToolBar in android?

To add a SearchView widget to the app bar, create a file named res/menu/options_menu. xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item.

How to implement SearchView in android?

SearchView widget can be implemented over ToolBar/ActionBar or inside a layout. SearchView is by default collapsible and set to be iconified using setIconifiedByDefault(true) method of SearchView class. For making search field visible, SearchView uses setIconifiedByDefault(false) method.

What does Android completionHint attribute in auto complete?

android:completionHint This defines the hint displayed in the drop down menu.


1 Answers

For this I have Create one Layout with AutoCompleteTextView and add it in ActionBar its call Custom layout in ActionBar.

After that I have Create Adapter with android.R.layout.simple_dropdown_item_1line. set it in AutoCompleteTextView.

check Below Code:

enter image description here

package com.example.testapp;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

@TargetApi(11)
public class MainActivity extends Activity {

    private static final String[] COUNTRIES = new String[] { "Belgium",
            "France", "France_", "Italy", "Germany", "Spain" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowCustomEnabled(true);
        // actionBar.setDisplayShowTitleEnabled(false);
        // actionBar.setIcon(R.drawable.ic_action_search);

        LayoutInflater inflator = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.actionbar, null);

        actionBar.setCustomView(v);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);
        AutoCompleteTextView textView = (AutoCompleteTextView) v
                .findViewById(R.id.editText1);
        textView.setAdapter(adapter);

    }

}

Your Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Action Bar:"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" />

    <AutoCompleteTextView
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:imeOptions="actionSearch"
        android:inputType="textAutoComplete|textAutoCorrect"
        android:textColor="#FFFFFF" >

        <requestFocus />
    </AutoCompleteTextView>

</LinearLayout>

For More Details check this articals: one, two and three

Best Luck!

like image 147
Dhaval Parmar Avatar answered Sep 21 '22 12:09

Dhaval Parmar