Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package horizonideas.allergeen_app;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.graphics.Color;
- import android.graphics.Typeface;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.os.Handler;
- import android.support.v4.app.Fragment;
- import android.support.v7.widget.SwitchCompat;
- import android.util.DisplayMetrics;
- import android.util.TypedValue;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.CompoundButton;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Set;
- import horizonideas.allergeen_app.models.Allergy;
- import static android.content.Context.MODE_PRIVATE;
- /**
- * A simple {@link Fragment} subclass.
- */
- public class AllergyProfileFragment extends Fragment {
- SharedPreferences sharedPreferences;
- public static final String selectedAllergies = "selectedAllergies";
- public Set<String> set;
- public Map<Integer, Allergy> map = AllergyRegister.getInstance().getAllAllergies();
- public SharedPreferences.Editor editor;
- public LinearLayout list;
- public LinearLayout wheatlist;
- public DisplayMetrics dm;
- public ImageView dropIcon;
- public LinearLayout wheatHeader;
- public boolean visibility;
- public Set<Integer> wheatIds = new HashSet<Integer>();
- public AllergyProfileFragment() {
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- View v = inflater.inflate(R.layout.fragment_allergy_profile, container, false);
- sharedPreferences = getActivity().getSharedPreferences(selectedAllergies, Context.MODE_PRIVATE);
- editor = getActivity().getSharedPreferences(selectedAllergies, MODE_PRIVATE).edit();
- set = sharedPreferences.getStringSet("List", new HashSet<String>());
- dm = getResources().getDisplayMetrics();
- list = (LinearLayout) v.findViewById(R.id.list);
- for(final Allergy allergy : map.values()) {
- drawAllergy(allergy);
- }
- return v;
- }
- @Override
- public void onPause(){
- super.onPause();
- final Handler handler = new Handler();
- handler.postDelayed(new Runnable() {
- public void run() {
- editor.putStringSet("List", set);
- editor.commit();
- }
- }, 500);
- }
- public void drawAllergy(final Allergy allergy){
- final LinearLayout row = new LinearLayout(getActivity());
- LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
- params5.setMargins(0,0, getResources().getDimensionPixelOffset(R.dimen.margins), 0);
- row.setOrientation(LinearLayout.HORIZONTAL);
- LinearLayout allergyIconLayout = new LinearLayout(getActivity());
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
- params.setMargins(getResources().getDimensionPixelSize(R.dimen.margins), getResources().getDimensionPixelSize(R.dimen.margins), getResources().getDimensionPixelSize(R.dimen.margins), getResources().getDimensionPixelSize(R.dimen.margins));
- allergyIconLayout.setLayoutParams(params);
- // Create LayoutParams for it // Note 200 200 is width, height in pixels
- RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(toPixels(dm, 26),toPixels(dm, 26));
- // Align bottom-right, and add bottom-margin
- params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
- params2.addRule(RelativeLayout.CENTER_IN_PARENT);
- ImageView image = new ImageView (getActivity());
- image.setLayoutParams(new ViewGroup.LayoutParams((toPixels(dm, 46)), (toPixels(dm, 46))));
- String name = allergy.RoundIcon;
- int id = getResources().getIdentifier(name, "drawable", getActivity().getPackageName());
- Drawable drawable = getResources().getDrawable(id);
- image.setImageDrawable(drawable);
- image.setMaxHeight((toPixels(dm, 46)));
- image.setMaxWidth((toPixels(dm, 46)));
- image.setAdjustViewBounds(true);
- image.setLayoutParams(params);
- ImageView divider = new ImageView(getActivity());
- divider.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
- divider.setBackgroundColor(Color.GRAY);
- TextView textView = new TextView(getActivity());
- textView.setTextSize(16);
- textView.setTypeface(null, Typeface.BOLD);
- textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
- textView.setGravity(Gravity.CENTER_VERTICAL);
- textView.setText(allergy.Name);
- final SwitchCompat switc = new SwitchCompat(getActivity());
- switc.setLayoutParams(params5);
- switc.setTag(allergy.ProductAllergyID);
- switc.setChecked(sharedPreferences.getBoolean("" + allergy.ProductAllergyID, false));
- switc.setGravity(Gravity.CENTER_VERTICAL + Gravity.END);
- switc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if (isChecked) {
- editor.putBoolean(buttonView.getTag().toString(), true);
- set.add(String.valueOf(allergy.ProductAllergyID));
- editor.commit();
- } else {
- editor.putBoolean(buttonView.getTag().toString(), false);
- set.remove(String.valueOf(allergy.ProductAllergyID));
- editor.commit();
- }
- }
- });
- row.addView(image);
- row.addView(textView);
- row.addView(switc);
- list.addView(row);
- list.addView(divider);
- }
- public static int toPixels(DisplayMetrics metrics, int dp) {
- return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement