Example usage for android.widget Switch setChecked

List of usage examples for android.widget Switch setChecked

Introduction

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

Prototype

@Override
    public void setChecked(boolean checked) 

Source Link

Usage

From source file:com.javadog.bluetoothproximitylock.test.BluetoothFragmentTest.java

/**
 * Tests {@link com.javadog.bluetoothproximitylock.BluetoothFragment#startBtService()}
 *
 * This will only test the state of the boolean serviceRunning and the state
 * of the related UI elements. Can't get a reference to the service directly.
 */// ww  w  .j  av a 2  s. co m
public void testStartService() {
    BluetoothFragment fragment = new BluetoothFragment() {
        @Override
        protected void startBtService() {
            Switch toggle = (Switch) getView().findViewById(R.id.button_bt_service_start_stop);
            toggle.setChecked(false);

            serviceBound = false;

            super.startBtService();

            assertTrue("Switch should be in enabled state", toggle.isChecked());
            assertTrue("serviceRunning should be true", serviceBound);
        }
    };

    addFragment(fragment);
}

From source file:cz.kovar.petr.homevoice.frontend.FragmentSettings.java

private void initZWayLayout(View aView) {

    final EditText remoteURLEdit = (EditText) aView.findViewById(R.id.remoteURL);
    remoteURLEdit.setText(m_profile.getRemoteURL());
    remoteURLEdit.setOnEditorActionListener(new ClearFocusListener(remoteURLEdit));
    remoteURLEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override/*from w  w  w .ja v a  2 s. c o  m*/
        public void onFocusChange(View view, boolean hasFocus) {
            if (!hasFocus) {
                m_profile.setRemoteURL(remoteURLEdit.getText().toString());
                onZWayProfileChanged(m_profile);
            }
        }
    });

    final EditText localIPEdit = (EditText) aView.findViewById(R.id.localIP);
    localIPEdit.setText(m_profile.getLocalIP());
    localIPEdit.setOnEditorActionListener(new ClearFocusListener(localIPEdit));
    localIPEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (!hasFocus) {
                m_profile.setLocalIP(localIPEdit.getText().toString());
                onZWayProfileChanged(m_profile);
            }
        }
    });

    final EditText localPortEdit = (EditText) aView.findViewById(R.id.localPort);
    localPortEdit.setText(String.format(Locale.ENGLISH, "%d", m_profile.getLocalPort()));
    localPortEdit.setOnEditorActionListener(new ClearFocusListener(localPortEdit));
    localPortEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (!hasFocus) {
                m_profile.setLocalPort(Integer.valueOf(localPortEdit.getText().toString()));
                onZWayProfileChanged(m_profile);
            }
        }
    });

    final EditText loginEdit = (EditText) aView.findViewById(R.id.login);
    loginEdit.setText(m_profile.getRemoteLogin());
    loginEdit.setOnEditorActionListener(new ClearFocusListener(loginEdit));
    loginEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (!hasFocus) {
                m_profile.setRemoteLogin(loginEdit.getText().toString());
                onZWayProfileChanged(m_profile);
            }
        }
    });

    final EditText passwordEdit = (EditText) aView.findViewById(R.id.password);
    passwordEdit.setText(m_profile.getPassword());
    passwordEdit.setOnEditorActionListener(new ClearFocusListener(passwordEdit));
    passwordEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (!hasFocus) {
                m_profile.setPassword(passwordEdit.getText().toString());
                onZWayProfileChanged(m_profile);
            }
        }
    });

    final Switch remoteSwitch = (Switch) aView.findViewById(R.id.remoteSwitch);
    remoteSwitch.setChecked(m_profile.useRemote());
    remoteSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            m_profile.useRemote(b);
            onZWayProfileChanged(m_profile);
        }
    });

    final TextView firebaseAPI = (TextView) aView.findViewById(R.id.firebaseAPI);
    firebaseAPI.setText(FirebaseInstanceId.getInstance().getToken());

}

From source file:samsungsami.io.example.samiremotecontrol.DeviceActivity.java

/**
 * Fill the device status and info table
 *//*from w  w  w.j a v  a  2  s.c o m*/
void fillTable() {

    TableRow row;
    TextView t1, t2, t3;
    for (int current = 0; current < fields.size(); current++) {
        row = new TableRow(this);
        t1 = new TextView(this);
        t1.setTextColor(Color.GRAY);
        t2 = new TextView(this);
        t2.setTextColor(Color.BLACK);
        t3 = new TextView(this);
        t3.setTextColor(Color.BLACK);

        t1.setText(fields.get(current));
        t2.setText(values.get(current));
        t3.setText(units.get(current));

        t1.setTextSize(15);
        t2.setTextSize(15);
        t3.setTextSize(15);

        row.addView(t1);

        if (fields.get(current).equals("state")) {
            Switch toggle = new Switch(this);
            toggle.setChecked(values.get(current).toLowerCase().equals("on"));
            toggle.setEnabled(false);
            row.addView(toggle);
        } else
            row.addView(t2);

        row.addView(t3);
        propertiesTableLayout.addView(row, new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        // Adjust the view
        propertiesTableLayout.setColumnStretchable(0, true);
        propertiesTableLayout.setColumnStretchable(1, true);
        propertiesTableLayout.setColumnStretchable(2, true);
    }
}

From source file:com.readystatesoftware.ghostlog.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Switch mainSwitch = new Switch(this);
    mainSwitch.setChecked(LogService.isRunning());
    mainSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override//from w ww .  j a v  a  2 s. c  om
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            Intent intent = new Intent(MainActivity.this, LogService.class);
            if (b) {
                if (!LogService.isRunning()) {
                    startService(intent);
                }
            } else {
                stopService(intent);
            }
        }
    });

    final ActionBar bar = getActionBar();
    final ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    lp.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
    lp.rightMargin = getResources().getDimensionPixelSize(R.dimen.main_switch_margin_right);
    bar.setCustomView(mainSwitch, lp);
    bar.setDisplayShowCustomEnabled(true);

    mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    if (!mPrefs.getBoolean(PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES, false)) {
        PreferenceManager.setDefaultValues(this, R.xml.pref_filters, true);
        PreferenceManager.setDefaultValues(this, R.xml.pref_appearance, true);
        SharedPreferences.Editor edit = mPrefs.edit();
        edit.putBoolean(PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES, true);
        edit.apply();
    }

}

From source file:eu.power_switch.gui.fragment.alarm_clock.StockAlarmClockFragment.java

@Override
public void onCreateViewEvent(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_stock_alarm_clock, container, false);

    setHasOptionsMenu(true);/*from w  w  w .j  av  a2  s  .  c  om*/

    final RecyclerViewFragment recyclerViewFragment = this;

    Switch switchOnOff = (Switch) rootView.findViewById(R.id.switch_on_off);
    switchOnOff.setChecked(SmartphonePreferencesHandler.getStockAlarmClockEnabled());
    switchOnOff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (buttonView.isPressed()) {
                SmartphonePreferencesHandler.setStockAlarmClockEnabled(isChecked);
            }
        }
    });

    spinnerEventType = (Spinner) rootView.findViewById(R.id.spinner_sleep_as_android_event);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(),
            R.array.stock_alarm_clock_event_names, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerEventType.setAdapter(adapter);
    SpinnerInteractionListener spinnerInteractionListener = new SpinnerInteractionListener() {
        @Override
        public void onItemSelectedByUser(AdapterView<?> parent, View view, int pos, long id) {
            updateUI();
        }
    };
    spinnerEventType.setOnItemSelectedListener(spinnerInteractionListener);
    spinnerEventType.setOnTouchListener(spinnerInteractionListener);

    recyclerViewActions = (RecyclerView) rootView.findViewById(R.id.recyclerView);
    recyclerViewAdapter = new ActionRecyclerViewAdapter(getContext(), actions);
    recyclerViewAdapter.setOnDeleteClickListener(new ActionRecyclerViewAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View itemView, final int position) {
            new AlertDialog.Builder(getContext()).setTitle(R.string.delete).setMessage(R.string.are_you_sure)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            try {
                                actions.remove(position);
                                DatabaseHandler.setAlarmActions(
                                        Event.getById(spinnerEventType.getSelectedItemPosition()), actions);
                                StatusMessageHandler.showInfoMessage(recyclerViewFragment.getRecyclerView(),
                                        R.string.action_removed, Snackbar.LENGTH_LONG);
                            } catch (Exception e) {
                                StatusMessageHandler.showErrorMessage(recyclerViewFragment.getRecyclerView(),
                                        e);
                            }

                            recyclerViewAdapter.notifyDataSetChanged();
                        }
                    }).setNeutralButton(android.R.string.cancel, null).show();
        }
    });
    recyclerViewActions.setAdapter(recyclerViewAdapter);
    StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(getSpanCount(),
            StaggeredGridLayoutManager.VERTICAL);
    recyclerViewActions.setLayoutManager(layoutManager);

    addActionFAB = (FloatingActionButton) rootView.findViewById(R.id.add_fab);
    addActionFAB.setImageDrawable(IconicsHelper.getAddIcon(getActivity(),
            ContextCompat.getColor(getActivity(), android.R.color.white)));
    addActionFAB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AddStockAlarmClockEventActionDialog addAlarmEventActionDialog = AddStockAlarmClockEventActionDialog
                    .newInstance(spinnerEventType.getSelectedItemPosition());
            addAlarmEventActionDialog.setTargetFragment(recyclerViewFragment, 0);
            addAlarmEventActionDialog.show(getActivity().getSupportFragmentManager(), null);
        }
    });

    // BroadcastReceiver to get notifications from background service if room data has changed
    broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(this, "received intent: " + intent.getAction());

            if (LocalBroadcastConstants.INTENT_ALARM_EVENT_ACTION_ADDED.equals(intent.getAction())) {
                updateUI();
            } else {
                updateUI();
            }
        }
    };
}

