Code example for Switch

Methods: isChecked, setChecked

0
        return true; 
    } 
 
    private void updateHardKeyboardEnabled() { 
        if (mHardKeyboardAvailable) {
            final boolean checked = mHardKeyboardSwitch.isChecked();
            if (mHardKeyboardEnabled != checked) {
                mHardKeyboardEnabled = checked;
                if (mHardKeyboardEnabledChangeListener != null)
                    mHardKeyboardEnabledChangeListener.onHardKeyboardEnabledChange(checked);
            } 
        } 
    } 
 
    public void openPanel() { 
        setVisibility(View.VISIBLE);
        updateUiElements(); 
        if (mInputMethodSwitchButton != null) {
            mInputMethodSwitchButton.setIconImage(R.drawable.ic_sysbar_ime_pressed);
        } 
    } 
 
    public void closePanel(boolean closeKeyboard) {
        setVisibility(View.GONE);
        if (mInputMethodSwitchButton != null) {
            mInputMethodSwitchButton.setIconImage(R.drawable.ic_sysbar_ime);
        } 
        if (closeKeyboard) {
            mImm.hideSoftInputFromWindow(getWindowToken(), 0);
        } 
    } 
 
    private void startActivity(Intent intent) {
        mContext.startActivity(intent);
    } 
 
    private void showConfigureInputMethods() { 
        Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
                | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    } 
 
    private View createInputMethodItem(
            final InputMethodInfo imi, final InputMethodSubtype subtype) {
        final CharSequence subtypeName; 
        if (subtype == null || subtype.overridesImplicitlyEnabledSubtype()) { 
            subtypeName = null; 
        } else { 
            subtypeName = getSubtypeName(imi, subtype); 
        } 
        final CharSequence imiName = getIMIName(imi); 
        final Drawable icon = getSubtypeIcon(imi, subtype); 
        final View view = View.inflate(mContext, R.layout.system_bar_input_methods_item, null); 
        final ImageView subtypeIcon = (ImageView)view.findViewById(R.id.item_icon); 
        final TextView itemTitle = (TextView)view.findViewById(R.id.item_title); 
        final TextView itemSubtitle = (TextView)view.findViewById(R.id.item_subtitle); 
        final ImageView settingsIcon = (ImageView)view.findViewById(R.id.item_settings_icon); 
        final View subtypeView = view.findViewById(R.id.item_subtype); 
        if (subtypeName == null) { 
            itemTitle.setText(imiName); 
            itemSubtitle.setVisibility(View.GONE); 
        } else { 
            itemTitle.setText(subtypeName); 
            itemSubtitle.setVisibility(View.VISIBLE); 
            itemSubtitle.setText(imiName); 
        } 
        subtypeIcon.setImageDrawable(icon); 
        subtypeIcon.setContentDescription(itemTitle.getText()); 
        final String settingsActivity = imi.getSettingsActivity(); 
        if (!TextUtils.isEmpty(settingsActivity)) { 
            settingsIcon.setOnClickListener(new View.OnClickListener() { 
                @Override 
                public void onClick(View arg0) { 
                    Intent intent = new Intent(Intent.ACTION_MAIN); 
                    intent.setClassName(imi.getPackageName(), settingsActivity); 
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
                            | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED 
                            | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
                    startActivity(intent); 
                    closePanel(true); 
                } 
            }); 
        } else { 
            // Do not show the settings icon if the IME does not have a settings preference 
            view.findViewById(R.id.item_vertical_separator).setVisibility(View.GONE); 
            settingsIcon.setVisibility(View.GONE); 
        } 
        mRadioViewAndImiMap.put( 
                subtypeView, new Pair<InputMethodInfo, InputMethodSubtype> (imi, subtype)); 
        subtypeView.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View v) {
                Pair<InputMethodInfo, InputMethodSubtype> imiAndSubtype =
                        updateRadioButtonsByView(v);
                closePanel(false); 
                setInputMethodAndSubtype(imiAndSubtype.first, imiAndSubtype.second);
            } 
        }); 
        return view; 
    } 
 
    private void updateUiElements() { 
        updateHardKeyboardSection(); 
 
        // TODO: Reuse subtype views. 
        mInputMethodMenuList.removeAllViews();
        mRadioViewAndImiMap.clear();
        mPackageManager = mContext.getPackageManager();
 
        Map<InputMethodInfo, List<InputMethodSubtype>> enabledIMIs =
                getEnabledInputMethodAndSubtypeList(); 
        Set<InputMethodInfo> cachedImiSet = enabledIMIs.keySet();
        for (InputMethodInfo imi: cachedImiSet) {
            List<InputMethodSubtype> subtypes = enabledIMIs.get(imi);
            if (subtypes == null || subtypes.size() == 0) {
                mInputMethodMenuList.addView(
                        createInputMethodItem(imi, null));
                continue; 
            } 
            for (InputMethodSubtype subtype: subtypes) {
                mInputMethodMenuList.addView(createInputMethodItem(imi, subtype));
            } 
        } 
        updateRadioButtons(); 
    } 
 
    public void setImeToken(IBinder token) {
        mToken = token;
    } 
 
    public void setImeSwitchButton(InputMethodButton imb) {
        mInputMethodSwitchButton = imb;
    } 
 
    private void setInputMethodAndSubtype(InputMethodInfo imi, InputMethodSubtype subtype) {
        if (mToken != null) {
            mImm.setInputMethodAndSubtype(mToken, imi.getId(), subtype);
        } else { 
            Log.w(TAG, "IME Token is not set yet.");
        } 
    } 
 
    public void setHardKeyboardStatus(boolean available, boolean enabled) {
        if (mHardKeyboardAvailable != available || mHardKeyboardEnabled != enabled) {
            mHardKeyboardAvailable = available;
            mHardKeyboardEnabled = enabled;
            updateHardKeyboardSection(); 
        } 
    } 
 
    private void updateHardKeyboardSection() { 
        if (mHardKeyboardAvailable) {
            mHardKeyboardSection.setVisibility(View.VISIBLE);
            if (mHardKeyboardSwitch.isChecked() != mHardKeyboardEnabled) {
                mHardKeyboardSwitch.setChecked(mHardKeyboardEnabled);
                updateHardKeyboardEnabled(); 
            } 
        } else { 
            mHardKeyboardSection.setVisibility(View.GONE);
        }