Search This Blog

04 May 2010

Auto Complete

Getting back to basics, let's start with creating a base project.
  • mvn archetype:generate -DarchetypeCatalog=http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml
  • select the galatea archetype
  • specify your groupId, artifactId and version.  In my case, I chose 'org.eoti.galatea', 'AutoComplete' and '1.0-SNAPSHOT'.
  • Make sure your emulator is running... In my case, that was 'emulator -avd Device1
  • Change into your new directory (whatever your artifactId name was) and do a 'mvn clean install
At this point, we should have a basic yet bland hello world project in your emulator.  Make sure it launches before we continue.

Now, we are going to modify our project as per this tutorial.

In src/main/android/layout create an li.xml file:
<?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="fill_parent"
    android:padding="10dp"
    android:textSize="16sp"
    android:textColor="#000">
</TextView>
Now, replace src/main/android/layout/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>
Open MyActivity.java and add these lines into the end of the onCreate method:
        AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
        String[] countries = getResources().getStringArray(R.array.countries_array);
        ArrayAdapter adapter = new ArrayAdapter(this, R.layout.li, countries);
        textView.setAdapter(adapter);
Open src/main/android/res/values/strings.xml and add this inside the resources tag:
    <string-array name="countries_array">
        <item>Bahrain</item>
        <item>Bangladesh</item>
        <item>Barbados</item>
        <item>Belarus</item>
        <item>Belgium</item>
        <item>Belize</item>
        <item>Benin</item>
    </string-array>
Redeploy (mvn clean install) and launch the app.  Start typing.  If you type either a 'ba' or 'be' you will see a small subset of options.

No comments:

Post a Comment