Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. package horizonideas.allergeen_app;
  2.  
  3.  
  4. import android.content.Context;
  5. import android.content.SharedPreferences;
  6. import android.graphics.Color;
  7. import android.graphics.Typeface;
  8. import android.graphics.drawable.Drawable;
  9. import android.os.Bundle;
  10. import android.os.Handler;
  11. import android.support.v4.app.Fragment;
  12. import android.support.v7.widget.SwitchCompat;
  13. import android.util.DisplayMetrics;
  14. import android.util.TypedValue;
  15. import android.view.Gravity;
  16. import android.view.LayoutInflater;
  17. import android.view.View;
  18. import android.view.ViewGroup;
  19. import android.widget.CompoundButton;
  20. import android.widget.ImageView;
  21. import android.widget.LinearLayout;
  22. import android.widget.RelativeLayout;
  23. import android.widget.TextView;
  24.  
  25. import java.util.HashSet;
  26. import java.util.Map;
  27. import java.util.Set;
  28.  
  29. import horizonideas.allergeen_app.models.Allergy;
  30.  
  31. import static android.content.Context.MODE_PRIVATE;
  32.  
  33. /**
  34. * A simple {@link Fragment} subclass.
  35. */
  36. public class AllergyProfileFragment extends Fragment {
  37.  
  38. SharedPreferences sharedPreferences;
  39. public static final String selectedAllergies = "selectedAllergies";
  40. public Set<String> set;
  41. public Map<Integer, Allergy> map = AllergyRegister.getInstance().getAllAllergies();
  42. public SharedPreferences.Editor editor;
  43. public LinearLayout list;
  44. public LinearLayout wheatlist;
  45. public DisplayMetrics dm;
  46. public ImageView dropIcon;
  47. public LinearLayout wheatHeader;
  48. public boolean visibility;
  49. public Set<Integer> wheatIds = new HashSet<Integer>();
  50.  
  51. public AllergyProfileFragment() {
  52.  
  53. }
  54.  
  55. @Override
  56. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  57. Bundle savedInstanceState) {
  58.  
  59. // Inflate the layout for this fragment
  60. View v = inflater.inflate(R.layout.fragment_allergy_profile, container, false);
  61.  
  62. sharedPreferences = getActivity().getSharedPreferences(selectedAllergies, Context.MODE_PRIVATE);
  63. editor = getActivity().getSharedPreferences(selectedAllergies, MODE_PRIVATE).edit();
  64. set = sharedPreferences.getStringSet("List", new HashSet<String>());
  65.  
  66. dm = getResources().getDisplayMetrics();
  67.  
  68. list = (LinearLayout) v.findViewById(R.id.list);
  69.  
  70. for(final Allergy allergy : map.values()) {
  71.  
  72. drawAllergy(allergy);
  73. }
  74.  
  75.  
  76. return v;
  77. }
  78.  
  79. @Override
  80. public void onPause(){
  81. super.onPause();
  82. final Handler handler = new Handler();
  83. handler.postDelayed(new Runnable() {
  84. public void run() {
  85. editor.putStringSet("List", set);
  86. editor.commit();
  87. }
  88. }, 500);
  89.  
  90. }
  91.  
  92. public void drawAllergy(final Allergy allergy){
  93. final LinearLayout row = new LinearLayout(getActivity());
  94. LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
  95. params5.setMargins(0,0, getResources().getDimensionPixelOffset(R.dimen.margins), 0);
  96. row.setOrientation(LinearLayout.HORIZONTAL);
  97.  
  98. LinearLayout allergyIconLayout = new LinearLayout(getActivity());
  99. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  100. params.setMargins(getResources().getDimensionPixelSize(R.dimen.margins), getResources().getDimensionPixelSize(R.dimen.margins), getResources().getDimensionPixelSize(R.dimen.margins), getResources().getDimensionPixelSize(R.dimen.margins));
  101. allergyIconLayout.setLayoutParams(params);
  102.  
  103. // Create LayoutParams for it // Note 200 200 is width, height in pixels
  104. RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(toPixels(dm, 26),toPixels(dm, 26));
  105. // Align bottom-right, and add bottom-margin
  106. params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
  107. params2.addRule(RelativeLayout.CENTER_IN_PARENT);
  108.  
  109.  
  110. ImageView image = new ImageView (getActivity());
  111. image.setLayoutParams(new ViewGroup.LayoutParams((toPixels(dm, 46)), (toPixels(dm, 46))));
  112. String name = allergy.RoundIcon;
  113. int id = getResources().getIdentifier(name, "drawable", getActivity().getPackageName());
  114. Drawable drawable = getResources().getDrawable(id);
  115. image.setImageDrawable(drawable);
  116.  
  117. image.setMaxHeight((toPixels(dm, 46)));
  118. image.setMaxWidth((toPixels(dm, 46)));
  119. image.setAdjustViewBounds(true);
  120. image.setLayoutParams(params);
  121.  
  122.  
  123. ImageView divider = new ImageView(getActivity());
  124. divider.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
  125. divider.setBackgroundColor(Color.GRAY);
  126.  
  127. TextView textView = new TextView(getActivity());
  128. textView.setTextSize(16);
  129. textView.setTypeface(null, Typeface.BOLD);
  130. textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
  131. textView.setGravity(Gravity.CENTER_VERTICAL);
  132. textView.setText(allergy.Name);
  133.  
  134. final SwitchCompat switc = new SwitchCompat(getActivity());
  135. switc.setLayoutParams(params5);
  136. switc.setTag(allergy.ProductAllergyID);
  137.  
  138. switc.setChecked(sharedPreferences.getBoolean("" + allergy.ProductAllergyID, false));
  139.  
  140. switc.setGravity(Gravity.CENTER_VERTICAL + Gravity.END);
  141. switc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  142. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  143. if (isChecked) {
  144.  
  145. editor.putBoolean(buttonView.getTag().toString(), true);
  146. set.add(String.valueOf(allergy.ProductAllergyID));
  147. editor.commit();
  148. } else {
  149. editor.putBoolean(buttonView.getTag().toString(), false);
  150. set.remove(String.valueOf(allergy.ProductAllergyID));
  151. editor.commit();
  152. }
  153. }
  154. });
  155.  
  156. row.addView(image);
  157. row.addView(textView);
  158. row.addView(switc);
  159.  
  160. list.addView(row);
  161. list.addView(divider);
  162.  
  163.  
  164.  
  165. }
  166.  
  167.  
  168. public static int toPixels(DisplayMetrics metrics, int dp) {
  169. return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics));
  170. }
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement