Example usage for android.support.v4.widget TextViewCompat setCompoundDrawablesRelative

List of usage examples for android.support.v4.widget TextViewCompat setCompoundDrawablesRelative

Introduction

In this page you can find the example usage for android.support.v4.widget TextViewCompat setCompoundDrawablesRelative.

Prototype

public static void setCompoundDrawablesRelative(@NonNull TextView textView, @Nullable Drawable start,
        @Nullable Drawable top, @Nullable Drawable end, @Nullable Drawable bottom) 

Source Link

Document

Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text.

Usage

From source file:com.alainesp.fan.sanderson.MainActivity.java

/**
 * Set the badge number to all menu items visible
 *///from  w ww . ja v  a 2  s  .c  om
private static void setBadgesNumberUI() {
    if (navigationView != null) {
        NavigationMenuView v = (NavigationMenuView) navigationView.getChildAt(0);
        Menu drawerMenu = navigationView.getMenu();

        if (v != null)
            // Iterate all children
            for (int childIndex = 0; childIndex < v.getChildCount(); childIndex++) {
                View v1 = v.getChildAt(childIndex);
                if (v1 instanceof NavigationMenuItemView) {
                    TextView mTextView = (TextView) ((NavigationMenuItemView) v1).getChildAt(0);
                    if (mTextView != null) {
                        // Get the menu index
                        Integer menuIndex = reverseMenuText.get(mTextView.getText().toString());
                        if (menuIndex != null && menuIndex < badgeNumbers.length) {
                            Drawable numberText = null;
                            if (badgeNumbers[menuIndex] > 0) {
                                int height = mTextView.getHeight();
                                numberText = new TextDrawable(badgeNumbers[menuIndex], mTextView.getTextSize(),
                                        mTextView.getCurrentTextColor(), height);
                                numberText.setBounds(0, 0, height, height);
                            }

                            // Similar to NavigationMenuItemView.setIcon
                            Drawable icon = drawerMenu.getItem(menuIndex).getIcon();
                            Drawable.ConstantState state = icon.getConstantState();
                            icon = DrawableCompat.wrap(state == null ? icon : state.newDrawable()).mutate();
                            int mIconSize = navigationView.getContext().getResources().getDimensionPixelSize(
                                    android.support.design.R.dimen.design_navigation_icon_size);
                            icon.setBounds(0, 0, mIconSize, mIconSize);
                            DrawableCompat.setTintList(icon, navigationView.getItemIconTintList());
                            TextViewCompat.setCompoundDrawablesRelative(mTextView, icon, null, numberText,
                                    null);
                        }
                    }
                }
            }
    }
}

From source file:android.support.design.internal.NavigationMenuItemView.java

@Override
public void setIcon(Drawable icon) {
    if (icon != null) {
        if (mHasIconTintList) {
            Drawable.ConstantState state = icon.getConstantState();
            icon = DrawableCompat.wrap(state == null ? icon : state.newDrawable()).mutate();
            DrawableCompat.setTintList(icon, mIconTintList);
        }//from w  w w  .ja  v  a 2s  .c om
        icon.setBounds(0, 0, mIconSize, mIconSize);
    } else if (mNeedsEmptyIcon) {
        if (mEmptyDrawable == null) {
            mEmptyDrawable = ResourcesCompat.getDrawable(getResources(), R.drawable.navigation_empty_icon,
                    getContext().getTheme());
            if (mEmptyDrawable != null) {
                mEmptyDrawable.setBounds(0, 0, mIconSize, mIconSize);
            }
        }
        icon = mEmptyDrawable;
    }
    TextViewCompat.setCompoundDrawablesRelative(mTextView, icon, null, null, null);
}

From source file:org.buffer.android.buffertextinputlayout.BufferTextInputLayout.java

private void updatePasswordToggleView() {
    if (editText == null) {
        // If there is no EditText, there is nothing to update
        return;//from  ww w .  j  av  a 2 s . c  o  m
    }
    if (shouldShowPasswordIcon()) {
        if (passwordToggleView == null) {
            passwordToggleView = (CheckableImageButton) LayoutInflater.from(getContext())
                    .inflate(R.layout.design_text_input_password_icon, inputFrame, false);
            passwordToggleView.setImageDrawable(passwordToggleDrawable);
            passwordToggleView.setContentDescription(passwordToggleContentDesc);
            inputFrame.addView(passwordToggleView);
            passwordToggleView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    passwordVisibilityToggleRequested();
                }
            });
        }
        passwordToggleView.setVisibility(VISIBLE);
        // We need to add a dummy drawable as the end compound drawable so that the text is
        // indented and doesn't display below the toggle view
        if (passwordToggleDummyDrawable == null) {
            passwordToggleDummyDrawable = new ColorDrawable();
        }
        passwordToggleDummyDrawable.setBounds(0, 0, passwordToggleView.getMeasuredWidth(), 1);
        final Drawable[] compounds = TextViewCompat.getCompoundDrawablesRelative(editText);
        // Store the user defined end compound drawable so that we can restore it later
        if (compounds[2] != passwordToggleDummyDrawable) {
            originalEditTextEndDrawable = compounds[2];
        }
        TextViewCompat.setCompoundDrawablesRelative(editText, compounds[0], compounds[1],
                passwordToggleDummyDrawable, compounds[3]);
        // Copy over the EditText's padding so that we match
        passwordToggleView.setPadding(editText.getPaddingLeft(), editText.getPaddingTop(),
                editText.getPaddingRight(), editText.getPaddingBottom());
    } else {
        if (passwordToggleView != null && passwordToggleView.getVisibility() == VISIBLE) {
            passwordToggleView.setVisibility(View.GONE);
        }
        // Make sure that we remove the dummy end compound drawable
        final Drawable[] compounds = TextViewCompat.getCompoundDrawablesRelative(editText);
        if (compounds[2] == passwordToggleDummyDrawable) {
            TextViewCompat.setCompoundDrawablesRelative(editText, compounds[0], compounds[1],
                    originalEditTextEndDrawable, compounds[3]);
        }
    }
}

From source file:android.support.design.widget.TextInputLayout.java

private void updatePasswordToggleView() {
    if (mEditText == null) {
        // If there is no EditText, there is nothing to update
        return;/* ww w. j a  v a 2  s.c  o m*/
    }

    if (shouldShowPasswordIcon()) {
        if (mPasswordToggleView == null) {
            mPasswordToggleView = (CheckableImageButton) LayoutInflater.from(getContext())
                    .inflate(R.layout.design_text_input_password_icon, mInputFrame, false);
            mPasswordToggleView.setImageDrawable(mPasswordToggleDrawable);
            mPasswordToggleView.setContentDescription(mPasswordToggleContentDesc);
            mInputFrame.addView(mPasswordToggleView);

            mPasswordToggleView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    passwordVisibilityToggleRequested();
                }
            });
        }

        if (mEditText != null && ViewCompat.getMinimumHeight(mEditText) <= 0) {
            // We should make sure that the EditText has the same min-height as the password
            // toggle view. This ensure focus works properly, and there is no visual jump
            // if the password toggle is enabled/disabled.
            mEditText.setMinimumHeight(ViewCompat.getMinimumHeight(mPasswordToggleView));
        }

        mPasswordToggleView.setVisibility(VISIBLE);
        mPasswordToggleView.setChecked(mPasswordToggledVisible);

        // We need to add a dummy drawable as the end compound drawable so that the text is
        // indented and doesn't display below the toggle view
        if (mPasswordToggleDummyDrawable == null) {
            mPasswordToggleDummyDrawable = new ColorDrawable();
        }
        mPasswordToggleDummyDrawable.setBounds(0, 0, mPasswordToggleView.getMeasuredWidth(), 1);

        final Drawable[] compounds = TextViewCompat.getCompoundDrawablesRelative(mEditText);
        // Store the user defined end compound drawable so that we can restore it later
        if (compounds[2] != mPasswordToggleDummyDrawable) {
            mOriginalEditTextEndDrawable = compounds[2];
        }
        TextViewCompat.setCompoundDrawablesRelative(mEditText, compounds[0], compounds[1],
                mPasswordToggleDummyDrawable, compounds[3]);

        // Copy over the EditText's padding so that we match
        mPasswordToggleView.setPadding(mEditText.getPaddingLeft(), mEditText.getPaddingTop(),
                mEditText.getPaddingRight(), mEditText.getPaddingBottom());
    } else {
        if (mPasswordToggleView != null && mPasswordToggleView.getVisibility() == VISIBLE) {
            mPasswordToggleView.setVisibility(View.GONE);
        }

        if (mPasswordToggleDummyDrawable != null) {
            // Make sure that we remove the dummy end compound drawable if it exists, and then
            // clear it
            final Drawable[] compounds = TextViewCompat.getCompoundDrawablesRelative(mEditText);
            if (compounds[2] == mPasswordToggleDummyDrawable) {
                TextViewCompat.setCompoundDrawablesRelative(mEditText, compounds[0], compounds[1],
                        mOriginalEditTextEndDrawable, compounds[3]);
                mPasswordToggleDummyDrawable = null;
            }
        }
    }
}

