22
33import android .app .Dialog ;
44import android .content .Context ;
5+ import android .content .res .ColorStateList ;
56import android .os .Bundle ;
7+ import android .text .Editable ;
8+ import android .text .TextWatcher ;
9+ import android .view .LayoutInflater ;
10+ import android .view .View ;
11+ import android .widget .EditText ;
12+ import android .widget .ImageView ;
13+ import android .widget .LinearLayout ;
14+ import android .widget .TextView ;
15+ import android .widget .Toast ;
616
717import java .util .ArrayList ;
818
919import androidx .annotation .Nullable ;
1020import androidx .appcompat .app .AppCompatActivity ;
21+ import androidx .core .view .ViewCompat ;
22+ import androidx .recyclerview .widget .LinearLayoutManager ;
23+ import androidx .recyclerview .widget .RecyclerView ;
1124
1225public class MultiSelectionDialog extends AppCompatActivity {
1326
1427 Dialog dialog ;
1528 private Context context ;
16- private ArrayList <MultiSelection > list = new ArrayList <>();
17- private ArrayList <String > temp_data_list = new ArrayList <>();
29+ private ArrayList <String > list = new ArrayList <>();
30+ private ArrayList <MultiSelection > multiList = new ArrayList <>();
31+ private ArrayList <MultiSelection > temp_data_list = new ArrayList <>();
1832 private String headerTitle = "Select" ;
1933 private Boolean isSearchEnabled = false ;
2034 SingleSelectionAdapter dialogAdapter ;
21- private String currentField = "" ;
35+ private String currentField = "" , currentValue = "" , currentPosition = "" , tag = "" , hintText = "Search here" ;
2236 private int headerColor ;
23- SingleSelectionListener singleSelectionListener ;
37+ MultiSelectionListener multiSelectionListener ;
2438
2539
2640 @ Override
@@ -29,18 +43,24 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
2943 setContentView (R .layout .single_selection_dialog );
3044 }
3145
32- public MultiSelectionDialog (SingleSelectionListener singleSelectionListener ) {
33- this .singleSelectionListener = singleSelectionListener ;
34- }
35-
36- public void with (Context mContext ) {
46+ public MultiSelectionDialog (MultiSelectionListener mMultiSelectionListener , Context mContext , String mTag ) {
47+ this .multiSelectionListener = mMultiSelectionListener ;
3748 this .context = mContext ;
49+ this .tag = mTag ;
3850 }
3951
40- public void setList (ArrayList <String > dataList ) {
4152
53+ public void setContent (ArrayList <String > contentProvide ) {
54+ list = contentProvide ;
4255 temp_data_list .clear ();
43- this .temp_data_list = dataList ;
56+
57+ if (list != null && list .size () > 0 ) {
58+ for (int i = 0 ; i < list .size (); i ++) {
59+ multiList .get (i ).setTitle (list .get (i ));
60+ temp_data_list .get (i ).setTitle (list .get (i ));
61+ }
62+ }
63+
4464 }
4565
4666 public void setTitle (String mTitle ) {
@@ -51,76 +71,89 @@ public void setTitle(String mTitle) {
5171 }
5272 }
5373
54- public void enableSearch (Boolean value ) {
74+ public void enableSearch (Boolean value , String hint ) {
5575 isSearchEnabled = value ;
76+ hintText = hint ;
5677 }
5778
5879 public void setColor (int color ) {
5980 headerColor = color ;
6081 }
6182
83+ public void setSelectedField (String selectedField ) {
84+ currentField = selectedField ;
85+ }
6286
63- // public void show() {
64- // //Custom pop up dialog for selecting options
65- // try {
66- // dialog = new Dialog(context);
67- // final View convertView = LayoutInflater.from(context).inflate(R.layout.multi_selection_dialog, null);
68- // dialog.setContentView(convertView);
69- // TextView tvTitle = convertView.findViewById(R.id.tv_title );
70- // ImageView imgDone = convertView.findViewById(R.id.img_done_multi_dialog);
71- // final RecyclerView recyclerView = convertView.findViewById(R.id.recycler_multi_dialog);
72- // RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
73- // recyclerView.setLayoutManager(layoutManager);
74- // LinearLayout header = convertView.findViewById(R.id.linear_multi_dialog);
87+ public void show () {
88+ //Custom pop up dialog for selecting options
89+ try {
90+ dialog = new Dialog (context );
91+ final View convertView = LayoutInflater .from (context ).inflate (R .layout .multi_selection_dialog , null );
92+ dialog .setContentView (convertView );
93+ TextView tvTitle = convertView .findViewById (R .id .tv_title_multi_dialog );
94+ ImageView imgDone = convertView .findViewById (R .id .img_done_multi_dialog );
95+ final RecyclerView recyclerView = convertView .findViewById (R .id .recycler_multi_dialog );
96+ RecyclerView .LayoutManager layoutManager = new LinearLayoutManager (context );
97+ recyclerView .setLayoutManager (layoutManager );
98+ LinearLayout header = convertView .findViewById (R .id .linear_multi_dialog );
7599// final EditText etSearch = convertView.findViewById(R.id.et_search_single_selection);
76- // tvTitle.setText(headerTitle);
100+ tvTitle .setText (headerTitle );
77101//
78- // if (headerColor != 0) {
79- // try {
80- // header.setBackgroundColor(headerColor);
81- // } catch (Exception e) {
82- // e.printStackTrace();
83- // }
102+ // if (isSearchEnabled) {
103+ // etSearch.setVisibility(View.VISIBLE);
104+ // } else {
105+ // etSearch.setVisibility(View.GONE);
84106// }
85- // imgDone.setOnClickListener(new View.OnClickListener() {
86- // @Override
87- // public void onClick(View view) {
88- // String getAssetsValue = "";
89- // String assetValue = "";
90- // if (list != null && list.size() > 0) {
91- // for (int i = 0; i < list.size(); i++) {
92- // if (list.get(i).getCheck()) {
93- // getAssetsValue += list.get(i).getTitle() + ",";
94- // assetValue = getAssetsValue.substring(0, getAssetsValue.length() - 1);
95- // }
96- // }
97- // }
98- // if (getAssetsValue.equals("")) {
99- //
100- //
101- // } else {
102- //
103- // }
104- // dialog.dismiss();
105- //
106- // }
107- // });
108- // recyclerView.addOnItemTouchListener(
109- // new RecyclerItemClickListener(context, recyclerView,
110- // new RecyclerItemClickListener.OnItemClickListener() {
111- // @Override
112- // public void onItemClick(View view, int position) {
113- // dialog.dismiss();
114- // singleSelectionListener.onDialogItemSelected(currentField, position,tag);
115- // }
116- //
117- // @Override
118- // public void onItemLongClick(View view, int position) {
119- // dialog.dismiss();
120- // }
121- //
122- // })
123- // );
107+ if (headerColor != 0 ) {
108+ try {
109+ header .setBackgroundColor (headerColor );
110+ ColorStateList colorStateList = ColorStateList .valueOf (headerColor );
111+ // ViewCompat.setBackgroundTintList(etSearch, colorStateList);
112+ } catch (Exception e ) {
113+ e .printStackTrace ();
114+ }
115+ }
116+ //
117+ // if (hintText != null && !hintText.equals("")) {
118+ // etSearch.setHint(hintText);
119+ // }
120+
121+
122+ recyclerView .addOnItemTouchListener (
123+ new RecyclerItemClickListener (context , recyclerView ,
124+ new RecyclerItemClickListener .OnItemClickListener () {
125+ @ Override
126+ public void onItemClick (View view , int position ) {
127+
128+
129+ }
130+
131+ @ Override
132+ public void onItemLongClick (View view , int position ) {
133+ dialog .dismiss ();
134+ }
135+
136+ })
137+ );
138+ imgDone .setOnClickListener (new View .OnClickListener () {
139+ @ Override
140+ public void onClick (View view ) {
141+ String getAssetsValue = "" ;
142+ String assetValue = "" ;
143+ if (multiList != null && multiList .size () > 0 ) {
144+ for (int i = 0 ; i < multiList .size (); i ++) {
145+ if (multiList .get (i ).getCheck ()) {
146+ getAssetsValue += multiList .get (i ).getTitle () + "," ;
147+ assetValue = getAssetsValue .substring (0 , getAssetsValue .length () - 1 );
148+ }
149+ }
150+ multiSelectionListener .onMultiDialogItemsSelected (assetValue , tag );
151+ }
152+
153+ dialog .dismiss ();
154+
155+ }
156+ });
124157//
125158// etSearch.addTextChangedListener(new TextWatcher() {
126159// @Override
@@ -132,43 +165,41 @@ public void setColor(int color) {
132165//
133166// if (etSearch.getText().toString().equals("")) {
134167// if (list != null && list.size() > 0) {
135- //// dialogAdapter = new SingleSelectionAdapter(list, context, currentField);
168+ // dialogAdapter = new SingleSelectionAdapter(list, context, currentField, headerColor );
136169// dialogAdapter.notifyDataSetChanged();
137170// recyclerView.setAdapter(dialogAdapter);
138171// } else {
139- // singleSelectionListener.onDialogError("List is empty or null");
172+ // singleSelectionListener.onDialogError("List is empty or null", tag );
140173// }
141174//
142175//
143176// } else {
144- //// getSearchCountryList (etSearch.getText().toString(), recyclerView);
177+ // getSearch (etSearch.getText().toString(), recyclerView);
145178// }
146179//
147- //
148180// }
149181//
150- //
151182// @Override
152183// public void afterTextChanged(Editable s) {
153184//
154185// }
155- // });
186+ //// });
156187//
157188// if (list != null && list.size() > 0) {
158- //// dialogAdapter = new SingleSelectionAdapter(list, context, currentField);
189+ // dialogAdapter = new SingleSelectionAdapter(list, context, currentField, headerColor );
159190// recyclerView.setAdapter(dialogAdapter);
160191// dialog.show();
161192// } else {
162193// Toast.makeText(context, "List is empty", Toast.LENGTH_SHORT).show();
163194// }
164- //
165- // } catch (Exception e) {
166- // e.printStackTrace();
167- // }
168- //
169- // }
170195
171- // private void getSearchCountryList(String search, RecyclerView recyclerView) {
196+ } catch (Exception e ) {
197+ e .printStackTrace ();
198+ }
199+
200+ }
201+
202+ // private void getSearch(String search, RecyclerView recyclerView) {
172203// ArrayList<String> temp_list = new ArrayList<>();
173204// if (list != null && list.size() > 0) {
174205// for (int i = 0; i < list.size(); i++) {
@@ -178,14 +209,14 @@ public void setColor(int color) {
178209// }
179210// }
180211// } else {
181- // singleSelectionListener.onError ("List is empty or null");
212+ // singleSelectionListener.onDialogError ("List is empty or null", tag );
182213//
183214// }
184215//
185- // tempCountryList.clear();
186- // tempCountryList.addAll(temp_list);
216+ // temp_data_list = new ArrayList<>();
217+ // temp_data_list.addAll(temp_list);
218+ // dialogAdapter = new SingleSelectionAdapter(temp_data_list, context, currentField, headerColor);
187219//
188- // dialogAdapter = new RadioDialogAdapter(tempCountryList, context, currentField);
189220// dialogAdapter.notifyDataSetChanged();
190221// recyclerView.setAdapter(dialogAdapter);
191222//
0 commit comments