Example usage for android.widget Switch setOnCheckedChangeListener

List of usage examples for android.widget Switch setOnCheckedChangeListener

Introduction

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

Prototype

public void setOnCheckedChangeListener(@Nullable OnCheckedChangeListener listener) 

Source Link

Document

Register a callback to be invoked when the checked state of this button changes.

Usage

From source file:android.support.v14.preference.SwitchPreference.java

private void syncSwitchView(View view) {
    if (view instanceof Switch) {
        final Switch switchView = (Switch) view;
        switchView.setOnCheckedChangeListener(null);
    }//w w w.j av a2  s.  co  m
    if (view instanceof Checkable) {
        ((Checkable) view).setChecked(mChecked);
    }
    if (view instanceof Switch) {
        final Switch switchView = (Switch) view;
        switchView.setTextOn(mSwitchOn);
        switchView.setTextOff(mSwitchOff);
        switchView.setOnCheckedChangeListener(mListener);
    }
}

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//  ww w  .  j av a  2 s  . c o 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;
        }
    });
}

From source file:com.kubotaku.android.code4kyoto5374.fragments.NotifySettingsDialogFragment.java

private void setupViews() {
    final View view = getView();

    final TextView alarmTime = (TextView) view.findViewById(R.id.alarm_text_time);
    alarmTime.setText(this.alarm.toString());
    alarmTime.setOnClickListener(onClickAlarmTimeListener);

    final Switch alarmSwitch = (Switch) view.findViewById(R.id.alarm_switch);
    alarmSwitch.setChecked(this.alarm.enable);
    alarmSwitch.setOnCheckedChangeListener(onCheckedChangeListener);
}

From source file:eu.geopaparazzi.library.core.dialogs.StrokeDashDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle bundle) {

    // create the dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    View dashStrokeDialogView = getActivity().getLayoutInflater().inflate(R.layout.fragment_dialog_stroke_dash,
            null);/*w ww .ja v a 2s .c o  m*/
    builder.setView(dashStrokeDialogView); // add GUI to dialog

    Switch switch1 = (Switch) dashStrokeDialogView.findViewById(R.id.switch1);
    Switch switch2 = (Switch) dashStrokeDialogView.findViewById(R.id.switch2);
    Switch switch3 = (Switch) dashStrokeDialogView.findViewById(R.id.switch3);
    Switch switch4 = (Switch) dashStrokeDialogView.findViewById(R.id.switch4);
    Switch switch5 = (Switch) dashStrokeDialogView.findViewById(R.id.switch5);
    Switch switch6 = (Switch) dashStrokeDialogView.findViewById(R.id.switch6);
    dashSwitches = new Switch[] { switch1, switch2, switch3, switch4, switch5, switch6 };
    for (Switch dashSwitch : dashSwitches) {
        dashSwitch.setOnCheckedChangeListener(this);
    }

    LinearLayout image1 = (LinearLayout) dashStrokeDialogView.findViewById(R.id.imageView1);
    LinearLayout image2 = (LinearLayout) dashStrokeDialogView.findViewById(R.id.imageView2);
    LinearLayout image3 = (LinearLayout) dashStrokeDialogView.findViewById(R.id.imageView3);
    LinearLayout image4 = (LinearLayout) dashStrokeDialogView.findViewById(R.id.imageView4);
    LinearLayout image5 = (LinearLayout) dashStrokeDialogView.findViewById(R.id.imageView5);
    LinearLayout image6 = (LinearLayout) dashStrokeDialogView.findViewById(R.id.imageView6);
    dashImages = new LinearLayout[] { image1, image2, image3, image4, image5, image6 };

    unitText = (EditText) dashStrokeDialogView.findViewById(R.id.unitText);
    finalDashText = (EditText) dashStrokeDialogView.findViewById(R.id.finalDashText);
    finalShiftText = (EditText) dashStrokeDialogView.findViewById(R.id.finalDashShiftText);
    if (mCurrentDash != null) {
        String dashStr = Style.dashToString(mCurrentDash, null);
        finalDashText.setText(dashStr);
        finalShiftText.setText(mDashShift + "");
    }

    builder.setPositiveButton(R.string.set_dash, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            if (iDashStrokePropertiesChangeListener != null) {
                iDashStrokePropertiesChangeListener.onDashChanged(mCurrentDash, mDashShift);
            }
        }
    });
    builder.setNegativeButton(getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
        }
    });

    return builder.create(); // return dialog
}

