Android tutorial

Android MultiAutoCompleteTextView With API Example Tutorial

Hiii Everyone in this article I am sharing how to use MultiAutoCompleteTextView with using API data. This is a very easy way to show API data in MultiAutoCompleteTextView. And add more text if Data Not Available in your API you can add Manual.

AutocompleteTextView is an editable text view that shows completion suggestions automatically while the user is typing in android apps. In this tutorial, we’ll implement android MultiAutoCompleteTextView in our application using an ArrayAdapter to define the list of suggestions.

Some important methods of the Autocomplete list are given below:

  1. getAdapter() : This method returns a filterable list adapter used for auto completion
  2. getCompletionHint(): This method returns optional hint text displayed at the bottom of the matching list
  3. getDropDownAnchor(): This method returns the id for the view that the auto-complete drop-down list is attached to
  4. getListSelection() : This method returns the position of the dropdown view selection, if there is any
  5. isPopupShowing(): This method indicates whether the popup menu is showing
  6. setText(CharSequence text, boolean filter): This method sets text except that it can disable filtering
  7. showDropDown(): This method displays the drop down on the screen.

Step 1: First one to  Start Android Studio

Step 2 :  Seconds step to Create a New Project Project ClickOn  ==> File  ==> NEW ==> New Project

Step 3: After create on your project open your java file and XML file and you can just copy the code and paste on your file and run your project.

Step 4: Open Your XML File and add these Code.

<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="top"
android:padding="10dp"
android:textAppearance="?android:textAppearanceSmall"
android:inputType="textMultiLine"
android:background="@drawable/edit_text"
android:ems="10"
android:hint="Enter here" />

 

Step 5: Open Your Java File and Add these Code.

public class Home extends AppCompatActivity {
    public static String Machine_Id ="";
    List<String> MachineList;
    List<String> MachineListId;
    MultiAutoCompleteTextView multiAutoCompleteTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        multiAutoCompleteTextView = findViewById(R.id.multiAutoCompleteTextView);
        MachineList=new ArrayList<>();
        MachineListId=new ArrayList<>();
        if (AppStatus.getInstance(this).isOnline()) {
            Machine();

        } else {
            ContextThemeWrapper ctw = new ContextThemeWrapper(Add_Tretment.this, R.style.Theme_AlertDialog);
            final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw);
            alertDialogBuilder.setTitle("No internet connection");
            alertDialogBuilder.setMessage("Check your  internet connection or try again");
            alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                }
            });
            alertDialogBuilder.show();
        }
    }

    private void Machine() {
        final ProgressDialog loading = new ProgressDialog(Add_Tretment.this);
        loading.setMessage("Please Wait...");
        loading.show();
        StringRequest stringRequest = new StringRequest(Request.Method.POST, ConfiURL.All_MachineList_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            Log.d("JSON", response);
                            loading.dismiss();
                            JSONObject eventObject = new JSONObject(response);
                            String error_status=eventObject.getString("status");
                            if(error_status.equals("0")){
                                String error_msg=eventObject.getString("message");
                                Toast.makeText(Add_Tretment.this, error_msg, Toast.LENGTH_SHORT).show();
                            }
                            else{
                                StateDropdownObject(eventObject);
                            }
                        } catch (Exception e) {
                            Log.d("Tag",e.getMessage());

                        }
                    }
                },

                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        loading.dismiss();
                        Toast.makeText(Add_Tretment.this,"no internet Connection", Toast.LENGTH_SHORT ).show();
                    }
                }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<String,String>();
                return map;
            }
        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }
    /*hare to pares the json response in list and show to spener*/
    public void StateDropdownObject(JSONObject Ward) {
        String State_list,State_Id;


        try {
            JSONArray projectNameArray = Ward.getJSONArray("allData");
            MachineList.add("Select Machine");
            MachineListId.add("Select Machine");
            for (int i = 0; i <= projectNameArray.length(); i++) {
                JSONObject obj = projectNameArray.getJSONObject(i);
                State_Id=obj.getString("id");
                State_list = obj.getString("name");
                Log.d("name", State_list);
                MachineList.add(State_list);
                MachineListId.add(State_Id);
                //Log.d("Dropdown",Dropdown.get(i));
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        ArrayAdapter<String> randomArray = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, MachineList);
        multiAutoCompleteTextView.setAdapter(randomArray);
        multiAutoCompleteTextView.setThreshold(1);
        multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    }

 

https://www.youtube.com/watch?v=JlkSb-HlWGQ