Example usage for android.widget Switch toggle

List of usage examples for android.widget Switch toggle

Introduction

In this page you can find the example usage for android.widget Switch toggle.

Prototype

@Override
    public void toggle() 

Source Link

Usage

From source file:com.synox.android.ui.fragment.ShareFileFragment.java

/**
 * Updates in the UI the section about public share with the information in the current
 * public share bound to mFile, if any// ww w. j  a v a2s  .  c  o m
 */
private void updatePublicShareSection() {
    if (mPublicShare != null && ShareType.PUBLIC_LINK.equals(mPublicShare.getShareType())) {
        /// public share bound -> expand section
        Switch shareViaLinkSwitch = getShareViaLinkSwitch();
        if (!shareViaLinkSwitch.isChecked()) {
            // set null listener before setChecked() to prevent infinite loop of calls
            shareViaLinkSwitch.setOnCheckedChangeListener(null);
            shareViaLinkSwitch.setChecked(true);
            shareViaLinkSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener);
        }
        getExpirationDateSection().setVisibility(View.VISIBLE);
        getPasswordSection().setVisibility(View.VISIBLE);
        // GetLink button
        AppCompatButton getLinkButton = getGetLinkButton();
        getLinkButton.setVisibility(View.VISIBLE);
        getLinkButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //GetLink from the server and show ShareLinkToDialog
                ((FileActivity) getActivity()).getFileOperationsHelper().getFileWithLink(mFile);

            }
        });

        /// update state of expiration date switch and message depending on expiration date
        Switch expirationDateSwitch = getExpirationDateSwitch();
        // set null listener before setChecked() to prevent infinite loop of calls
        expirationDateSwitch.setOnCheckedChangeListener(null);
        long expirationDate = mPublicShare.getExpirationDate();
        if (expirationDate > 0) {
            if (!expirationDateSwitch.isChecked()) {
                expirationDateSwitch.toggle();
            }
            String formattedDate = SimpleDateFormat.getDateInstance().format(new Date(expirationDate));
            getExpirationDateValue().setText(formattedDate);
        } else {
            if (expirationDateSwitch.isChecked()) {
                expirationDateSwitch.toggle();
            }
            getExpirationDateValue().setText(R.string.empty);
        }
        // recover listener
        expirationDateSwitch.setOnCheckedChangeListener(mOnExpirationDateInteractionListener);

        /// update state of password switch and message depending on password protection
        Switch passwordSwitch = getPasswordSwitch();
        // set null listener before setChecked() to prevent infinite loop of calls
        passwordSwitch.setOnCheckedChangeListener(null);
        if (mPublicShare.isPasswordProtected()) {
            if (!passwordSwitch.isChecked()) {
                passwordSwitch.toggle();
            }
            getPasswordValue().setVisibility(View.VISIBLE);
        } else {
            if (passwordSwitch.isChecked()) {
                passwordSwitch.toggle();
            }
            getPasswordValue().setVisibility(View.INVISIBLE);
        }
        // recover listener
        passwordSwitch.setOnCheckedChangeListener(mOnPasswordInteractionListener);

    } else {
        /// no public share -> collapse section
        Switch shareViaLinkSwitch = getShareViaLinkSwitch();
        if (shareViaLinkSwitch.isChecked()) {
            shareViaLinkSwitch.setOnCheckedChangeListener(null);
            getShareViaLinkSwitch().setChecked(false);
            shareViaLinkSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener);
        }
        getExpirationDateSection().setVisibility(View.GONE);
        getPasswordSection().setVisibility(View.GONE);
        getGetLinkButton().setVisibility(View.GONE);
    }
}

From source file:org.mozilla.focus.widget.TelemetrySwitchPreference.java

@Override
protected void onBindView(final View view) {
    super.onBindView(view);

    final Switch switchWidget = view.findViewById(R.id.switch_widget);

    switchWidget.setChecked(TelemetryWrapper.isTelemetryEnabled(getContext()));

    switchWidget.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override//w ww .  ja va2  s . co  m
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            TelemetryWrapper.setTelemetryEnabled(getContext(), isChecked);
        }
    });

    final Resources resources = view.getResources();

    final TextView summary = view.findViewById(android.R.id.summary);
    summary.setText(resources.getString(R.string.preference_mozilla_telemetry_summary2,
            resources.getString(R.string.app_name)));

    final TextView learnMoreLink = view.findViewById(R.id.link);
    learnMoreLink.setPaintFlags(learnMoreLink.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    learnMoreLink.setTextColor(ContextCompat.getColor(view.getContext(), R.color.colorAction));
    learnMoreLink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // This is a hardcoded link: if we ever end up needing more of these links, we should
            // move the link into an xml parameter, but there's no advantage to making it configurable now.
            final String url = SupportUtils.getSumoURLForTopic(getContext(), "usage-data");
            final String title = getTitle().toString();

            final Intent intent = InfoActivity.getIntentFor(getContext(), url, title);
            getContext().startActivity(intent);
        }
    });

    final TypedArray backgroundDrawableArray = view.getContext()
            .obtainStyledAttributes(new int[] { R.attr.selectableItemBackground });
    final Drawable backgroundDrawable = backgroundDrawableArray.getDrawable(0);
    backgroundDrawableArray.recycle();
    learnMoreLink.setBackground(backgroundDrawable);

    // We still want to allow toggling the pref by touching any part of the pref (except for
    // the "learn more" link)
    setOnPreferenceClickListener(new OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            switchWidget.toggle();
            return true;
        }
    });
}