com.google.android.material.button.MaterialButtonToggleGroup Java Examples

The following examples show how to use com.google.android.material.button.MaterialButtonToggleGroup. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TimePickerView.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public TimePickerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  LayoutInflater.from(context).inflate(R.layout.material_timepicker, this);
  clockFace = findViewById(R.id.material_clock_face);
  toggle = findViewById(R.id.material_clock_period_toggle);
  toggle.addOnButtonCheckedListener(
      new OnButtonCheckedListener() {
        @Override
        public void onButtonChecked(
            MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
          int period = checkedId == R.id.material_clock_period_pm_button ? PM : AM;
          if (onPeriodChangeListener != null && isChecked) {
            onPeriodChangeListener.onPeriodChange(period);
          }
        }
      });

  minuteView = findViewById(R.id.material_minute_tv);
  hourView = findViewById(R.id.material_hour_tv);
  clockHandView = findViewById(R.id.material_clock_hand);

  setUpDisplay();
}
 
Example #2
Source File: MainActivity.java    From journaldev with MIT License 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnToggle = findViewById(R.id.toggleGroup);

    btnToggle.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
        @Override
        public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {

        }
    });

}
 
Example #3
Source File: ThemeSwitcherDialogFragment.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void initializeChooseThemeButtons(View view) {
  Context context = view.getContext();
  ThemePreferencesManager themePreferencesManager =
      new ThemePreferencesManager(context, resourceProvider);

  MaterialButtonToggleGroup themeToggleGroup = view.findViewById(R.id.theme_toggle_group);
  themeToggleGroup.check(themePreferencesManager.getCurrentThemeId());

  themeToggleGroup.addOnButtonCheckedListener((group, checkedId, isChecked) -> {
    if (isChecked) {
      themePreferencesManager.saveAndApplyTheme(checkedId);
    }
  });
}
 
Example #4
Source File: ContainerTransformConfigurationHelper.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
/** Update whether to use arc motion based on the selected radio button */
private void setUpBottomSheetPathMotionButtonGroup(View view) {
  MaterialButtonToggleGroup toggleGroup = view.findViewById(R.id.path_motion_button_group);
  if (toggleGroup != null) {
    // Set initial value.
    toggleGroup.check(arcMotionEnabled ? R.id.arc_motion_button : R.id.linear_motion_button);
    toggleGroup.addOnButtonCheckedListener(
        (group, checkedId, isChecked) -> {
          if (checkedId == R.id.arc_motion_button) {
            arcMotionEnabled = isChecked;
          }
        });
  }
}
 
Example #5
Source File: ContainerTransformConfigurationHelper.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
/** Update the fade mode based on the selected radio button */
private void setUpBottomSheetFadeModeButtonGroup(View view) {
  MaterialButtonToggleGroup toggleGroup = view.findViewById(R.id.fade_mode_button_group);
  if (toggleGroup != null) {
    // Set initial value.
    toggleGroup.check(fadeModeButtonId);
    toggleGroup.addOnButtonCheckedListener(
        (group, checkedId, isChecked) -> {
          if (isChecked) {
            fadeModeButtonId = checkedId;
          }
        });
  }
}
 
Example #6
Source File: ShapeableImageViewMainDemoFragment.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a demo fragment with an image that can change shape and randomely makes the dog image
 * wink.
 */
@Override
public View onCreateDemoView(
    LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
  View view =
      layoutInflater.inflate(R.layout.catalog_imageview, viewGroup, false /* attachToRoot */);
  MaterialButtonToggleGroup toggleGroup = view.findViewById(R.id.togglegroup);
  ShapeableImageView imageView = view.findViewById(R.id.image_view);

  SparseArray<ShapeAppearanceModel> shapes = new SparseArray<>();
  shapes.put(
      R.id.button_diamond,
      ShapeAppearanceModel.builder()
          .setAllCorners(CornerFamily.CUT, /*cornerSize=*/0f)
          .setAllCornerSizes(ShapeAppearanceModel.PILL)
          .build());
  shapes.put(
      R.id.button_circle,
      ShapeAppearanceModel.builder().setAllCornerSizes(ShapeAppearanceModel.PILL).build());
  shapes.put(R.id.button_square, ShapeAppearanceModel.builder().build());

  Random random = new Random();

  toggleGroup.addOnButtonCheckedListener(
      (group, checkedId, isChecked) -> {
        if (!isChecked) {
          return;
        }

        // Randomly makes dog wink.
        imageView.setImageResource(
            random.nextBoolean() ? R.drawable.dog_image : R.drawable.dog_image_wink);
        imageView.setShapeAppearanceModel(shapes.get(checkedId));
      });

  return view;
}
 
Example #7
Source File: ButtonToggleGroupDemoFragmentTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private static TypeSafeMatcher<View> checkSelectionRequired(boolean required) {
  return new TypeSafeMatcher<View>() {

    @Override
    public void describeTo(Description description) {}

    @Override
    protected boolean matchesSafely(View view) {
      MaterialButtonToggleGroup toggleGroup = (MaterialButtonToggleGroup) view;
      return required == toggleGroup.isSelectionRequired();
    }
  };
}
 
Example #8
Source File: TimePickerTextInputPresenter.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void setupPeriodToggle() {
  toggle = timePickerView.findViewById(R.id.material_clock_period_toggle);

  toggle.addOnButtonCheckedListener(
      new OnButtonCheckedListener() {
        @Override
        public void onButtonChecked(
            MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
          int period = checkedId == R.id.material_clock_period_pm_button ? PM : AM;
          time.setPeriod(period);
        }
      });
  toggle.setVisibility(View.VISIBLE);
  updateSelection();
}
 
Example #9
Source File: TimePickerMainDemoFragment.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("deprecation") // requireFragmentManager()
public View onCreateDemoView(
    LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
  ViewGroup view =
      (ViewGroup) layoutInflater.inflate(R.layout.time_picker_main_demo, viewGroup, false);

  Button button = view.findViewById(R.id.timepicker_button);
  textView = view.findViewById(R.id.timepicker_time);
  MaterialButtonToggleGroup timeFormatToggle = view.findViewById(R.id.time_format_toggle);
  clockFormat = TimeFormat.CLOCK_12H;
  timeFormatToggle.check(R.id.time_format_12h);

  timeFormatToggle.addOnButtonCheckedListener(
      (group, checkedId, isChecked) -> {
        boolean isSystem24Hour = DateFormat.is24HourFormat(getContext());
        boolean is24Hour =
            checkedId == R.id.time_format_24h
                || (checkedId == R.id.time_format_system && isSystem24Hour);

        clockFormat = is24Hour ? TimeFormat.CLOCK_24H : TimeFormat.CLOCK_12H;
      });

  SwitchCompat frameworkSwitch = view.findViewById(R.id.framework_switch);
  button.setOnClickListener(v -> {
    if (frameworkSwitch.isChecked()) {
      showFrameworkTimepicker();
      return;
    }
    TimePickerDialog timePickerDialog = TimePickerDialog.newInstance();
    timePickerDialog.show(requireFragmentManager(), "fragment_tag");
    timePickerDialog.setTimeFormat(clockFormat);
    timePickerDialog.setHour(hour);
    timePickerDialog.setMinute(minute);

    timePickerDialog.setListener(dialog -> {
      int newHour = dialog.getHour();
      int newMinute = dialog.getMinute();
      TimePickerMainDemoFragment.this.onTimeSet(newHour, newMinute);
    });
  });

  return view;
}