Android: Make AutoCompleteTextView dropdown wrap to 2 lines or more - android

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.

Related

Android: ArrayAdapter showing only one item even though the ListView isn't placed in a ScrollView

I am new to programming in Android. I encountered a problem where I need to print three items in a list View, yet getting only a single item. In many answers, I saw that ListView should't be placed in a ScrollView, I have rechecked my code, and that isn't actually the problem.
package com.apress.gerber.reminder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class RemindersActivity extends AppCompatActivity {
private ListView mListView;
private String records[] = {"first record", "second record", "third record"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminders);
mListView = (ListView) findViewById(R.id.reminders_list_view);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
R.layout.reminders_row,
R.id.row_text,
records);
mListView.setAdapter(arrayAdapter);
}
}
Xml files:-
Reminders_row.xml
<?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="50dp"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<view
android:id="#+id/row_tab"
class="android.view.View"
android:layout_width="10dp"
android:layout_height="match_parent"
android:background="#android:color/holo_green_light"
/>
<TextView
android:id="#+id/row_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="#string/reminder"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white"
android:textSize="18sp"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:ellipsize="end"
android:maxLines="1"/>
</LinearLayout>
</LinearLayout>
activity_reminders.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"
tools:context="com.apress.gerber.reminder.RemindersActivity">
<ListView
android:id="#+id/reminders_list_view"
android:layout_width="373dp"
android:layout_height="50dp"
android:background="#color/dark_grey"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"
tools:listitem="#layout/reminders_row"/>
</RelativeLayout>
What actually is the error occuring?
If you are not going to extend your class with ArrayAdapter and customize getView method but you want to use your customized layout than your TextView must have id android:id="#android:id/text1" Or you can use android.R.layout.simple_list_item_1 in your ArrayAdapter like:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
records);
mListView.setAdapter(arrayAdapter);
Also for your ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"

Android AutoCompleteTextView result in List instead of default dropdown

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.

Element of listView highlight not fully

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" />

Item and SubItem in ListView

I want to show Item and corresponding subitem in list view using Arrayadapter and android.R.layout.simple_list_item_2. I am able to list items with following code
XML file:
<?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"
android:orientation="vertical"
android:paddingLeft="8dp" >
<ListView android:id="#android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView android:id="#android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Empty set" />
java file:
public class Lists extends ListActivity {
String[] listItems = {"Item1", "Item2 ", "Item3", "Item4"};
String[] SubItems = {"SubItem1", "SubItem2", "SubItem3", "SubItem4"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListAdapter adp = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
setListAdapter(adp);
}
}

Trying to use a listview in .xml with a cursor but without a ListActivity

I'm stumped as to how to properly implement an adapter that can pull a column of data from my sqlite database and add it to a listview (based on the android-provided multiple checkbox). All of the existing examples use the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] myList = new String[] {"Hello","World","Foo","Bar"};
ListView lv = new ListView(this);
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myList));
setContentView(lv);
}
However, I don't want a string. Here's my code:
public class ListGroups extends Activity {
/** Called when the activity is first created. */
private ExpensesDbAdapter mDBHelper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
ListView lv = (ListView)findViewById(R.id.list1);
mDBHelper = new ExpensesDbAdapter(ListGroups.this);
mDBHelper.open();
Cursor c = mDBHelper.fetchAllGroups();
startManagingCursor(c);
String[] from = new String[] {ExpensesDbAdapter.KEY_GROUPNAME};
int[] to = new int[] {R.id.groupname};
ListAdapter ladapter = new SimpleCursorAdapter(ListGroups.this, R.layout.grouprow, c, from, to);
lv.setAdapter(ladapter);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
Here's the view.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView android:id="#+id/header01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Select Groups"/>
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/footerbutton01"
android:text="Get Expense Reports" />
</LinearLayout>
And, because the SimpleCursorAdapter asks for it, here's the grouprow.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/groupname"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"/>
If I use a ListActivity and don't invoke the layout on Creation, I can get it to work, but I need the wrappers around that ListView, and I can't make the .addHeader and .addFooter methods work for me. Any help on creating the proper adapter would be appeciated.
Answered it with some help. Several things were wrong, mostly in the list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView android:id="#+id/header01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Select Groups"/>
<ListView
android:id="#android:id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_around"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/footerbutton01"
android:text="Get Expense Reports" />
</LinearLayout>
This allows you to tie the ListView to that element in the xml. You don't have a name for it, but you can use getListView() in the onCreate method to access it.

Categories

Resources