From source file:net.hyx.app.volumenotification.activity.ItemViewActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_buttons_item, menu);
    LinearLayout actionLayout = (LinearLayout) menu.findItem(R.id.item_btn_checked_layout).getActionView();
    Switch statusInput = (Switch) actionLayout.findViewById(R.id.menu_item_switch);
    statusInput.setChecked((frag.item.status == 1));
    statusInput.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override//from   ww w  .j  av  a  2s  . c o  m
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            frag.item.status = (isChecked) ? 1 : 0;
            frag.model.saveItem(frag.item);

        }
    });
    return super.onCreateOptionsMenu(menu);
}

From source file:org.deviceconnect.android.uiapp.fragment.profile.MediaStreamRecordingProfileFragment.java

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
        final Bundle savedInstanceState) {
    mLogger.entering(getClass().getName(), "onCreateView",
            new Object[] { inflater, container, savedInstanceState });
    super.onCreate(savedInstanceState);

    View view = inflater.inflate(R.layout.fragment_mediastream, container, false);
    view.setBackgroundColor(getResources().getColor(android.R.color.background_light));

    // /*  w  w w  .j a  v  a  2  s  .  co  m*/
    Button takePhotoBtn = (Button) view.findViewById(R.id.fragment_mediastream_take_photo);
    takePhotoBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {
            v.setEnabled(false);
            (new TaskPhotoTask()).execute();
        }
    });

    Switch onphoto = (Switch) view.findViewById(R.id.fragment_mediastream_onphoto);
    onphoto.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
        }
    });

    Switch ondata = (Switch) view.findViewById(R.id.fragment_mediastream_ondata);
    ondata.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
            if (isChecked) {
                (new RegisterOnDataAvaiableTask()).execute();
            } else {
                (new UnegisterOnDataAvaiableTask()).execute();
            }
        }
    });

    mLogger.exiting(this.getClass().getName(), "onCreateView");
    return view;
}

From source file:xyz.jamescarroll.genipass.Fragment.PasswordFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    Switch s = ((Switch) findView(R.id.s_toggle_space));
    mSpaces = PreferenceManager.getDefaultSharedPreferences(getActivity())
            .getBoolean(getString(R.string.sp_toggle_spaces), true);

    handlePassword(mSpaces);//from w w  w. j av a 2  s.com

    s.setChecked(mSpaces);
    s.setOnCheckedChangeListener(this);
}

From source file:org.mozilla.mozstumbler.client.navdrawer.MainDrawerActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    mMenuItemStartStop = menu.add(Menu.NONE, MENU_START_STOP, Menu.NONE, R.string.start_scanning);
    if (Build.VERSION.SDK_INT >= 14) {
        Switch s = new Switch(this);
        s.setChecked(false);//ww  w .j a  v a 2s  .  co m
        s.setOnCheckedChangeListener(mStartStopButtonListener);
        mMenuItemStartStop.setActionView(s);
        mMenuItemStartStop.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    } else {
        MenuItemCompat.setShowAsAction(mMenuItemStartStop, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
    }

    updateStartStopMenuItemState();
    return true;
}

From source file:com.serenegiant.aceparrot.ConfigAppFragment.java

private void initOthers(final View rootView) {
    // ??//from   ww w.  j  a  v a 2s .co m
    mAutoHide = mPref.getBoolean(KEY_AUTO_HIDE, false);
    Switch sw = (Switch) rootView.findViewById(R.id.icon_auto_hide_switch);
    sw.setChecked(mAutoHide);
    sw.setOnCheckedChangeListener(mOnCheckedChangeListener);
}

From source file:com.serenegiant.aceparrot.ConfigAppFragment.java

private void initGamepad(final View rootView) {
    // ?????????//www .j  a v  a2s. c o m
    mScriptGamepad = mPref.getBoolean(KEY_CONFIG_GAMEPAD_ENABLE_SCRIPT, false);
    Switch sw = (Switch) rootView.findViewById(R.id.enable_gamepad_script_switch);
    sw.setChecked(mScriptGamepad);
    sw.setOnCheckedChangeListener(mOnCheckedChangeListener);
}