Learner_51 Learner_51 - 4 months ago 9
Java Question

How to Get user input from a Text Entry Box in Android?

in an android website, I found an article about how to create a text entry widget that provides auto-complete suggestions. (Following is the link to the site; and it shows all the codes).

http://developer.android.com/resources/tutorials/views/hello-autocomplete.html

Can anyone please tell me how I can capture the input entered by the user? For example, if the user chooses “Canada”, is there a way I can know the result in the “HelloAutoComplete.java” activity? Any help would be greatly appreciated.

public class HelloAutoComplete extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
String[] countries = getResources().getStringArray(R.array.countries_array);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, countries);
textView.setAdapter(adapter);

textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (adapterView.getItemAtPosition(i).toString().equals("Canada")) {
Toast.makeText(getApplicationContext(), "Result Canada", Toast.LENGTH_SHORT).show();
} //Does not get an out put when I select Canada.
}

public void onNothingSelected(AdapterView<?> adapterView) {
}
});

} }

Answer
textView.getText();

textView.getText().toString(); // If you need an actual String

Currently your code does not hook up the listener to the text view.

You need to either (a) use an immediate listener:

textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        if (adapterView.getItemAtPosition(i).toString().equals("Canada")) {
            Toast.makeText(getApplicationContext(), "Result Canada", Toast.LENGTH_SHORT).show();
        }
    }

    public void onNothingSelected(AdapterView<?> adapterView) {
    }
});

Or (b) if you want to use the activity as the listener (I wouldn't) have it implement the interface and set the itemSelectedListener to this. But yuck.


To set the selected text into another text element, a few changes must be made. First, the layout must now include the autocomplete "component", and the additional text view. We set the "parent" layout to vertical orientation, create a new horizontal layout for the autoselect stuff, and add a new text view.

<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
              a:orientation="vertical"
              a:layout_width="fill_parent"
              a:layout_height="wrap_content"
              a:padding="5dp">

  <LinearLayout a:orientation="horizontal"
                a:layout_width="fill_parent"
                a:layout_height="wrap_content">
    <TextView a:layout_width="wrap_content"
              a:layout_height="wrap_content"
              a:text="Country"/>

    <AutoCompleteTextView a:id="@+id/autocomplete_country"
                          a:layout_width="fill_parent"
                          a:layout_height="wrap_content"
                          a:layout_marginLeft="5dp"/>
  </LinearLayout>

  <TextView a:id="@+id/selected_country"
            a:layout_height="wrap_content"
            a:layout_width="fill_parent"/>
</LinearLayout>

The activity gets a new TextView instance property, which I'm calling selectedCountry. I'm not showing its declaration. The onCreate method looks it up by ID, and the select listener just updates it.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Text view for selected country.
    selectedCountry = (TextView) findViewById(R.id.selected_country);

    textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            selectedCountry.setText(adapterView.getItemAtPosition(i).toString());
        }
        public void onNothingSelected(AdapterView<?> adapterView) { }
    });
}
Comments