PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/IzvorniKod/ZOOVrt/app/src/main/java/com/example/natkobiscan/zoovrt/activities/ActivityUtility.java

https://gitlab.com/dvesinger/TheFault
Java | 114 lines | 57 code | 10 blank | 47 comment | 5 complexity | e49fab89a21171947a1293f8fbc6fa07 MD5 | raw file
  1. package com.example.natkobiscan.zoovrt.activities;
  2. import android.content.Context;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.Adapter;
  6. import android.widget.FrameLayout;
  7. import android.widget.ListAdapter;
  8. import android.widget.ListView;
  9. import com.example.natkobiscan.zoovrt.model.database.User;
  10. import java.util.regex.Matcher;
  11. /**
  12. * Utility methods used in activites.
  13. *
  14. * @author Domagoj Pluscec
  15. * @version v1.0, 2.1.2017.
  16. */
  17. public class ActivityUtility {
  18. /**
  19. * Utility class constructor.
  20. */
  21. private ActivityUtility() {
  22. }
  23. /**
  24. * Computes the widest view in an adapter, best used when you need to wrap_content on a ListView, please be careful
  25. * and don't use it on an adapter that is extremely numerous in items or it will take a long time.
  26. *
  27. * @param context Some context
  28. * @param adapter The adapter to process
  29. * @return The pixel width of the widest View
  30. */
  31. public static int getWidestView(Context context, Adapter adapter) {
  32. int maxWidth = 0;
  33. View view = null;
  34. FrameLayout fakeParent = new FrameLayout(context);
  35. for (int i = 0, count = adapter.getCount(); i < count; i++) {
  36. view = adapter.getView(i, view, fakeParent);
  37. view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
  38. int width = view.getMeasuredWidth();
  39. if (width > maxWidth) {
  40. maxWidth = width;
  41. }
  42. }
  43. return maxWidth;
  44. }
  45. /**
  46. * Method validates email by user's valid email address regex.
  47. *
  48. * @param emailStr email string
  49. * @return true if the email regex was found, false otherwise
  50. */
  51. public static boolean validate(String emailStr) {
  52. Matcher matcher = User.VALID_EMAIL_ADDRESS_REGEX.matcher(emailStr);
  53. return matcher.find();
  54. }
  55. /**
  56. * Edit type status enumeration.
  57. */
  58. public enum EditType {
  59. /**
  60. * No edit status.
  61. */
  62. NO_EDIT,
  63. /**
  64. * Addition status.
  65. */
  66. ADD,
  67. /**
  68. * Edit status.
  69. */
  70. EDIT,
  71. /**
  72. * Deletion status.
  73. */
  74. DELETE;
  75. }
  76. /**
  77. * List view utilites.
  78. */
  79. public static class ListUtils {
  80. /**
  81. * Method sets dynamic height of a given list view.
  82. *
  83. * @param mListView list view to change height
  84. */
  85. public static void setDynamicHeight(ListView mListView) {
  86. ListAdapter mListAdapter = mListView.getAdapter();
  87. if (mListAdapter == null) {
  88. // when adapter is null
  89. return;
  90. }
  91. int height = 0;
  92. int desiredWidth = View.MeasureSpec.makeMeasureSpec(mListView.getWidth(), View.MeasureSpec.UNSPECIFIED);
  93. for (int i = 0; i < mListAdapter.getCount(); i++) {
  94. View listItem = mListAdapter.getView(i, null, mListView);
  95. listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
  96. height += listItem.getMeasuredHeight();
  97. }
  98. ViewGroup.LayoutParams params = mListView.getLayoutParams();
  99. params.height = height + (mListView.getDividerHeight() * (mListAdapter.getCount() - 1));
  100. mListView.setLayoutParams(params);
  101. mListView.requestLayout();
  102. }
  103. }
  104. }