From source file:com.example.android.shushme.MainActivity.java

/**
 * Called when the activity is starting/*  ww  w  .  j  a va  2s.  c om*/
 *
 * @param savedInstanceState The Bundle that contains the data supplied in onSaveInstanceState
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Set up the recycler view
    mRecyclerView = (RecyclerView) findViewById(R.id.places_list_recycler_view);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mAdapter = new PlaceListAdapter(this, null);
    mRecyclerView.setAdapter(mAdapter);

    // Initialize the switch state and Handle enable/disable switch change
    Switch onOffSwitch = (Switch) findViewById(R.id.enable_switch);
    mIsEnabled = getPreferences(MODE_PRIVATE).getBoolean(getString(R.string.setting_enabled), false);
    onOffSwitch.setChecked(mIsEnabled);
    onOffSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
            editor.putBoolean(getString(R.string.setting_enabled), isChecked);
            mIsEnabled = isChecked;
            editor.commit();
            if (isChecked)
                mGeofencing.registerAllGeofences();
            else
                mGeofencing.unRegisterAllGeofences();
        }

    });

    mClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this)
            .addApi(LocationServices.API).addApi(Places.GEO_DATA_API).enableAutoManage(this, this).build();

    mGeofencing = new Geofencing(this, mClient);
}

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

private void initVoice(final View rootView) {
    // ??????(Android>=6)
    mOfflineVoiceRecognition = mPref.getBoolean(KEY_CONFIG_VOICE_RECOGNITION_PREFER_OFFLINE, false)
            && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
    Switch sw = (Switch) rootView.findViewById(R.id.enable_offline_voice_recognition_switch);
    sw.setEnabled(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M);
    sw.setChecked(mOfflineVoiceRecognition);
    sw.setOnCheckedChangeListener(mOnCheckedChangeListener);
    // /*  w  w  w. j a  va  2  s .com*/
    mDampingRateTv = (TextView) rootView.findViewById(R.id.damping_rate_textview);
    mDampingRate = mPref.getInt(KEY_CONFIG_VOICE_RECOGNITION_DAMPING_RATE,
            DEFAULT_VOICE_RECOGNITION_DAMPING_RATE);
    final SeekBar seekBar = (SeekBar) rootView.findViewById(R.id.damping_rate_seekbar);
    seekBar.setProgress(mDampingRate);
    seekBar.setOnSeekBarChangeListener(mOnSeekBarChangeListener);
    updateDampingRate(mDampingRate);
    // ?????????
    mScriptVoiceRecognition = mPref.getBoolean(KEY_CONFIG_VOICE_RECOGNITION_ENABLE_SCRIPT, false);
    sw = (Switch) rootView.findViewById(R.id.enable_voice_recognition_script_switch);
    sw.setChecked(mScriptVoiceRecognition);
    sw.setOnCheckedChangeListener(mOnCheckedChangeListener);
}

From source file:eu.power_switch.gui.fragment.alarm_clock.SleepAsAndroidFragment.java

