AutoCompleteTextView: provide values in XML? - android

Is it possible to provide values to the AutoCompleteTextView in XML / via resources, therefore without setting an adapter in code?
I want to use the AutoCompleteTextView as part of an exposed dropdown menu, so all values shall be shown at once and no filtering shall happen. Also all values to be shown are known at compile time.

I think you can do that by using the list via Resource and implementing it in AutoCompleteTextView passing by the adapter.
See the example here :
public class CountriesActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
Resources res = getResources();
private static final String[] COUNTRIES = res.getStringArray(R.array.planets_array);
}
Hope this could help you or any one else .

Yes its possible. There is a method showDropDown() which you can call.
Try something like
autoCompleteTextView.showDropDown()
Reference :
https://developer.android.com/reference/android/widget/AutoCompleteTextView.html#showDropDown()

Related

Why my app containing listView is crashing?

My app contains only 2 files one is MainActivity.java and activity_main.xml file. I wanted to use to listView to display a list of string but for some my app is crashing continuously.
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> arrayList=new ArrayList<String>();
arrayList.add("Akshay");
arrayList.add("Akshay");
arrayList.add("Akshay");
arrayList.add("Akshay");
arrayList.add("Pandey");
arrayList.add("Akshay");
arrayList.add("Akshay");
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,0,arrayList);
ListView listView=(ListView) findViewById(R.id.list_item);
listView.setAdapter(adapter);
}
}
Error
Your app is crashing because you are passing 0 as the second argument to your ArrayAdapter constructor. This is the "layout resource id" that your list will use for each item, so it needs to be something real.
The Android platform comes with some resources for this purpose built in, like android.R.layout.simple_list_item_1. You could try that. Change this line:
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,0,arrayList);
to this instead:
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
More information can be found in the developer guide: https://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews
You can't set the second argument of ArrayAdapter to 0. The constructor expects a valid resource. You need to set it to something like this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.my_resource_view, arrayList);

Where is the layout file in my ListView?

I am new to android and i am confused about something. I have created a simple ListView, here is my code:
public class MainActivity extends ListActivity {
ArrayList<String> listItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listItems = new ArrayList<String>();
for(int i=0;i<20;i++){
listItems.add("List Item #"+i);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, listItems);
setListAdapter(adapter);
}
}
This correctly displays the list items on the screen, but here is my question: I have no xml files, no layouts in my project and i do not use setContentView function here. So why is this code working? How can it be displayed even though there is no xml files or layouts?
Thanks
You are using android.R.layout.simple_list_item_1 as the layout, which provides a TextView for you to work with. This is a layout provided by Android for simple lists of text.
If you want to make a more complex layout, you will/can define your own layout.
Android ships with default containers for list items.
As the name suggests, android.R.layout.simple_list_item_1 belongs to the android.R package and not to your app's resources.

Newbie setting up a ListView

I am just starting out using Android, so this is probably a very basic question.
I have created an array called priorityNames. I want to display that in a list and be able to make a selection from that list. At this stage, I cannot get the list to display.
As a starting point I used a short example from windrealm.org tutorials
Any help would be appreciated. Also if anyone can point me to a better example it would be appreciated.
public class SimpleListViewActivity extends Activity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, R.array.priorityNames);
mainListView.setAdapter(listAdapter);
}
}
You are passing just an id with R.array.priorityNames.
Use:
getResources().getStringArray(R.array.priorityNames)
You are probably missing a TextView in your xml layout (simplerow.xml) with id:
...android:id="#android:id/text1"...

Looking for examples on how to implement something like this in Android

My senior project group is developing an android application and we would like to be able to implement something like the example found in this screenshot:
What we have right now is a Spinner drop down, the very first default will be "Add faculty...". Once you create that faculty, it will be selectable in the spinner, but in case a class has more than one faculty(ie: Professor, Teaching Assistant), we want to be able to add more than one spinner using the +/- buttons like you see here.
Any examples or leads in the right direction would be most appreciated.
Create two variable of ArrayAdapter and Arraylist of string to be global.
ArrayList<String> art;
ArrayAdapter<String> adapter;
And then in onCreate initialize the spinner as below
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
s1 = (Spinner) findViewById(R.id.spinner);
art=new ArrayList<String>();
art.add("professer");
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item,art);//Items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
}
And then in button click , add new elements in array and notify the adapter
public void btn_click(View v)
{
art.add("Technical Assistant");
adapter.notifyDataSetChanged();
}

ListActivity adding images?

i have a following ListActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get Strings from res/strings.xml
String items[] = { getString(R.string.mainMenu_1),
getString(R.string.mainMenu_2), getString(R.string.mainMenu_3),
getString(R.string.mainMenu_4), getString(R.string.mainMenu_5)};
strings = items;
ListAdapter listAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
setListAdapter(listAdapter);
}
Displaying it and reacting to user clicks works great, now I want to add some pictures before the text (like "Settings").
can someone explain me how to do this?
(How to get pictures from res/drawable isn needed to get explained ;-))
greets,
poeschlorn
You have to make your own ListAdapter. Here's a fine tutorial about this: http://www.anddev.org/novice-tutorials-f8/iconified-textlist-the-making-of-t97.html?sid=57c1096099cb666b386eb4ab65aba0c6

Categories

Resources