Skip to main content

Android AutoCompleteTextView with Text Highlighter

 We all know about AutoCompleteTextView , It is an editable text view which shows a list of suggestions when user starts typing text. Basically it is use for filter the list based on entered characters. In this post we will create custom AutoCompleteTextView with Text Highlighter .So lets move to the coding part.

Step 1: Create spinner_item_divider.xml file under drawable folder

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    >
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#000000"/>
        </shape>
    </item>
    <item android:bottom="0.5dp">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff"/>
        </shape>
    </item>
</layer-list>

Step 2: Create activity_auto_complete_tvmain.xml under layout folder

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
       android:layout_gravity="center"
        android:layout_height="wrap_content">

        <com.droidmedium.AutoCompleteTV
            android:id="@+id/languages"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:completionThreshold="1"
            android:hint="language"
            android:imeOptions="actionNext"
            android:maxLines="1"
            android:paddingLeft="10dp"
            android:paddingTop="15dp"
            android:paddingRight="10dp"
            android:paddingBottom="15dp"
            android:singleLine="false"
            android:textColor="#333333"
            android:textColorHint="#808080"
            android:textSize="12sp" />

    </android.support.design.widget.TextInputLayout>

</LinearLayout>

Step 3: Again create row_dropdown.xml under layout folder

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textview"
    style="?android:attr/dropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:ellipsize="marquee"
    android:paddingTop="8dp"
    android:paddingBottom="8dp"
    android:background="@drawable/spinner_item_divider"
    android:singleLine="true"
    android:textColor="#333333"
    android:textSize="15sp"
    tools:text="text"
    />

Step 4: Now Create AutoCompleteAdapter.java

package com.droidmedium;

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;


import java.text.Normalizer;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class AutoCompleteAdapter<T> extends ArrayAdapter<T> implements Filterable {

    private Context context;
    @LayoutRes
    private int layoutRes;
    @IdRes
    private int textViewResId;
    private ArrayList<T> fullList;
    private ArrayList<T> originalValues;
    private ArrayFilter filter;
    private LayoutInflater inflater;
    private String query = "";

    public AutoCompleteAdapter(@NonNull Context context, @LayoutRes int resource, @IdRes int textViewResourceId, @NonNull List<T> objects) {
        super(context, resource, textViewResourceId, objects);
        this.context = context;
        layoutRes = resource;
        textViewResId = textViewResourceId;
        fullList = (ArrayList<T>) objects;
        originalValues = new ArrayList<>(fullList);
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return fullList.size();
    }

    @Override
    public T getItem(int position) {
        return fullList.get(position);
    }

    @Override
    public @NonNull
    View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return createViewFromResource(inflater, position, convertView, parent, layoutRes);
    }

    private @NonNull
    View createViewFromResource(@NonNull LayoutInflater inflater, int position,
                                @Nullable View convertView, @NonNull ViewGroup parent, int resource) {
        final View view;
        final TextView text;

        if (convertView == null) {
            view = inflater.inflate(resource, parent, false);
        } else {
            view = convertView;
        }

        try {
            if (textViewResId == 0) {
                //  If no custom field is assigned, assume the whole resource is a TextView
                text = (TextView) view;
            } else {
                //  Otherwise, find the TextView field within the layout
                text = view.findViewById(textViewResId);

                if (text == null) {
                    throw new RuntimeException("Failed to find view with ID "
                            + context.getResources().getResourceName(textViewResId)
                            + " in item layout");
                }
            }
        } catch (ClassCastException e) {
            Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
            throw new IllegalStateException("ArrayAdapter requires the resource ID to be a TextView", e);
        }

        final T item = getItem(position);
        text.setText(highlight(query, item.toString()));

        return view;
    }

    @Override
    public @NonNull
    Filter getFilter() {
        if (filter == null) {
            filter = new ArrayFilter();
        }
        return filter;
    }

    private class ArrayFilter extends Filter {
        private final Object lock = new Object();

        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();
            if (prefix == null) {
                query = "";
            } else {
                query = prefix.toString();
            }
            if (originalValues == null) {
                synchronized (lock) {
                    originalValues = new ArrayList<>(fullList);
                }
            }

            if (prefix == null || prefix.length() == 0) {
                synchronized (lock) {
                    ArrayList<T> list = new ArrayList<>(originalValues);
                    results.values = list;
                    results.count = list.size();
                }
            } else {
                final String prefixString = prefix.toString().toLowerCase();
                ArrayList<T> values = originalValues;
                int count = values.size();

                ArrayList<T> newValues = new ArrayList<>(count);

                for (int i = 0; i < count; i++) {
                    T item = values.get(i);
                    if (item.toString().toLowerCase().contains(prefixString)) {
                        newValues.add(item);
                    }

                }
                results.values = newValues;
                results.count = newValues.size();
            }

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results.values != null) {
                fullList = (ArrayList<T>) results.values;
            } else {
                fullList = new ArrayList<>();
            }
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

    }

    private static CharSequence highlight(@NonNull String search, @NonNull CharSequence originalText) {
        if (search.isEmpty())
            return originalText;

        // ignore case and accents
        // the same thing should have been done for the search text
        String normalizedText = Normalizer
                .normalize(originalText, Normalizer.Form.NFD)
                .replaceAll("\\p{InCombiningDiacriticalMarks}+", "")
                .toLowerCase(Locale.ENGLISH);

        int start = normalizedText.indexOf(search.toLowerCase(Locale.ENGLISH));
        if (start < 0) {
            // not found, nothing to do
            return originalText;
        } else {
            // highlight each appearance in the original text
            // while searching in normalized text
            Spannable highlighted = new SpannableString(originalText);
            while (start >= 0) {
                int spanStart = Math.min(start, originalText.length());
                int spanEnd = Math.min(start + search.length(),
                        originalText.length());

                highlighted.setSpan(new StyleSpan(Typeface.BOLD), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

                start = normalizedText.indexOf(search, spanEnd);
            }

            return highlighted;
        }
    }
}