@Override
public void onCreateViewEvent(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_sleep_as_android, container, false);

    setHasOptionsMenu(true);/*  w  ww .j ava  2s.  c om*/

    final RecyclerViewFragment recyclerViewFragment = this;

    Switch switchOnOff = (Switch) rootView.findViewById(R.id.switch_on_off);
    switchOnOff.setChecked(SmartphonePreferencesHandler.getSleepAsAndroidEnabled());
    switchOnOff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (buttonView.isPressed()) {
                SmartphonePreferencesHandler.setSleepAsAndroidEnabled(isChecked);
            }
        }
    });

    layout_installed = (LinearLayout) rootView.findViewById(R.id.layout_installed);
    layout_not_installed = (LinearLayout) rootView.findViewById(R.id.layout_not_installed);

    IconicsImageView getFromPlayStore = (IconicsImageView) rootView.findViewById(R.id.get_from_play_store);
    getFromPlayStore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SleepAsAndroidHelper.openPlayStorePage(getActivity());
        }
    });

    spinnerEventType = (Spinner) rootView.findViewById(R.id.spinner_sleep_as_android_event);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(),
            R.array.sleep_as_android_event_names, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerEventType.setAdapter(adapter);
    SpinnerInteractionListener spinnerInteractionListener = new SpinnerInteractionListener() {
        @Override
        public void onItemSelectedByUser(AdapterView<?> parent, View view, int pos, long id) {
            updateUI();
        }
    };
    spinnerEventType.setOnItemSelectedListener(spinnerInteractionListener);
    spinnerEventType.setOnTouchListener(spinnerInteractionListener);

    recyclerViewActions = (RecyclerView) rootView.findViewById(R.id.recyclerView);
    recyclerViewAdapter = new ActionRecyclerViewAdapter(getContext(), actions);
    recyclerViewAdapter.setOnDeleteClickListener(new ActionRecyclerViewAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View itemView, final int position) {
            new AlertDialog.Builder(getContext()).setTitle(R.string.delete).setMessage(R.string.are_you_sure)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            try {
                                actions.remove(position);
                                DatabaseHandler.setAlarmActions(SLEEP_AS_ANDROID_ALARM_EVENT
                                        .getById(spinnerEventType.getSelectedItemPosition()), actions);
                                StatusMessageHandler.showInfoMessage(recyclerViewFragment.getRecyclerView(),
                                        R.string.action_removed, Snackbar.LENGTH_LONG);
                            } catch (Exception e) {
                                StatusMessageHandler.showErrorMessage(recyclerViewFragment.getRecyclerView(),
                                        e);
                            }

                            recyclerViewAdapter.notifyDataSetChanged();
                        }
                    }).setNeutralButton(android.R.string.cancel, null).show();
        }
    });
    recyclerViewActions.setAdapter(recyclerViewAdapter);
    StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(getSpanCount(),
            StaggeredGridLayoutManager.VERTICAL);
    recyclerViewActions.setLayoutManager(layoutManager);

    addActionFAB = (FloatingActionButton) rootView.findViewById(R.id.add_fab);
    addActionFAB.setImageDrawable(IconicsHelper.getAddIcon(getActivity(),
            ContextCompat.getColor(getActivity(), android.R.color.white)));
    addActionFAB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AddSleepAsAndroidAlarmEventActionDialog addAlarmEventActionDialog = AddSleepAsAndroidAlarmEventActionDialog
                    .newInstance(spinnerEventType.getSelectedItemPosition());
            addAlarmEventActionDialog.setTargetFragment(recyclerViewFragment, 0);
            addAlarmEventActionDialog.show(getActivity().getSupportFragmentManager(), null);
        }
    });

    // BroadcastReceiver to get notifications from background service if room data has changed
    broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(this, "received intent: " + intent.getAction());

            if (LocalBroadcastConstants.INTENT_ALARM_EVENT_ACTION_ADDED.equals(intent.getAction())) {
                updateUI();
            } else {
                updateUI();
            }
        }
    };
}

From source file:com.decad3nce.aegis.AegisActivity.java

private void addAdminListener(int toggle, boolean what, Switch who) {
    switch (toggle) {
    case R.id.alarm_toggle:
        if (what) {
            who.setChecked(true);
        }//from   w w w.  jav  a 2 s .c  om
        break;
    case R.id.lock_toggle:
    case R.id.wipe_toggle:
    case R.id.locate_toggle:

        if (mDevicePolicyManager.getActiveAdmins() != null) {
            if (what && mDevicePolicyManager.isAdminActive(DEVICE_ADMIN_COMPONENT)) {
                who.setChecked(true);
            }
        }
        break;

    }

    if (who != null) {
        who.setOnCheckedChangeListener(deviceAdminPreferencesOnChangeListener);
    }
}

From source file:org.ciasaboark.tacere.activity.SettingsActivity.java

private void drawAlarmWidgets() {
    TextView alarmTV = (TextView) findViewById(id.settings_alarmText);
    if (prefs.getIsServiceActivated()) {
        alarmTV.setTextColor(getResources().getColor(R.color.textcolor));
    } else {/*w w  w .ja v a2s  .co  m*/
        alarmTV.setTextColor(getResources().getColor(R.color.textColorDisabled));
    }

    //the alarm volumes toggle
    Switch alarmSwitch = (Switch) findViewById(id.adjustAlarmCheckBox);
    alarmSwitch.setChecked(prefs.getAdjustAlarm());
    alarmSwitch.setEnabled(prefs.getIsServiceActivated());
    findViewById(id.settings_alarmBox).setEnabled(prefs.getIsServiceActivated());

    //the alarm volumes slider
    SeekBar alarmSB = (SeekBar) findViewById(id.alarmSeekBar);
    alarmSB.setMax(VolumesManager.getMaxAlarmVolume());
    alarmSB.setProgress(prefs.getCurAlarmVolume());
    if (prefs.getAdjustAlarm() && prefs.getIsServiceActivated()) {
        this.animateRevealView(alarmSB);
        alarmSB.setEnabled(true);
    } else {
        alarmSB.setEnabled(false);
        this.animateHideView(alarmSB);
    }
    alarmSB.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            prefs.setCurAlarmVolume(progress);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            //required stub
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            //required stub
        }
    });
}