Is there an (reusable) Android user interface component for an input field that supports autocompletion and suggestions?

I am developing a browser application for Android 2.2. Of course, this application requires a user interface element to enter the URL. Is there a way to reuse user interface components, such as those used in the default Android browser, Dolphin HD or Fennec? I do not want to rebuild all functions from scratch, like - autocomplete / Suggestions from Google - automatic selection of a domain name (without "www.")

+3
source share
2 answers

I don't know the exact behavior of each input type, but maybe this will help: https://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType

If you find a good type, please give a short feedback.

+2
source

Now I found an interesting thing: there is a widget called AutoCompleteTextView .

You need to install an adapter that stores available automatically completed records. If you catch a text change event, you only need to get the current input from the text view and implement a search for available URLs and add the result to the array adapter. These new entries will appear as available automatically completing the current entry.

:

public class AutoCompleteTest
    extends Activity
    implements TextWatcher
{

private AutoCompleteTextView autoCompleteView;

/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(layoutRes);
    this.autoCompleteView = (AutoCompleteTextView) findViewById(viewId);
    this.autoCompleteView.addTextChangedListener(this);
    this.autoCompleteView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}

public void onTextChanged(final CharSequence currentSubmission, final int start, final int before, final int count)
{
    new Thread() {

        @Override
        public void run()
        {
            // your code for retrieving url for the current input
            // maybe a remote service request
            final String[] urls;

            final Bundle data = new Bundle();
            data.putStringArray("urls", urls);

            final Message msg = new Message();
            msg.setData(data);
            MultiTouchTest.this.messageHandler.sendMessage(new Message());
        }

    }.start();
}

public void afterTextChanged(final Editable s)
{
    //  do nothing
}

public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after)
{
    //  do nothing
}

private final Handler messageHandler = new Handler() {

    @Override
    @SuppressWarnings("unchecked")
    public void handleMessage(final Message msg)
    {
        final ArrayAdapter<String> adapter = (ArrayAdapter<String>) MultiTouchTest.this.autoCompleteView.getAdapter();
        final String[] urls = msg.getData().getStringArray("urls");
        for (final String url : urls)
        {
            adapter.add(url);
        }
        adapter.notifyDataSetChanged();
    }

};

}

, , URL-.

+2

Source: https://habr.com/ru/post/1776278/


All Articles