ComboBox is a very useful widget that allows user to type the text in or select from predefined list of options.
While Android comes with Spinner, it is not always what we need. Spinner is a Listbox that only lets user to chose from predefined options.
It is fairly easy to create "sort of" ComboBox however. Here is how:
package myWidgets;
import android.content.Context;
import android.database.Cursor;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
public class ComboBox extends LinearLayout {
private AutoCompleteTextView _text;
private ImageButton _button;
public ComboBox(Context context) {
super(context);
this.createChildControls(context);
}
public ComboBox(Context context, AttributeSet attrs) {
super(context, attrs);
this.createChildControls(context);
}
private void createChildControls(Context context) {
this.setOrientation(HORIZONTAL);
this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
_text = new AutoCompleteTextView(context);
_text.setSingleLine();
_text.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_NORMAL
| InputType.TYPE_TEXT_FLAG_CAP_SENTENCES
| InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE
| InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
_text.setRawInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
this.addView(_text, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1));
_button = new ImageButton(context);
_button.setImageResource(android.R.drawable.arrow_down_float);
_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
_text.showDropDown();
}
});
this.addView(_button, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
/**
* Sets the source for DDLB suggestions.
* Cursor MUST be managed by supplier!!
* @param source Source of suggestions.
* @param column Which column from source to show.
*/
public void setSuggestionSource(Cursor source, String column) {
String[] from = new String[] { column };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this.getContext(),
android.R.layout.simple_dropdown_item_1line, source, from, to);
// this is to ensure that when suggestion is selected
// it provides the value to the textbox
cursorAdapter.setStringConversionColumn(source.getColumnIndex(column));
_text.setAdapter(cursorAdapter);
}
/**
* Gets the text in the combo box.
*
* @return Text.
*/
public String getText() {
return _text.getText().toString();
}
/**
* Sets the text in combo box.
*/
public void setText(String text) {
_text.setText(text);
}
}

Hello all!!
I'm a newbie on Android Dev so, sorry about my question, how can I use Enum?
I mean, I have one and would like to use instead cursor in your component, Is it possible?
Enums are generally discouraged.
Use constants defined on the class.
You will have to populate cursor or list manually. If you use list you will have to change the setSuggestionSource() function to take ArrayAdapter.
Thank you gnugu!!!
How can I make it? Do you have an exemple?
I've been implementing these events but they don't work, the compiler accept my code but don't work, do nothing. Do you know what is it appening?
The code is the follow:
in MainActivity:
comboAction comboListener = new comboAction();
combo.setOnClickListener(comboListener);
combo.setOnFocusListener(comboListener);
:
:
private class comboAction implements
MyWidgets.ComboBox.OnClickListener,
MyWidgets.ComboBox.OnFocusChangeListener
{
@Override
public void onClick(View arg0)
{
//just put a message in a var
textvar.setText("onClick");
}
@Override
public void onFocusChange(View arg0,
boolean isFocused)
{
if (isFocused == true)
{
//just put a message in a var
textvar.setText("Focused");
}
else
{
//just put a message in a var
textvar.setText("Out of focus");
}
}
}
I'm not sure what exactly you want to achieve.
Both the AutoCompleteTextView and ImageButton do receive click events. Otherwise they would not open the drop down.
Ok, Tranks...
Sometimes it is necesary these events because in my case I need to fill a combo depending of the value selected in another one.
I fill the combobox with data with this code:
SQLiteDatabase db;
db = openOrCreateDatabase("BD.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
final MyWidgets.ComboBox combo = (MyWidgets.ComboBox)findViewById(R.id.combo);
Cursor cur = db.query("tabla", null, null, null, null, null, null);
combo.setSuggestionSource(cur, "name");
----------------
BD exists, "tabla" exists (have 2 fields "-id" and "name"), have records, the cursor works, but when run my app there is nothing in combo.
Anybody could help me please!!
The only difference between yours and mine is that I have:
Cursor cur = db.query("tabla", null, null, null, null, null, null); //*** we are responsible for managing cursor this.startManagingCursor(cur); combo.setSuggestionSource(cur, "name");Thanks now it is working, but it wasn't easy because at first my app not show me the data, but i discovered i have to mantain open the Database and the Cursor.
Another question, how can i change the text size?
>>Another question, how can i change the text size?
I didn't play with that. My guess is do the usual Android styling.
Ok thank you very much... it is a good control.
when I add ComboBox componet in my layout file ,my app does not work.
please give me a example.
thanks.
could you show me an example how to use this class?
I tried to use it but really don't know how to do it.
thanks
Once you add the class, it becomes a widget as any other. This is how you can use it in your layout file:
If you need to use it in the code, you simply find it as an element by id and you can access all its public members.
Hai,
could you explain how to add text filter added in to combobox search, and also i would like to change matching text color changed while searching.
Please help me.
Thanks in advance.
Thanks it's working for me too.
Now i trying to fill the combobox with data from a table.
Any help is wellcomed.
It works.thanks.
Please explain how to add filters for drop down items and also change text color based on the search text.
P.Venkat