Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6832051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:48:41+00:00 2026-05-26T22:48:41+00:00

in an android website, I found an article about how to create a text

  • 0

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) {
        }
    });

}  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:48:42+00:00Added an answer on May 26, 2026 at 10:48 pm
    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) { }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In an android website, I found an article about a widget similar to a
I followed the example in the Android website to create a tab activity with
I've been keen to learn about triggers in Android and SQLite. I found a
On the Android developers website, there is a Notepad tutorial and I found a
I'm following the MapView tutorial on the Android website [2010-08-22 11:12:24 - com.android.ide.eclipse.adt.internal.project.AndroidManifestParser] Parser
I'm planning to use my website to provide updates for my android application whenever
I am making an HTTP get request to a website for an android application
Android comes with lots of system resources ( android.R ) that can be used
When android unbind a service I created (service.MyService), I see the following DeadObjectException. Can
I have my website that I am creating here and it's looking good (right

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.