Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Should've been easy: Autocomplete text in a dialog box

Sign in to follow this  


A few weeks ago I threw up a quick example about dynamically building Expandable Lists, using a simple app called TopicNotes. While it is simple, I like to tinker with it here and there to test features I'm unfamiliar with. About a week ago I was playing with TopicNotes and decided pretty quickly that it needed an autocomplete feature for entering Topics to help avoid unintentional multiple versions of the same topic (I found I had a 'Study hall' topic and a 'Study Hall' topic). I could have done a variety of string checks to avoid this, but decided that adding an AutoCompleteTextView would be the most straightforward way to go. There are a number of good tutorials and examples of using AutoCompleteTextView readily available online. It is very easy to implement in any typical Activity.

However, when you use it in a dialog box, you have to explicitly tie it to the desired dialog in a way that isn't immediately evident (at least to me) in the available tutorials. My dialog box crashed time after time, and I had narrowed my problem down to the ArrayAdapter initially. As it turned out, it was a domino effect: the ArrayAdapter was crashing because the app was apparently confused about who owned the findViewById that was taking the dialog layout that was getting the AutoCompleteTextView.

Anyway, here's the code that works, in case anyone else finds themselves in this situation.

[source lang= "java"]
public void onCreate(Bundle savedInstanceState) {

// topicnotes database
topic_notes_data = new TopicNotesData(this);

// The button to add new notes
final Button btnAddTopicNote = (Button) findViewById(R.id.Button_AddTopicNote);
btnAddTopicNote.setOnClickListener(new View.OnClickListener() {

// Here's where the custom dialog is created
public void onClick(View v) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setTitle("Add Topic Note");

// this is where we need to have dialog explicitly call findViewById
final AutoCompleteTextView topicText = (AutoCompleteTextView) dialog.findViewById(R.id.AutoCompleteTopic);
ArrayAdapter adapter = new ArrayAdapter(context,R.layout.list_item,getAllTopics());

Button saveButton = (Button)dialog.findViewById(R.id.Button_SaveTopicNote);
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(topicText.getText() != null){
String strTopic = topicText.getText().toString();
EditText noteText = (EditText)dialog.findViewById(R.id.EditText_Note);

String strNote = noteText.getText().toString();

Button cancelButton = (Button)dialog.findViewById(R.id.Button_CancelTopicNote);
cancelButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {



Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!