Advertisement
ali50mahmoud

spinner

Mar 15th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.00 KB | None | 0 0
  1. <RelativeLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:paddingBottom="@dimen/activity_vertical_margin"
  8. android:paddingLeft="@dimen/activity_horizontal_margin"
  9. android:paddingRight="@dimen/activity_horizontal_margin"
  10. android:paddingTop="@dimen/activity_vertical_margin"
  11. tools:context="com.example.manishvishwakarma.searchablespinnermine.MainActivity">
  12.  
  13. <com.example.manishvishwakarma.searchablespinnermine.SearchableSpinner
  14. android:id="@+id/spinner"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:entries="@array/colleges"
  18. app:hintText="Name your college with State"/>
  19. </RelativeLayout>
  20. ///////////////////////////////////////////////
  21. public class SearchableSpinner extends Spinner implements View.OnTouchListener,
  22. SearchableListDialog.SearchableItem {
  23.  
  24. String selectedItem;
  25. //this string above will store the value of selected item.
  26.  
  27. public static final int NO_ITEM_SELECTED = -1;
  28. private Context _context;
  29. private List _items;
  30. private SearchableListDialog _searchableListDialog;
  31.  
  32. private boolean _isDirty;
  33. private ArrayAdapter _arrayAdapter;
  34. private String _strHintText;
  35. private boolean _isFromInit;
  36.  
  37. public SearchableSpinner(Context context) {
  38. super(context);
  39. this._context = context;
  40. init();
  41. }
  42.  
  43. public SearchableSpinner(Context context, AttributeSet attrs) {
  44. super(context, attrs);
  45. this._context = context;
  46. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SearchableSpinner);
  47. final int N = a.getIndexCount();
  48. for (int i = 0; i < N; ++i) {
  49. int attr = a.getIndex(i);
  50. if (attr == R.styleable.SearchableSpinner_hintText) {
  51. _strHintText = a.getString(attr);
  52. }
  53. }
  54. a.recycle();
  55. init();
  56. }
  57.  
  58. public SearchableSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
  59. super(context, attrs, defStyleAttr);
  60. this._context = context;
  61. init();
  62. }
  63.  
  64. private void init() {
  65. _items = new ArrayList();
  66. _searchableListDialog = SearchableListDialog.newInstance
  67. (_items);
  68. _searchableListDialog.setOnSearchableItemClickListener(this);
  69. setOnTouchListener(this);
  70.  
  71. _arrayAdapter = (ArrayAdapter) getAdapter();
  72. if (!TextUtils.isEmpty(_strHintText)) {
  73. ArrayAdapter arrayAdapter = new ArrayAdapter(_context, android.R.layout
  74. .simple_list_item_1, new String[]{_strHintText});
  75. _isFromInit = true;
  76. setAdapter(arrayAdapter);
  77. }
  78. }
  79.  
  80. @Override
  81. public boolean onTouch(View v, MotionEvent event) {
  82. if (event.getAction() == MotionEvent.ACTION_UP) {
  83.  
  84. if (null != _arrayAdapter) {
  85.  
  86. // Refresh content #6
  87. // Change Start
  88. // Description: The items were only set initially, not reloading the data in the
  89. // spinner every time it is loaded with items in the adapter.
  90. _items.clear();
  91. for (int i = 0; i < _arrayAdapter.getCount(); i++) {
  92. _items.add(_arrayAdapter.getItem(i));
  93. }
  94. // Change end.
  95.  
  96. _searchableListDialog.show(scanForActivity(_context).getFragmentManager(), "TAG");
  97. }
  98. }
  99. return true;
  100. }
  101.  
  102. @Override
  103. public void setAdapter(SpinnerAdapter adapter) {
  104.  
  105. if (!_isFromInit) {
  106. _arrayAdapter = (ArrayAdapter) adapter;
  107. if (!TextUtils.isEmpty(_strHintText) && !_isDirty) {
  108. ArrayAdapter arrayAdapter = new ArrayAdapter(_context, android.R.layout
  109. .simple_list_item_1, new String[]{_strHintText});
  110. super.setAdapter(arrayAdapter);
  111. } else {
  112. super.setAdapter(adapter);
  113. }
  114.  
  115. } else {
  116. _isFromInit = false;
  117. super.setAdapter(adapter);
  118. }
  119. }
  120. //The method just below is executed when an item in the searchlist is tapped.This is where we store the value int string called selectedItem.
  121. @Override
  122. public void onSearchableItemClicked(Object item, int position) {
  123. setSelection(_items.indexOf(item));
  124.  
  125. if (!_isDirty) {
  126. _isDirty = true;
  127. setAdapter(_arrayAdapter);
  128. setSelection(_items.indexOf(item));
  129. }
  130. selectedItem= getItemAtPosition(position).toString();
  131.  
  132. Toast.makeText(getContext(),"You selected "+selectedItem,Toast.LENGTH_LONG).show();
  133. }
  134.  
  135.  
  136.  
  137. private Activity scanForActivity(Context cont) {
  138. if (cont == null)
  139. return null;
  140. else if (cont instanceof Activity)
  141. return (Activity) cont;
  142. else if (cont instanceof ContextWrapper)
  143. return scanForActivity(((ContextWrapper) cont).getBaseContext());
  144.  
  145. return null;
  146. }
  147.  
  148. @Override
  149. public int getSelectedItemPosition() {
  150. if (!TextUtils.isEmpty(_strHintText) && !_isDirty) {
  151. return NO_ITEM_SELECTED;
  152. } else {
  153. return super.getSelectedItemPosition();
  154. }
  155. }
  156.  
  157. @Override
  158. public Object getSelectedItem() {
  159. if (!TextUtils.isEmpty(_strHintText) && !_isDirty) {
  160. return null;
  161. } else {
  162.  
  163. return super.getSelectedItem();
  164. }
  165. }
  166. }
  167. ////////////////////////////////////
  168. public class SearchableListDialog extends DialogFragment implements
  169. SearchView.OnQueryTextListener, SearchView.OnCloseListener {
  170.  
  171. private static final String ITEMS = "items";
  172.  
  173. private ArrayAdapter listAdapter;
  174.  
  175. private ListView _listViewItems;
  176.  
  177. private SearchableItem _searchableItem;
  178.  
  179. private OnSearchTextChanged _onSearchTextChanged;
  180.  
  181. private SearchView _searchView;
  182.  
  183. private String _strTitle;
  184.  
  185. private String _strPositiveButtonText;
  186.  
  187. private DialogInterface.OnClickListener _onClickListener;
  188.  
  189. public SearchableListDialog() {
  190.  
  191. }
  192.  
  193. public static SearchableListDialog newInstance(List items) {
  194. SearchableListDialog multiSelectExpandableFragment = new
  195. SearchableListDialog();
  196.  
  197. Bundle args = new Bundle();
  198. args.putSerializable(ITEMS, (Serializable) items);
  199.  
  200. multiSelectExpandableFragment.setArguments(args);
  201.  
  202. return multiSelectExpandableFragment;
  203. }
  204.  
  205. @Override
  206. public void onCreate(Bundle savedInstanceState) {
  207. super.onCreate(savedInstanceState);
  208.  
  209. }
  210.  
  211. @Override
  212. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  213. Bundle savedInstanceState) {
  214. getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams
  215. .SOFT_INPUT_STATE_HIDDEN);
  216. return super.onCreateView(inflater, container, savedInstanceState);
  217. }
  218.  
  219. @Override
  220. public Dialog onCreateDialog(Bundle savedInstanceState) {
  221.  
  222. // Getting the layout inflater to inflate the view in an alert dialog.
  223. LayoutInflater inflater = LayoutInflater.from(getActivity());
  224.  
  225. // Crash on orientation change #7
  226. // Change Start
  227. // Description: As the instance was re initializing to null on rotating the device,
  228. // getting the instance from the saved instance
  229. if (null != savedInstanceState) {
  230. _searchableItem = (SearchableItem) savedInstanceState.getSerializable("item");
  231. }
  232. // Change End
  233.  
  234. View rootView = inflater.inflate(R.layout.searchable_list_dialog, null);
  235. setData(rootView);
  236.  
  237. AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
  238. alertDialog.setView(rootView);
  239.  
  240. String strPositiveButton = _strPositiveButtonText == null ? "CLOSE" : _strPositiveButtonText;
  241. alertDialog.setPositiveButton(strPositiveButton, _onClickListener);
  242.  
  243. String strTitle = _strTitle == null ? "Select Item" : _strTitle;
  244. alertDialog.setTitle(strTitle);
  245.  
  246. final AlertDialog dialog = alertDialog.create();
  247. dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams
  248. .SOFT_INPUT_STATE_HIDDEN);
  249. return dialog;
  250. }
  251.  
  252. // Crash on orientation change #7
  253. // Change Start
  254. // Description: Saving the instance of searchable item instance.
  255. @Override
  256. public void onSaveInstanceState(Bundle outState) {
  257. outState.putSerializable("item", _searchableItem);
  258. super.onSaveInstanceState(outState);
  259. }
  260. // Change End
  261.  
  262. public void setTitle(String strTitle) {
  263. _strTitle = strTitle;
  264. }
  265.  
  266. public void setPositiveButton(String strPositiveButtonText) {
  267. _strPositiveButtonText = strPositiveButtonText;
  268. }
  269.  
  270. public void setPositiveButton(String strPositiveButtonText, DialogInterface.OnClickListener onClickListener) {
  271. _strPositiveButtonText = strPositiveButtonText;
  272. _onClickListener = onClickListener;
  273. }
  274.  
  275. public void setOnSearchableItemClickListener(SearchableItem searchableItem) {
  276. this._searchableItem = searchableItem;
  277. }
  278.  
  279. public void setOnSearchTextChangedListener(OnSearchTextChanged onSearchTextChanged) {
  280. this._onSearchTextChanged = onSearchTextChanged;
  281. }
  282.  
  283. private void setData(View rootView) {
  284. SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context
  285. .SEARCH_SERVICE);
  286.  
  287. _searchView = (SearchView) rootView.findViewById(R.id.search);
  288. _searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName
  289. ()));
  290. _searchView.setIconifiedByDefault(false);
  291. _searchView.setOnQueryTextListener(this);
  292. _searchView.setOnCloseListener(this);
  293. _searchView.clearFocus();
  294. InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context
  295. .INPUT_METHOD_SERVICE);
  296. mgr.hideSoftInputFromWindow(_searchView.getWindowToken(), 0);
  297.  
  298.  
  299. List items = (List) getArguments().getSerializable(ITEMS);
  300.  
  301. _listViewItems = (ListView) rootView.findViewById(R.id.listItems);
  302.  
  303. //create the adapter by passing your ArrayList data
  304. listAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1,items); //attach the adapter to the list
  305. _listViewItems.setAdapter(listAdapter);
  306.  
  307. _listViewItems.setTextFilterEnabled(true);
  308.  
  309. _listViewItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  310. @Override
  311. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  312. _searchableItem.onSearchableItemClicked(listAdapter.getItem(position), position);
  313. getDialog().dismiss();
  314. }
  315. });
  316. }
  317.  
  318. @Override
  319. public boolean onClose() {
  320. return false;
  321. }
  322.  
  323. @Override
  324. public boolean onQueryTextSubmit(String s) {
  325. _searchView.clearFocus();
  326. return true;
  327. }
  328.  
  329. @Override
  330. public boolean onQueryTextChange(String s) {
  331. // listAdapter.filterData(s);
  332. if (TextUtils.isEmpty(s)) {
  333. // _listViewItems.clearTextFilter();
  334. ((ArrayAdapter) _listViewItems.getAdapter()).getFilter().filter(null);
  335. } else {
  336. ((ArrayAdapter) _listViewItems.getAdapter()).getFilter().filter(s);
  337. }
  338. if (null != _onSearchTextChanged) {
  339. _onSearchTextChanged.onSearchTextChanged(s);
  340. }
  341. return true;
  342. }
  343.  
  344. public interface SearchableItem<T> extends Serializable {
  345. void onSearchableItemClicked(T item, int position);
  346. }
  347.  
  348. public interface OnSearchTextChanged {
  349. void onSearchTextChanged(String strText);
  350. }
  351. }
  352. //////////////////searchable_list_dialog.xml////////////
  353. <LinearLayout
  354. xmlns:android="http://schemas.android.com/apk/res/android"
  355. android:layout_width="match_parent"
  356. android:layout_height="match_parent"
  357. android:orientation="vertical"
  358. android:paddingTop="8dp"
  359. android:paddingRight="8dp"
  360. android:paddingLeft="8dp">
  361.  
  362. <SearchView
  363. android:id="@+id/search"
  364. android:layout_width="match_parent"
  365. android:layout_height="wrap_content"
  366. android:iconifiedByDefault="false"/>
  367.  
  368. <ListView
  369. android:id="@+id/listItems"
  370. android:layout_width="match_parent"
  371. android:layout_height="wrap_content"/>
  372.  
  373. </LinearLayout>
  374. /////////////////////////////////////////colleges.xml////////////values
  375. resources>
  376. <string name="app_name">Spinner with Search</string>
  377.  
  378. <string-array name="colleges">
  379.  
  380. //Arts and Science Colleges
  381.  
  382. <item>Mahatma Gandhi Government College,Andaman and Nicobar Islands</item>
  383. <item>Tagore Government College of Education,Andaman and Nicobar Islands</item>
  384. <item>Dr B R Ambedkar Institute of Technology Port Blair,Andaman and Nicobar Islands</item>
  385. <item>Government Arts College, Rajahmundry,Andhra Pradesh</item>
  386. <item>Government Medical College, Anantapur,Andhra Pradesh</item>
  387. <item>Maharajah\'s Government College of Music and Dance,Andhra Pradesh</item>
  388. <item>P. V. K. N. Government College,Andhra Pradesh</item>
  389. <item>Silver Jubilee Government Degree College,Andhra Pradesh</item>
  390.  
  391.  
  392. </string-array>
  393. </resources>
  394. //////////////////////////////attrs.xml/////////////
  395. <?xml version="1.0" encoding="utf-8"?>
  396. <resources>
  397. <declare-styleable name="SearchableSpinner">
  398. <attr name="hintText" format="string"/>
  399. </declare-styleable>
  400. </resources>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement