Java Code Examples for android.widget.Switch#setEnabled()

The following examples show how to use android.widget.Switch#setEnabled() . 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: LinkingBeaconListFragment.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
@Override
public void onResume() {
    super.onResume();
    refresh();

    if (getView() != null) {
        Switch switchBtn = getView().findViewById(R.id.fragment_beacon_scan_switch);
        if (switchBtn != null) {
            switchBtn.setEnabled(LinkingUtil.isApplicationInstalled(getContext()));
        }
    }

    LinkingApplication app = (LinkingApplication) getActivity().getApplication();
    LinkingBeaconManager mgr = app.getLinkingBeaconManager();
    mgr.addOnBeaconConnectListener(this);
    mgr.addOnBeaconScanStateListener(this);
}
 
Example 2
Source File: KeyAssignmentUtils.java    From talkback with Apache License 2.0 6 votes vote down vote up
/**
 * Configures the screen switch preference toggle based on whether and where the screen switch is
 * currently assigned.
 *
 * @param screenSwitchToggle A switch that corresponds to the current assignment status of the
 *     screen switch.
 * @param keyCombos List of keys currently assigned to this action
 * @param context Context used to determine if the screen switch is currently assigned to a
 *     different action
 * @param key The key to the map that pairs an action with a set of switches currently assigned to
 *     said action.
 */
public static void updateScreenAsASwitchToggle(
    Switch screenSwitchToggle, Set<Long> keyCombos, Context context, String key) {
  long screenSwitchKeyCombo =
      KeyAssignmentUtils.keyEventToExtendedKeyCode(SCREEN_SWITCH_EVENT_UP);

  if (!keyCombos.isEmpty() && keyCombos.contains(screenSwitchKeyCombo)) {
    screenSwitchToggle.setEnabled(true);
    screenSwitchToggle.setChecked(true);
    screenSwitchToggle.setText(R.string.key_combo_preference_screen_switch_toggle);
  } else if (KeyAssignmentUtils.otherActionAssociatedWithKey(
      screenSwitchKeyCombo, context, key)) {
    screenSwitchToggle.setEnabled(false);
    screenSwitchToggle.setChecked(false);
    screenSwitchToggle.setText(R.string.key_combo_preference_screen_switch_toggle_disabled);
  } else {
    screenSwitchToggle.setEnabled(true);
    screenSwitchToggle.setChecked(false);
    screenSwitchToggle.setText(R.string.key_combo_preference_screen_switch_toggle);
  }
}
 
Example 3
Source File: ToggleElement.java    From debugdrawer with Apache License 2.0 6 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup root) {
    final Context context = root.getContext();
    checked = new BooleanPreference(
        context.getSharedPreferences(getKey(),Context.MODE_PRIVATE),
        getTitle(),
        String.valueOf(defaultValue));
    if(!checked.isSet()) checked.set(defaultValue);

    aSwitch = new Switch(new ContextThemeWrapper(context, R.style.Widget_U2020_DebugDrawer_RowWidget));
    aSwitch.setText(title);
    aSwitch.setChecked(checked.get());
    aSwitch.setEnabled(isEnabled());
    aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            checked.set(isChecked);
            onSwitch(isChecked);
        }
    });
    aSwitch.setGravity(Gravity.START | Gravity.END | Gravity.CENTER_VERTICAL); // "start|end|center_vertical"
    return aSwitch;
}
 
Example 4
Source File: MergeLayoutConfigView.java    From QNRTC-Android with Apache License 2.0 5 votes vote down vote up
private void updateSwitchState(UserTrack userTrack, Switch switchButton) {
    if (userTrack != null) {
        switchButton.setChecked(userTrack.isTrackInclude());
        switchButton.setEnabled(true);
    } else {
        switchButton.setChecked(true);
        switchButton.setEnabled(false);
    }
}
 
Example 5
Source File: BackupImportChooserActivitySwitchSimpleAdapter.java    From FreezeYou with Apache License 2.0 5 votes vote down vote up
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    Switch s = view.findViewById(R.id.bica_list_item_switch);

    if (s != null) {

        s.setOnCheckedChangeListener(null);

        String category = mData.get(position).get("category");
        s.setChecked(!isDisabledList.contains(position));
        if ("Failed!".equals(category)) {
            s.setChecked(true);
            s.setEnabled(false);
        }
        s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    isDisabledList.remove((Integer) position);
                    needExcludeData.remove(mData.get(position));
                } else {
                    if (!needExcludeData.contains(mData.get(position))) {
                        needExcludeData.add(mData.get(position));
                    }
                    if (!isDisabledList.contains(position)) {
                        isDisabledList.add(position);
                    }
                }
            }
        });
    }

    return view;
}
 
Example 6
Source File: HOGPSettingActivity.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
void onServiceDisconnected() {
    Switch sw =  findViewById(R.id.activity_setting_device_switch);
    sw.setOnCheckedChangeListener(null);
    sw.setEnabled(false);
    findViewById(R.id.activity_setting_btn).setEnabled(false);

    HOGPMessageService service = getHOGPMessageService();
    if (service != null) {
        service.getServiceProvider().removeServiceListener(this);
    }
}
 
Example 7
Source File: ModulesFragment.java    From EdXposedManager with GNU General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    View view = super.getView(position, convertView, parent);

    if (convertView == null) {
        // The reusable view was created for the first time, set up the
        // listener on the checkbox
        ((Switch) view.findViewById(R.id.checkbox)).setOnCheckedChangeListener((buttonView, isChecked) -> {
            String packageName = (String) buttonView.getTag();
            boolean changed = mModuleUtil.isModuleEnabled(packageName) ^ isChecked;
            if (changed) {
                mModuleUtil.setModuleEnabled(packageName, isChecked);
                mModuleUtil.updateModulesList(true, view);
            }
        });
    }

    InstalledModule item = getItem(position);

    TextView version = view.findViewById(R.id.version_name);
    version.setText(Objects.requireNonNull(item).versionName);
    version.setSelected(true);
    version.setTextColor(Color.parseColor("#808080"));

    TextView packageTv = view.findViewById(R.id.package_name);
    packageTv.setText(item.packageName);
    packageTv.setSelected(true);

    TextView installTimeTv = view.findViewById(R.id.tvInstallTime);
    installTimeTv.setText(dateformat.format(new Date(item.installTime)));
    installTimeTv.setSelected(true);

    TextView updateTv = view.findViewById(R.id.tvUpdateTime);
    updateTv.setText(dateformat.format(new Date(item.updateTime)));
    updateTv.setSelected(true);

    // Store the package name in some views' tag for later access
    view.findViewById(R.id.checkbox).setTag(item.packageName);
    view.setTag(item.packageName);

    ((ImageView) view.findViewById(R.id.icon)).setImageDrawable(item.getIcon());

    TextView descriptionText = view.findViewById(R.id.description);
    if (!item.getDescription().isEmpty()) {
        descriptionText.setText(item.getDescription());
        descriptionText.setTextColor(ThemeUtil.getThemeColor(getContext(), android.R.attr.textColorSecondary));
    } else {
        descriptionText.setText(getString(R.string.module_empty_description));
        descriptionText.setTextColor(getResources().getColor(R.color.warning, null));
    }

    Switch mSwitch = view.findViewById(R.id.checkbox);
    mSwitch.setChecked(mModuleUtil.isModuleEnabled(item.packageName));
    TextView warningText = view.findViewById(R.id.warning);

    if (item.minVersion == 0) {
        if (!XposedApp.getPreferences().getBoolean("skip_xposedminversion_check", false)) {
            mSwitch.setEnabled(false);
        }
        warningText.setText(getString(R.string.no_min_version_specified));
        warningText.setVisibility(View.VISIBLE);
    } else if (installedXposedVersion > 0 && item.minVersion > installedXposedVersion) {
        if (!XposedApp.getPreferences().getBoolean("skip_xposedminversion_check", false)) {
            mSwitch.setEnabled(false);
        }
        warningText.setText(String.format(getString(R.string.warning_xposed_min_version), item.minVersion));
        warningText.setVisibility(View.VISIBLE);
    } else if (item.minVersion < ModuleUtil.MIN_MODULE_VERSION) {
        if (!XposedApp.getPreferences().getBoolean("skip_xposedminversion_check", false)) {
            mSwitch.setEnabled(false);
        }
        warningText.setText(String.format(getString(R.string.warning_min_version_too_low), item.minVersion, ModuleUtil.MIN_MODULE_VERSION));
        warningText.setVisibility(View.VISIBLE);
    } else if (item.isInstalledOnExternalStorage()) {
        if (!XposedApp.getPreferences().getBoolean("skip_xposedminversion_check", false)) {
            mSwitch.setEnabled(false);
        }
        warningText.setText(getString(R.string.warning_installed_on_external_storage));
        warningText.setVisibility(View.VISIBLE);
    } else if (installedXposedVersion == 0 || (installedXposedVersion == -1 && !StatusInstallerFragment.DISABLE_FILE.exists())) {
        if (!XposedApp.getPreferences().getBoolean("skip_xposedminversion_check", false)) {
            mSwitch.setEnabled(false);
        }
        warningText.setText(getString(R.string.not_installed_no_lollipop));
        warningText.setVisibility(View.VISIBLE);
    } else {
        mSwitch.setEnabled(true);
        warningText.setVisibility(View.GONE);
    }
    return view;
}