From source file:com.commonsware.cwac.crossport.design.widget.TextInputLayout.java

private void updatePasswordToggleView() {
    if (mEditText == null) {
        // If there is no EditText, there is nothing to update
        return;/* w w w .  j  a v  a  2s.c  om*/
    }

    if (shouldShowPasswordIcon()) {
        if (mPasswordToggleView == null) {
            mPasswordToggleView = (CheckableImageButton) LayoutInflater.from(getContext())
                    .inflate(R.layout.design_text_input_password_icon, mInputFrame, false);
            mPasswordToggleView.setImageDrawable(mPasswordToggleDrawable);
            mPasswordToggleView.setContentDescription(mPasswordToggleContentDesc);
            mInputFrame.addView(mPasswordToggleView);

            mPasswordToggleView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    passwordVisibilityToggleRequested(false);
                }
            });
        }

        if (mEditText != null && ViewCompat.getMinimumHeight(mEditText) <= 0) {
            // We should make sure that the EditText has the same min-height as the password
            // toggle view. This ensure focus works properly, and there is no visual jump
            // if the password toggle is enabled/disabled.
            mEditText.setMinimumHeight(ViewCompat.getMinimumHeight(mPasswordToggleView));
        }

        mPasswordToggleView.setVisibility(VISIBLE);
        mPasswordToggleView.setChecked(mPasswordToggledVisible);

        // We need to add a dummy drawable as the end compound drawable so that the text is
        // indented and doesn't display below the toggle view
        if (mPasswordToggleDummyDrawable == null) {
            mPasswordToggleDummyDrawable = new ColorDrawable();
        }
        mPasswordToggleDummyDrawable.setBounds(0, 0, mPasswordToggleView.getMeasuredWidth(), 1);

        final Drawable[] compounds = TextViewCompat.getCompoundDrawablesRelative(mEditText);
        // Store the user defined end compound drawable so that we can restore it later
        if (compounds[2] != mPasswordToggleDummyDrawable) {
            mOriginalEditTextEndDrawable = compounds[2];
        }
        TextViewCompat.setCompoundDrawablesRelative(mEditText, compounds[0], compounds[1],
                mPasswordToggleDummyDrawable, compounds[3]);

        // Copy over the EditText's padding so that we match
        mPasswordToggleView.setPadding(mEditText.getPaddingLeft(), mEditText.getPaddingTop(),
                mEditText.getPaddingRight(), mEditText.getPaddingBottom());
    } else {
        if (mPasswordToggleView != null && mPasswordToggleView.getVisibility() == VISIBLE) {
            mPasswordToggleView.setVisibility(View.GONE);
        }

        if (mPasswordToggleDummyDrawable != null) {
            // Make sure that we remove the dummy end compound drawable if it exists, and then
            // clear it
            final Drawable[] compounds = TextViewCompat.getCompoundDrawablesRelative(mEditText);
            if (compounds[2] == mPasswordToggleDummyDrawable) {
                TextViewCompat.setCompoundDrawablesRelative(mEditText, compounds[0], compounds[1],
                        mOriginalEditTextEndDrawable, compounds[3]);
                mPasswordToggleDummyDrawable = null;
            }
        }
    }
}