Android AutoCompleteTextView search and returns the result in List instead of default dropdown.
Is there a way that I could redesign and put the result of an AutoCompleteTextView into a list other than to its default list design?
In my MainActivity, I have this:
String[]fruits = {"apple", "avocado", "banana", "blackberry", "blueberry", "coconut", "durian", "mango", "melon"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, fruits);
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocom);
autoCompleteTextView.setAdapter(stringArrayAdapter);
autoCompleteTextView.setTextColor(Color.RED);
ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(stringArrayAdapter);
}
In My xml,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.android.autocompletenativesample.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:text="Pick a Fruit"
android:id="#+id/tv"
/>
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="#+id/autocom"
android:layout_below="#+id/tv"
android:layout_marginLeft="36dp"
android:layout_marginTop="17dp"
android:completionThreshold="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ListView Implementation"
android:layout_below="#+id/autocom"
android:id="#+id/tv2"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lv"
android:layout_below="#+id/tv2"
>
</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RecyclerView Implementation"
android:layout_below="#+id/lv"
/>
</RelativeLayout>
Furthermore, here is a Visual Explanation. It will search in AutoCompleteTextView field but the result will not be displayed below instead on another layout.
Should I edit the ArrayAdapter ang change the layout, or have a CustomAutoCompleteTextView that implements AutoCompleteTextView or and EditText that implements AutoCompleteTextView.
I desire for my AutoCompleteTextView's Result to be formatted like this.
RecyclerView and CardView Result design
Thanks.
i can give you the logic. check out this link.
http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/ and this too http://www.tutorialsbuzz.com/2014/08/android-filters-for-simple-listview.html
and filteration is done in adapter so first bind listview with your empty adapter and in textWatcher bindlistview with datasource filteration and notifie for changes.
Related
I want to create EditText with clickable menu underneath. User should be able to write his own custom text into EditText or choose from predefined options.
I though about adding recyclerView under EditText, but for 4-5 options its waste of recylerView. Is there any way to do it better (I dont want to write a lot of code for this feature).
AutoCompleteTextView is a component used to show suggestions while writing in an editable text field. The suggestions list is shown in a drop-down menu from which a user can select the desired item. The list of suggestions is obtained from an adapter and it appears only after a number of characters that are specified in the threshold.
Here is a simple example of AutoCompleteTextView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:text="X" />
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView"
android:layout_marginLeft="36dp"
android:layout_marginTop="17dp"
android:ems="10"
android:text="">
<requestFocus />
</AutoCompleteTextView>
</RelativeLayout>
The MainActivity.java is defined below.
public class MainActivity extends Activity {
String[] animals = {"Ant", "Bbird", "Cat"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating the instance of ArrayAdapter containing list of fruit names
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.select_dialog_item, animals);
//Getting the instance of AutoCompleteTextView
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
actv.setThreshold(1);//will start working from first character
actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
actv.setTextColor(Color.BLACK);
}
}
try this,
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="State:" />
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
java
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView autoCompleteTextView;
String[] arr={"used","new","repaired"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
autoCompleteTextView=findViewById(R.id.autoCompleteTextView);
ArrayAdapter aa=new ArrayAdapter(this,android.R.layout.simple_list_item_1,arr);
autoCompleteTextView.setAdapter(aa);
}
}
I have a layout file in which I have defined a ListView and another layout file in which I have defined the style of the individual list element. OnClick how do I populate my ListView by using the other layout? Or is it even possible to do so?
This is my ListView
<ListView
android:id="#+id/chat_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
>
</ListView>
<RelativeLayout
android:id="#+id/form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical">
</RelativeLayout>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/chat"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/send_button"/>
<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/icons_chat"
android:height="20dp"
android:width="20dp"
android:id="#+id/send_button"
android:onClick="sendChat"
android:layout_alignBottom="#+id/chat"
android:layout_alignParentRight="true"
/>
This is my other layout
<EditText
android:id="#+id/user_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/chat_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
OnClick I want to getText from the EditText in the first layout and update the ListView using the style in the second layout. How can I do that?
I think that one way to do it is with an ArrayAdapter which will adapt your Collection (which you want to use) to your items in your layout ListView for your case. For example
public class YourActivity extends Activity {
private ListView listView;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.your_layout);
listView= (ListView) findViewById(R.id.you_list_id);
//Here you can set values from your edit texts
ArrayList<String> yourArrayList = new ArrayList<>();
yourArrayList.add("someTextFromYourEditTexts");
yourArrayList.add("someTextFromYourEditTexts");
//Create your ArrayAdapter with your already populate array list
ArrayAdapter<String> yourArrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
yourArrayList );
//And when your adapter is create set it to your list view
listView.setAdapter(yourArrayAdapter);
}
}
I try to do simplest listView. I create layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/history_menu_item"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:padding="60dp" >
</TextView>
And ListActivity:
public class HistoryMenu extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
String[] values = new String[] { "Edit", "Send" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.history_menu, values);
setListAdapter(adapter);
}
}
Element of listView highlight not fully, only where text.
How to fix it?
Use this as your list item layout:
<?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="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/history_menu_item"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:padding="60dp" >
</TextView>
</LinearLayout>
Make changes in Textview
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/history_menu_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="60dp" />
I'm doing the Extra Credit section of the Adding a List section in Android Tutorials. I tried populating the AutoCompleteTextView (ACTV) with the items contained in the ArrayAdapter, but if the addr field has characters in it above the threshold limit, the list doesn't show any items. Here is the code:
public class MyActivity extends Activity
{
List<Restaurant> model = new ArrayList<Restaurant>();
ArrayAdapter<Restaurant> adapter = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button save = (Button)findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView list = (ListView)findViewById(R.id.restaurants);
adapter = new ArrayAdapter<Restaurant>(this,
android.R.layout.simple_list_item_1,
model);
list.setAdapter(adapter);
AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.addr);
textView.setAdapter(adapter);
}
private View.OnClickListener onSave= new View.OnClickListener(){
...
};
}
And the XML...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TableLayout
android:id="#+id/details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:shrinkColumns="1"
android:layout_alignParentBottom="true"
>
<TableRow>
<TextView android:text="Name:"/>
<EditText android:id="#+id/name"/>
</TableRow>
<TableRow>
<TextView android:text="Address:"/>
<AutoCompleteTextView android:id="#+id/addr"
android:completionThreshold="5"
/>
</TableRow>
<TableRow>
<TextView android:text="Type:"/>
<RadioGroup android:id="#+id/types">
<RadioButton android:id="#+id/take_out"
android:text="Take Out"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:checked="true"
/>
<RadioButton android:id="#+id/sit_down"
android:text="Sit Down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="#+id/delivery"
android:text="Delivery"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</RadioGroup>
</TableRow>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/save"
android:text="Save"/>
</TableLayout>
<ListView android:id="#+id/restaurants"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#id/details"/>
</RelativeLayout>
Changing the completionThreshold changes the number of characters I can enter before the items in the List disappear.
Thanks for the help
You are attempting to use the same adapter for both the ListView and the AutoCompleteTextView. That is not a good idea. Please use separate adapters. You might even want a separate layout for the adapter used with the AutoCompleteTextView (e.g., android.R.layout.simple_dropdown_item_1line, as I used in this sample project).
I am writing an Android app that uses an AutoCompleteTextView just like the API example. The problem is that my text is too long and needs to wrap when the dropdown occurs. I have a custom list_item.xml, but only one line of text displays. The list_item.xml layout wraps to 2 lines if used with a spinner, but not with the AutoCompleteTextView.
main.java
public class main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES);
textView.setAdapter(adapter);
}
static final String[] COUNTRIES = new String[] {
"This is the first line that is too long and it needs to wrap content",
"Albania", "Algeria", "American Samoa", "Andorra",
"This is a second line that also needs to wrap its content",
"Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium"
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Country" />
<AutoCompleteTextView android:id="#+id/autocomplete_country"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000"
>
</TextView>
Wrap the TextView in a layout and use that for your ArrayAdapter
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000"
/>
</LinearLayout>
And the new calls for the ArrayAdapter:
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.item, COUNTRIES);
textView.setAdapter(adapter);
easy way :
adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.two_line_list_item,android.R.id.text1,....);
android.R.layout.two_line_list_item this is easy change.