2424import androidx .recyclerview .widget .LinearLayoutManager ;
2525import androidx .recyclerview .widget .RecyclerView ;
2626
27+ import android .app .Dialog ;
28+ import android .content .Context ;
29+ import android .content .res .ColorStateList ;
30+ import android .os .Bundle ;
31+ import android .util .Log ;
32+ import android .view .LayoutInflater ;
33+ import android .view .View ;
34+ import android .widget .ImageView ;
35+ import android .widget .LinearLayout ;
36+ import android .widget .TextView ;
37+
38+ import java .util .ArrayList ;
39+
40+ import androidx .annotation .Nullable ;
41+ import androidx .appcompat .app .AppCompatActivity ;
42+ import androidx .recyclerview .widget .LinearLayoutManager ;
43+ import androidx .recyclerview .widget .RecyclerView ;
44+
2745public class MultiSelectionDialog extends AppCompatActivity {
2846
2947 Dialog dialog ;
@@ -32,10 +50,9 @@ public class MultiSelectionDialog extends AppCompatActivity {
3250 private ArrayList <MultiSelection > multiList = new ArrayList <>();
3351 private ArrayList <MultiSelection > temp_data_list = new ArrayList <>();
3452 private String headerTitle = "Select" ;
35- private Boolean isSearchEnabled = false ;
3653 MulitpleSelectionAdapter dialogAdapter ;
3754 private String currentField = "" , currentValue = "" , currentPosition = "" , tag = "" , hintText = "Search here" ;
38- private int headerColor ,textColor ;
55+ private int headerColor , textColor ;
3956 MultiSelectionListener multiSelectionListener ;
4057
4158
@@ -45,17 +62,10 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4562 setContentView (R .layout .single_selection_dialog );
4663 }
4764
48- public MultiSelectionDialog (MultiSelectionListener mMultiSelectionListener , Context mContext , String mTag ) {
49- this .multiSelectionListener = mMultiSelectionListener ;
50- this .context = mContext ;
51- this .tag = mTag ;
52- }
53-
54-
55- public void setContent (ArrayList <String > contentProvide ) {
56- list = contentProvide ;
65+ private MultiSelectionDialog (Builder builder ) {
66+ context = builder .context ;
67+ list = builder .list ;
5768 temp_data_list .clear ();
58-
5969 if (list != null && list .size () > 0 ) {
6070 for (int i = 0 ; i < list .size (); i ++) {
6171 MultiSelection multiSelection = new MultiSelection ();
@@ -64,35 +74,22 @@ public void setContent(ArrayList<String> contentProvide) {
6474 temp_data_list .add (multiSelection );
6575 }
6676 }
67-
68- }
69-
70- public void setTitle (String mTitle ) {
71- if (mTitle != null && !mTitle .equals ("" )) {
72- this .headerTitle = mTitle ;
73- } else {
74- this .headerTitle = "Select" ;
75- }
77+ headerTitle = builder .headerTitle ;
78+ tag = builder .tag ;
79+ hintText = builder .hintText ;
80+ currentField = builder .currentField ;
81+ headerColor = builder .headerColor ;
82+ textColor = builder .textColor ;
83+ multiSelectionListener = builder .multiSelectionListener ;
84+ Log .d ("TAG--" , headerTitle );
7685 }
7786
78- public void setTextColor (int color ){
79- this .textColor = color ;
80- }
81- // public void enableSearch(Boolean value, String hint) {
82- // isSearchEnabled = value;
83- // hintText = hint;
84- // }
85-
86- public void setColor (int color ) {
87- headerColor = color ;
88- }
8987
9088 public void setSelectedFields (String selectedField ) {
9189 currentField = selectedField ;
9290 }
9391
9492 public void show () {
95- //Custom pop up dialog for selecting options
9693 try {
9794 dialog = new Dialog (context );
9895 final View convertView = LayoutInflater .from (context ).inflate (R .layout .multi_selection_dialog , null );
@@ -103,28 +100,15 @@ public void show() {
103100 RecyclerView .LayoutManager layoutManager = new LinearLayoutManager (context );
104101 recyclerView .setLayoutManager (layoutManager );
105102 LinearLayout header = convertView .findViewById (R .id .linear_multi_dialog );
106- // final EditText etSearch = convertView.findViewById(R.id.et_search_single_selection);
107103 tvTitle .setText (headerTitle );
108- //
109- // if (isSearchEnabled) {
110- // etSearch.setVisibility(View.VISIBLE);
111- // } else {
112- // etSearch.setVisibility(View.GONE);
113- // }
104+
114105 if (headerColor != 0 ) {
115106 try {
116107 header .setBackgroundColor (headerColor );
117- ColorStateList colorStateList = ColorStateList .valueOf (headerColor );
118- // ViewCompat.setBackgroundTintList(etSearch, colorStateList);
119108 } catch (Exception e ) {
120109 e .printStackTrace ();
121110 }
122111 }
123- //
124- // if (hintText != null && !hintText.equals("")) {
125- // etSearch.setHint(hintText);
126- // }
127-
128112
129113 recyclerView .addOnItemTouchListener (
130114 new RecyclerItemClickListener (context , recyclerView ,
@@ -156,53 +140,28 @@ public void onClick(View view) {
156140 assetValue = getAssetsValue .substring (0 , getAssetsValue .length () - 1 );
157141 }
158142 }
159- multiSelectionListener .onMultiDialogItemsSelected (assetValue , tag , temp_string_list );
143+ if (multiSelectionListener != null ) {
144+ multiSelectionListener .onMultiDialogItemsSelected (assetValue , tag , temp_string_list );
145+ }
160146 } else {
161- multiSelectionListener .onMultiDialogError ("List is null or empty" , tag );
147+ if (multiSelectionListener != null ) {
148+ multiSelectionListener .onMultiDialogError ("List is null or empty" , tag );
149+ }
162150 }
163151
164152 dialog .dismiss ();
165153
166154 }
167155 });
168156
169- // etSearch.addTextChangedListener(new TextWatcher() {
170- // @Override
171- // public void beforeTextChanged(CharSequence s, int start, int count, int after) {
172- // }
173- //
174- // @Override
175- // public void onTextChanged(CharSequence s, int start, int before, int count) {
176- //
177- // if (etSearch.getText().toString().equals("")) {
178- // if (list != null && list.size() > 0) {
179- // dialogAdapter = new SingleSelectionAdapter(list, context, currentField, headerColor);
180- // dialogAdapter.notifyDataSetChanged();
181- // recyclerView.setAdapter(dialogAdapter);
182- // } else {
183- // singleSelectionListener.onDialogError("List is empty or null", tag);
184- // }
185- //
186- //
187- // } else {
188- // getSearch(etSearch.getText().toString(), recyclerView);
189- // }
190- //
191- // }
192- //
193- // @Override
194- // public void afterTextChanged(Editable s) {
195- //
196- // }
197- //// });
198- //
199157 if (list != null && list .size () > 0 ) {
200- dialogAdapter = new MulitpleSelectionAdapter (multiSelectionListener ,multiList , currentField ,tag , context , headerColor ,textColor );
158+ dialogAdapter = new MulitpleSelectionAdapter (multiSelectionListener , multiList , currentField , tag , context , headerColor , textColor );
201159 recyclerView .setAdapter (dialogAdapter );
202160 dialog .show ();
203- } else {
161+ } else { if (multiSelectionListener !=null ){
162+
204163 multiSelectionListener .onMultiDialogError ("List is null or empty" , tag );
205- }
164+ }}
206165
207166 dialog .show ();
208167 } catch (Exception e ) {
@@ -211,29 +170,64 @@ public void onClick(View view) {
211170
212171 }
213172
214- // private void getSearch(String search, RecyclerView recyclerView) {
215- // ArrayList<String> temp_list = new ArrayList<>();
216- // if (list != null && list.size() > 0) {
217- // for (int i = 0; i < list.size(); i++) {
218- // if (list.get(i).toLowerCase().contains(search.toLowerCase())) {
219- // temp_list.add(list.get(i));
220- //
221- // }
222- // }
223- // } else {
224- // singleSelectionListener.onDialogError("List is empty or null", tag);
225- //
226- // }
227- //
228- // temp_data_list = new ArrayList<>();
229- // temp_data_list.addAll(temp_list);
230- // dialogAdapter = new SingleSelectionAdapter(temp_data_list, context, currentField, headerColor);
231- //
232- // dialogAdapter.notifyDataSetChanged();
233- // recyclerView.setAdapter(dialogAdapter);
234- //
235- //
236- // }
237173
174+ public static class Builder {
175+ //required
176+
177+ //optional
178+
179+ Dialog dialog ;
180+ private Context context ;
181+ private ArrayList <String > list = new ArrayList <>();
182+ private String headerTitle = "Select" ;
183+ private String currentField = "" , tag = "" , hintText = "Search here" ;
184+ private int headerColor , textColor ;
185+ MultiSelectionListener multiSelectionListener ;
186+
187+ public Builder (Context ctx , String tag ) {
188+ this .context = ctx ;
189+ this .tag = tag ;
190+ }
191+
192+ public Builder setContent (ArrayList <String > contentProvide ) {
193+ this .list = contentProvide ;
194+ return this ;
195+ }
196+
197+ public Builder setTitle (String value ) {
198+ if (value != null && !value .equals ("" )) {
199+ this .headerTitle = value ;
200+ } else {
201+ this .headerTitle = "Select" ;
202+ }
203+ return this ;
204+ }
205+
206+ public Builder setTextColor (int color ) {
207+ this .textColor = color ;
208+ return this ;
209+ }
210+
211+
212+ public Builder setColor (int color ) {
213+ headerColor = color ;
214+ return this ;
215+ }
216+
217+ public Builder setListener (MultiSelectionListener listener ) {
218+ multiSelectionListener = listener ;
219+ return this ;
220+ }
221+
222+
223+ public Builder setSelectedField (String selectedField ) {
224+ currentField = selectedField ;
225+ return this ;
226+ }
227+
228+ public MultiSelectionDialog build () {
229+ return new MultiSelectionDialog (this );
230+ }
231+ }
238232
239233}
0 commit comments