Showing posts with label Auto Complete Text with delete. Show all posts
Showing posts with label Auto Complete Text with delete. Show all posts

Saturday, December 09, 2017

Single Auto Selection and Entry with Delete

Single Auto Selection and Entry with Delete is used to select values from number of options and delete selected one.



Steps to use Single Auto Selection and Entry with Delete in your project.

  1. Crate a project(SingleSelectionWithDeleteDemo) in android studio.

  2. Create a interface CallBack.Java and update it-


    package com.pankaj.mmm;
    /**
     * Created by gsolc on 7/12/17.
     */
    public interface CallBacks {
        //to get event on select item
        void onSelected(String s);
        //to get event on delete item
        void onDeleted(int rowPosition);
        //to get event on done/enter click
        void onDone(String s);
    }
    
    
     
  3. Create a class MainActivity.Java and update it-


    package com.pankaj.mmm;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.View;
    import android.widget.Toast;
    import java.util.ArrayList;
    import java.util.List;
    public class MainActivity extends AppCompatActivity implements CallBacks {
        //create objects
        private RecyclerView rvTagList;
        private List<String> dataSelected = new ArrayList<>();
        private List<String> dataOptions = new ArrayList<>();
        private TagAdapter tagAdapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //Initialize list view
            rvTagList = findViewById(R.id.rvTagList);
            //Create layout manager
            LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
            //Set layout manager
            rvTagList.setLayoutManager(manager);
            //Add data to show entry point
            dataSelected.add("");
            //Create suggestions
            for (int i = 0; i < 500; i++) {
                dataOptions.add("data" + i);
            }
            //Initialize adapter
            tagAdapter = new TagAdapter(MainActivity.this, dataSelected, dataOptions);
            //Set callbacks
            tagAdapter.setCallBacks(this);
            //Set adapter
            rvTagList.setAdapter(tagAdapter);
        }
        @Override
        public void onSelected(String selected) {
            //Check size and put data into selecteddata list
            if (dataSelected.size() > 1) {
                dataSelected.add(dataSelected.size() - 1, selected);
            } else {
                dataSelected.add(0, selected);
            }
            scrollToLastPosition(false);
        }
        @Override
        public void onDeleted(int rowPosition) {
            //Delete selected item
            dataSelected.remove(rowPosition);
            scrollToLastPosition(true);
        }
        @Override
        public void onDone(String selected) {
            //Check size and put data into selecteddata list
            if (dataSelected.size() > 1) {
                dataSelected.add(dataSelected.size() - 1, selected);
            } else {
                dataSelected.add(0, selected);
            }
            scrollToLastPosition(true);
        }
        private void scrollToLastPosition(boolean goToLast) {
            //notify adapter to refresh data in it
            tagAdapter.notifyDataSetChanged();
    //        if (goToLast) rvTagList.scrollToPosition(dataSelected.size() - 1);
        }
        //function to show selected data
        public void selectedData(View view) {
            String message = "";
            for (String data : dataSelected) {
                if (!data.isEmpty())
                    message += data + "\n";
            }
            message.trim();
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        }
    }
    
    
    
    
    
     
  4. Create a class TagAdapter.Java and update it-


    package com.pankaj.mmm;
    import android.app.Activity;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.AutoCompleteTextView;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import java.util.List;
    /**
     * Created by gsolc on 7/12/17.
     */
    public class TagAdapter extends RecyclerView.Adapter<TagAdapter.MyViewHolder> {
        //Objects
        private String TAG = "TagAdapter";
        private List<String> dataOptions;
        private List<String> dataSelected;
        private Activity activity;
        private CallBacks callBacks;
        /**
         * View holder class
         */
        public class MyViewHolder extends RecyclerView.ViewHolder {
            private TextView tvText;
            private ImageView ivDelete;
            private AutoCompleteTextView tvAutocomplete;
            private LinearLayout llSelected;
            public MyViewHolder(View view) {
                super(view);
                tvAutocomplete = (AutoCompleteTextView) view.findViewById(R.id.tvAutocomplete);
                tvText = (TextView) view.findViewById(R.id.tvText);
                ivDelete = (ImageView) view.findViewById(R.id.ivDelete);
                llSelected = view.findViewById(R.id.llSelected);
            }
        }
        //Constructor
        public TagAdapter(Activity activity, List<String> dataSelected, List<String> dataOptions) {
            this.dataSelected = dataSelected;
            this.dataOptions = dataOptions;
            this.activity = activity;
        }
        @Override
        public void onBindViewHolder(final MyViewHolder holder, final int rowPosition) {
            if (rowPosition != dataSelected.size() - 1) {
    //        selected data
                holder.llSelected.setVisibility(View.VISIBLE);
                holder.tvAutocomplete.setVisibility(View.GONE);
                //Set data in cells
                holder.tvText.setText(dataSelected.get(rowPosition));
                //Add listener to delete cells
                holder.ivDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //send event to delete selected cell
                        callBacks.onDeleted(rowPosition);
                    }
                });
                //Set null to listeners
                holder.tvAutocomplete.setOnItemClickListener(null);
                holder.tvAutocomplete.setOnEditorActionListener(null);
            } else {
                //options in autocomplete
                //Hide cells
                holder.llSelected.setVisibility(View.GONE);
                holder.tvAutocomplete.setVisibility(View.VISIBLE);
                //Create adapter to show suggestion
                final ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, android.R.layout.simple_dropdown_item_1line, dataOptions);
                //Add adapter to show suggestion
                holder.tvAutocomplete.setAdapter(adapter);
                //Add listener to select item from suggestion
                holder.tvAutocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Log.e(TAG, holder.tvAutocomplete.getText() + " Position : " + position);
                        //send event to add selected suggestion in cells
                        callBacks.onSelected(holder.tvAutocomplete.getText().toString());
                        //reset entry point for new entry
                        holder.tvAutocomplete.setText("");
                    }
                });
                //Add listener to create cell of entered data
                holder.tvAutocomplete.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        Log.e(TAG, "" + actionId);
                        //check if pressed on Ok/Done button
                        if (actionId == 6) {
                            if (holder.tvAutocomplete.getText().toString().trim().length() > 0) {
                                //send event to add entered text in cells
                                callBacks.onDone(holder.tvAutocomplete.getText().toString().trim());
                                //reset entry point for new entry
                                holder.tvAutocomplete.setText("");
                            }
                        }
                        return false;
                    }
                });
            }
        }
        @Override
        public int getItemCount() {
            return dataSelected.size();
        }
        //Initialize callbacks interface
        public void setCallBacks(CallBacks callBacks) {
            this.callBacks = callBacks;
        }
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.row, parent, false);
            return new MyViewHolder(v);
        }
    }
    
    
    
    
     
  5. Create a xml row.xml to show individual selected item and update it-


     
     
     
     
    
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:minHeight="30dp">
        <LinearLayout
            android:id="@+id/llSelected"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="3dp"
            android:layout_marginTop="3dp"
            android:background="@drawable/bg_button_with_corner_orange"
            android:gravity="center_vertical"
            android:padding="3dp">
            <TextView
                android:id="@+id/tvText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:editable="false"
                android:singleLine="true"
                android:text="@string/app_name" />
            <ImageView
                android:id="@+id/ivDelete"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@android:drawable/ic_delete" />
        </LinearLayout>
        <AutoCompleteTextView
            android:id="@+id/tvAutocomplete"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:imeOptions="actionDone"
            android:layout_marginLeft="5dp"
            android:background="@android:color/transparent"
            android:hint="@string/app_name"
            android:singleLine="true"
            android:textSize="12sp" />
    </LinearLayout>
    
    
    
    
    
     
  6. Open your main_activity.xml file and update it-


    
    
    
     
     
     
     
    
    
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.pankaj.mmm.MainActivity">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rvTagList"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <Button
                android:onClick="selectedData"
                android:imeOptions="actionNone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Show entered Data" />
        </LinearLayout>
    </android.support.constraint.ConstraintLayout>
    
    
    
    
    

  7. All the application development process with Single Auto Selection and Entry with Delete has completed, Now run the app and look the screen. 









  8. Good bye, Thanks to read this blog.