Step 5: Create AutoCompleteTV.java

package com.droidmedium;

import android.content.Context;
import android.support.v7.widget.AppCompatAutoCompleteTextView;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class AutoCompleteTV extends AppCompatAutoCompleteTextView {

    public AutoCompleteTV(Context context) {
        super(context);
    }

    public AutoCompleteTV(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoCompleteTV(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            performClick();
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean performClick() {
        super.performClick();
        return true;
    }
}

Step 6: Create AutoCompleteTVMain.java

package com.droidmedium;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class AutoCompleteTVMain extends AppCompatActivity {
  
    AutoCompleteTextView acTextView;
    AutoCompleteAdapter adapter;
    List<String> wordList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_complete_tvmain);


         wordList = new ArrayList<String>();
         wordList.add("C");
         wordList.add("C++");
         wordList.add("JAVA");
         wordList.add("C#");
         wordList.add("PHP");
         wordList.add("JavaScript");
         wordList.add("jQuery");
         wordList.add("AJAX");
         wordList.add("JSON");


         adapter = new AutoCompleteAdapter(this,
                R.layout.row_dropdown,
                R.id.textview,wordList);
        acTextView = (AutoCompleteTextView) findViewById(R.id.languages);
             acTextView.setThreshold(1);
            acTextView.setAdapter(adapter);


        acTextView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent motionEvent) {

                    v.requestFocus();
                    v.performClick(); 

                return false;
            }
        });


        acTextView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                  String text = acTextView.getText().toString().toLowerCase(Locale.getDefault());

                  setnewMethod(text);

            }

            @Override
            public void afterTextChanged(Editable editable) {


            }
        });


        acTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String selectedis=adapterView.getItemAtPosition(i).toString();
                System.out.println("selected item is"+selectedis);
            }
        });


    }


    private void setnewMethod(String text)
    {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

// perform your action here to refresh list or get new list from server use wordList.clear(); method and add new list

                 adapter = new AutoCompleteAdapter(AutoCompleteTVMain.this, R.layout.row_dropdown, R.id.textview,wordList);
                 acTextView.setThreshold(1);   // start from first character
                 acTextView.setAdapter(adapter);
                 adapter.notifyDataSetChanged();

            }
        });

    }



}

Now that all for coding part run the app and you will find the result as 




Thank you and if you like this article Share this on your social platform .😇😇😇


Comments

Popular posts from this blog

How to Download Apk file from Url and Install Programmatically

In this post we learn about download apk file from server or website and then install it Programmatically in Phone. Sometimes we have to download external apk file from server and then install if downloading successfully finished.For this we use AsyncTask class  for background process. So here is Code Snippet for this task.Lets Start :- Before this we have to add these Permissions in Manifest.xml file : <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> DownloadNewVersion.java class DownloadNewVersion extends AsyncTask<String,Integer,Boolean> { @Override protected void onPreExecute() { super.onPreExecute(); bar = new ProgressDialog(getActivity()); bar.setCancelable(false); bar.setMessage("Downl

How to open pdf file url using webview in Android

 Sometimes we have requirement of showing pdf file in our Android Application. Although there are many third party gradle dependencies available online by which you can show your pdf file easily but one of the major drawback of using these libraries are they will increase you apk size .In some cases they will increase apk size upto ~14 MB. Alternatively you can access and show pdf file using assets and from storage options. This can take less size than any third party library. Here we use pdf using url and if you directly open your pdf file url in webview you will get no result because it will start downoad if you open this link in any web browser.  So in this article we will learn how we can open our pdf url using webview and without using any third party library. So lets Start step by Step:- Lets Suppose you have a pdf file url something like   http://yourwebsite.com/files/mydocumentfile.pdf Step 1: So we you this pdf file url to embed in our webview .First of all you have to host yo

How to use UPI Deep Linking in Android

 In this Article we will discuss about the UPI Deep linking. By this Deep linking we can perform UPI Payments through intent .Basically it open the All UPI supported Application in our mobile then user choose their preference and initiate the Payment and through this we get the payment credentials like payment id ,transaction id ,etc. without any payment gateway. So lets start with what is UPI? Unified Payments Interface(UPI) is an instant real-time payment system developed by National Payments Corporation of India facilitating inter-bank transactions. The interface is regulated by the Reserve Bank of India and works by instantly transferring funds between two bank accounts on a mobile platform. (from Wikipedia) Coding part: package com.example.droidmedium; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Butt