|
| 1 | +package com.llollox.androidprojects.compoundbuttongroupsample; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.support.annotation.Nullable; |
| 5 | +import android.support.v4.app.Fragment; |
| 6 | +import android.view.LayoutInflater; |
| 7 | +import android.view.View; |
| 8 | +import android.view.ViewGroup; |
| 9 | +import android.widget.Button; |
| 10 | +import android.widget.RelativeLayout; |
| 11 | +import android.widget.Toast; |
| 12 | + |
| 13 | +import com.llollox.androidprojects.compoundbuttongroup.CompoundButtonGroup; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | + |
| 17 | +/** |
| 18 | + * Created by lorenzorigato on 28/04/2017. |
| 19 | + */ |
| 20 | + |
| 21 | +public class CompoundButtonGroupFragment extends Fragment { |
| 22 | + |
| 23 | + private static class Argument { |
| 24 | + private static final String LAYOUT_ID = "layoutId"; |
| 25 | + } |
| 26 | + |
| 27 | + public static CompoundButtonGroupFragment newInstance(int layoutId) { |
| 28 | + Bundle args = new Bundle(); |
| 29 | + args.putInt(Argument.LAYOUT_ID, layoutId); |
| 30 | + CompoundButtonGroupFragment compoundButtonGroupFragment = new CompoundButtonGroupFragment(); |
| 31 | + compoundButtonGroupFragment.setArguments(args); |
| 32 | + return compoundButtonGroupFragment; |
| 33 | + } |
| 34 | + |
| 35 | + private CompoundButtonGroup compoundButtonGroup; |
| 36 | + private String[] planets; |
| 37 | + |
| 38 | + @Nullable |
| 39 | + @Override |
| 40 | + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
| 41 | + |
| 42 | + int layoutId; |
| 43 | + if (getArguments() != null && getArguments().containsKey(Argument.LAYOUT_ID)) { |
| 44 | + layoutId = getArguments().getInt(Argument.LAYOUT_ID); |
| 45 | + } |
| 46 | + else { |
| 47 | + throw new RuntimeException("A compound button group fragment requires a layout id as argument"); |
| 48 | + } |
| 49 | + |
| 50 | + planets = getResources().getStringArray(R.array.planets); |
| 51 | + |
| 52 | + LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); |
| 53 | + |
| 54 | + RelativeLayout relativeLayout = (RelativeLayout) layoutInflater.inflate( |
| 55 | + R.layout.compound_button_group_fragment, container, false); |
| 56 | + |
| 57 | + compoundButtonGroup = (CompoundButtonGroup) layoutInflater.inflate(layoutId, relativeLayout, false); |
| 58 | + |
| 59 | + |
| 60 | + RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) compoundButtonGroup.getLayoutParams(); |
| 61 | + params.addRule(RelativeLayout.ALIGN_PARENT_TOP); |
| 62 | + params.addRule(RelativeLayout.ABOVE, R.id.get_value_btn); |
| 63 | + |
| 64 | + compoundButtonGroup.setOnButtonSelectedListener(new CompoundButtonGroup.OnButtonSelectedListener() { |
| 65 | + @Override |
| 66 | + public void onButtonSelected(int position, boolean isChecked) { |
| 67 | + String planet = planets[position]; |
| 68 | + String checked = getString(isChecked ? R.string.checked : R.string.unchecked); |
| 69 | + Toast.makeText(getActivity(), checked + ": " + planet, Toast.LENGTH_SHORT).show(); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + Button getValueBtn = (Button) relativeLayout.findViewById(R.id.get_value_btn); |
| 74 | + getValueBtn.setOnClickListener(new View.OnClickListener() { |
| 75 | + @Override |
| 76 | + public void onClick(View v) { |
| 77 | + String checked = getString(R.string.checked); |
| 78 | + ArrayList<String> checkedPlanets = new ArrayList<>(); |
| 79 | + for (int position : compoundButtonGroup.getSelectedPositions()) { |
| 80 | + checkedPlanets.add(planets[position]); |
| 81 | + } |
| 82 | + Toast.makeText(getActivity(), |
| 83 | + checked + ": " + String.valueOf(checkedPlanets), |
| 84 | + Toast.LENGTH_SHORT).show(); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + relativeLayout.addView(compoundButtonGroup, 0); |
| 89 | + |
| 90 | + return relativeLayout; |
| 91 | + } |
| 92 | +} |
0 commit comments