AutoCompleteTextView in action bar not working?
I am trying to use auto full text view in action bar. While I can put the automatic full action view there, whenever I start typing I don't see the suggestion dropdown. It acts like a normal edit text box.
Then, just for testing purposes, I automatically included the full text view in the app layout. But it works exactly as it is supposed to. Both auto-completed text views were initialized using the same adapter.
This is the main onCreate method.
//defining an array to be used by the array adapters
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.MenuCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
private static final String[] COUNTRIES = new String[] { "Belgium",
"France", "France_", "Italy", "Germany", "Spain" };
protected void onCreate(Bundle savedInstanceState) {
//inflating the activity layout
super.onCreate(savedInstanceState);
setContentView(R.layout.frag_map);
//getting the action bar
actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.map_screen_ab_auto_commplete);
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.map_screen_ab_auto_commplete, null);
//Defining an adapter which will then be used by auto complete text view
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
//setting an auto complete text view in the action bar.PLEASE NOTE THAT THIS DOES NOT WORK
mAutocompleteView = (AutoCompleteTextView) v
.findViewById(R.id.map_screen_auto_complete);
mAutocompleteView.setAdapter(adapter);
//just for checking, another auto complete text view was added. THIS WORKS
test_ac_view = (AutoCompleteTextView) this.findViewById(R.id.test_ac_view);
test_ac_view.setAdapter( adapter);
}
This is the main layout of the activity, which also encodes the automatic full text representation:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New AutoCompleteTextView"
android:id="@+id/test_ac_view"
android:layout_gravity="center_horizontal" />
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
</LinearLayout>
This is an action bar layout that automatically contains a full text view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
<AutoCompleteTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/map_screen_auto_complete"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:hint="Search Location" />
</LinearLayout>
</LinearLayout>
source to share
The problem is that you have installed the adapter in AutoCompleteTextView
which was created with Inflator. But you ActionBar uses a different one created from R.layout.layout_id that was provided in the method setCustomView
.
Try this:
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.map_screen_ab_auto_commplete, null);
actionBar.setCustomView(v);
instead actionBar.setCustomView(R.layout.map_screen_ab_auto_commplete);
source to share