Example usage for android.widget TextView setCompoundDrawablesWithIntrinsicBounds

List of usage examples for android.widget TextView setCompoundDrawablesWithIntrinsicBounds

Introduction

In this page you can find the example usage for android.widget TextView setCompoundDrawablesWithIntrinsicBounds.

Prototype

@android.view.RemotableViewMethod
public void setCompoundDrawablesWithIntrinsicBounds(@Nullable Drawable left, @Nullable Drawable top,
        @Nullable Drawable right, @Nullable Drawable bottom) 

Source Link

Document

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

Usage

From source file:dev.drsoran.moloko.util.UIUtils.java

public final static View setDropDownItemIconAndText(View dropDownView, int iconId, String text) {
    final TextView textView = (TextView) dropDownView.findViewById(android.R.id.text1);
    textView.setText(text);/*from  w ww  . j  a v a2s.co m*/

    if (iconId != -1) {
        textView.setCompoundDrawablesWithIntrinsicBounds(iconId, 0, 0, 0);
    } else {
        textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
    }

    return dropDownView;
}

From source file:com.silentcircle.common.util.ViewUtil.java

private static void setDrawableLeft(TextView view, int drawableResourceID) {
    view.setCompoundDrawablesWithIntrinsicBounds(drawableResourceID, 0, 0, 0);
}

From source file:com.afeng.xf.widget.snackbarlight.Light.java

/**
 * Make a customized {@link Snackbar} to display a message without any action.
 *
 * @param view The view to find a parent from.
 * @param text The message to display. Formatted text is supported.
 * @param textIcon The left icon of the message.
 * @param backgroundColor The background color of the Snackbar. It should be a resolved color.
 * @param textColor The color of message text.
 * @param duration How long to show the message.
 *                 Either {@link Light#LENGTH_SHORT} or {@link Light#LENGTH_LONG}.
 *
 * @return The customized Snackbar that will be displayed.
 *//*from  ww w .java  2  s  .com*/
public static Snackbar make(@NonNull View view, @NonNull CharSequence text, Drawable textIcon,
        @ColorInt int backgroundColor, @ColorInt int textColor, int duration) {
    // Get a usual Snackbar
    Snackbar snackbar = Snackbar.make(view, text, duration);

    // Get the view of it.
    View mView = snackbar.getView();
    // Change the background color.
    mView.setBackgroundColor(backgroundColor);

    // Get the TextView of message.
    TextView textView = (TextView) mView.findViewById(android.support.design.R.id.snackbar_text);
    // Set the left icon of message.
    textView.setCompoundDrawablesWithIntrinsicBounds(textIcon, null, null, null);
    // Set the padding between message and icon.
    textView.setCompoundDrawablePadding(16);
    // To make icon and message aligned.
    textView.setGravity(Gravity.CENTER);
    // Change color of message text.
    textView.setTextColor(textColor);

    return snackbar;
}

From source file:Main.java

/**
 * Make a textview to a collapsible textview with the indicator on the right of the textview
 * //from  w w  w . j a va 2s .c o m
 * @param tv , {@link TextView} to be converted
 * @param upDrawableResId , drawable resource id to be used as up indicator
 * @param downDrawableResId , drawable resource id to be used as down indicator
 * @param lineTreshold , no of line to be displayed for the collapsed state
 */
public static void makeCollapsible(final TextView tv, int upDrawableResId, int downDrawableResId,
        final int lineTreshold) {

    final Drawable[] drawables = tv.getCompoundDrawables();
    final Drawable up = tv.getContext().getResources().getDrawable(upDrawableResId);
    final Drawable down = tv.getContext().getResources().getDrawable(downDrawableResId);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], down, drawables[3]);
        tv.setEllipsize(TruncateAt.END);
        tv.setMaxLines(lineTreshold);
        tv.setTag(true);
        tv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (v instanceof TextView) {
                    TextView tv = (TextView) v;
                    boolean snippet = (Boolean) tv.getTag();
                    if (snippet) {
                        // show everything
                        snippet = false;
                        tv.setMaxLines(Integer.MAX_VALUE);
                        tv.setEllipsize(null);
                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], up,
                                drawables[3]);
                    } else {
                        // show snippet
                        snippet = true;
                        tv.setMaxLines(lineTreshold);
                        tv.setEllipsize(TruncateAt.END);
                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], down,
                                drawables[3]);
                    }
                    tv.setTag(snippet);
                }

            }
        });
    } else {
        tv.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {

                ViewTreeObserver vto = tv.getViewTreeObserver();
                vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

                    @SuppressWarnings("deprecation")
                    @SuppressLint("NewApi")
                    @Override
                    public void onGlobalLayout() {

                        tv.setEllipsize(TruncateAt.END);
                        int line = tv.getLineCount();
                        tv.setMaxLines(lineTreshold);
                        if (line <= lineTreshold) {
                            tv.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View arg0) {
                                    // empty listener
                                    // Log.d("line count", "count: "+
                                    // tv.getLineCount());
                                }
                            });
                            if (tv.getLayout() != null) {
                                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                                    tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                                } else {
                                    tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                                }
                            }
                            return;
                        }

                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], down,
                                drawables[3]);
                        tv.setTag(true);
                        tv.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                if (v instanceof TextView) {
                                    TextView tv = (TextView) v;
                                    boolean snippet = (Boolean) tv.getTag();
                                    if (snippet) {
                                        snippet = false;
                                        // show everything
                                        tv.setMaxLines(Integer.MAX_VALUE);
                                        tv.setEllipsize(null);
                                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1],
                                                up, drawables[3]);
                                    } else {
                                        snippet = true;
                                        // show snippet
                                        tv.setMaxLines(lineTreshold);
                                        tv.setEllipsize(TruncateAt.END);
                                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1],
                                                down, drawables[3]);
                                    }
                                    tv.setTag(snippet);
                                }

                            }
                        });

                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                            tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        } else {
                            tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        }
                    }

                });
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

        });
    }

}

From source file:nl.sebastiaanschool.contact.app.gui.GrabBag.java

/**
 * Apply a vector drawable, using backwards compatibility as needed. This throws an exception
 * if your {@code resId} is actually a bitmap drawable.
 *///from w w  w .  jav a2  s  . c o  m
public static void applyVectorDrawableLeft(TextView view, @DrawableRes int resId) {
    if (Build.VERSION.SDK_INT >= 21) {
        view.setCompoundDrawablesRelativeWithIntrinsicBounds(resId, 0, 0, 0);
        view.getCompoundDrawablesRelative()[0].setTint(ResourcesCompat.getColor(view.getResources(),
                R.color.sebastiaan_blue, view.getContext().getTheme()));
    } else {
        final Context context = view.getContext();
        final Drawable drawable = loadVectorDrawable(context, resId);
        if (Build.VERSION.SDK_INT >= 17) {
            view.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null, null);
        } else {
            view.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
        }
    }
}

From source file:ca.rmen.android.scrumchatter.chart.ChartUtils.java

static void addLegendEntry(Context context, ViewGroup legendView, String name, int color) {
    TextView memberLegendEntry = new TextView(context);
    memberLegendEntry.setText(name);//  www.j  ava  2 s  .  co  m
    memberLegendEntry.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    memberLegendEntry.setPadding(0, 0,
            context.getResources().getDimensionPixelSize(R.dimen.chart_legend_entry_padding), 0);

    final Drawable icon;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        icon = ContextCompat.getDrawable(context, R.drawable.ic_legend_square);
        memberLegendEntry.setTextColor(color);
    } else {
        icon = ContextCompat.getDrawable(context, R.drawable.ic_legend_square).mutate();
        DrawableCompat.setTint(icon, color);
    }
    memberLegendEntry.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
    legendView.addView(memberLegendEntry);
}

From source file:com.fjoglar.etsitnoticias.utils.UiUtils.java

/**
 * Configure the TextViews that will show the attachments of the new.
 *
 * @param textView      TextView to be configured.
 * @param title         Text shown in TextView
 * @param downloadLink  Link attached to TextView.
 * @param fileType      Type of file of the attachment.
 * @param context       The context of activity.
 *///  w ww.  ja v  a2  s  . c  om
public static void configureTextView(TextView textView, String title, final String downloadLink,
        Attachment.FILE_TYPE fileType, final Context context) {

    final int TEXT_VIEW_MIN_HEIGHT = 40;
    final int TEXT_VIEW_MARGIN_TOP = 4;

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    params.setMargins(0, convertDpToPx(TEXT_VIEW_MARGIN_TOP, context), 0, 0);
    textView.setLayoutParams(params);

    textView.setText(title);
    textView.setTypeface(Typeface.create("sans-serif-condensed", Typeface.NORMAL));
    textView.setMinHeight(convertDpToPx(TEXT_VIEW_MIN_HEIGHT, context));
    textView.setGravity(Gravity.CENTER_VERTICAL);

    switch (fileType) {
    case FILE:
        textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_file, 0, 0, 0);
        break;
    case IMAGE:
        textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_photo, 0, 0, 0);
        break;
    case FOLDER:
        textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_folder, 0, 0, 0);
        break;
    default:
        textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_link, 0, 0, 0);
        break;
    }

    textView.setCompoundDrawablePadding(convertDpToPx(4, context));

    TypedValue typedValue = new TypedValue();
    context.getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);
    textView.setBackgroundResource(typedValue.resourceId);

    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Navigator.getInstance().openUrl(context, downloadLink);
        }
    });
}

From source file:com.hybris.mobile.lib.ui.view.Alert.java

/**
 * Set the icon on the alert/*w ww .jav a  2  s.  c  o m*/
 *
 * @param context       application-specific resources
 * @param configuration describes all device configuration information
 * @param mainView      ViewGroup resources
 * @param textView      alert message to be viewed message to be displayedView
 */
@SuppressLint("NewApi")
private static void setIcon(Activity context, Configuration configuration, ViewGroup mainView,
        TextView textView) {

    ImageView imageView = (ImageView) mainView.findViewById(R.id.alert_view_icon);

    // Reset the current icon
    if (imageView != null) {
        mainView.removeView(imageView);
    }

    // On the textview as well
    textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
    textView.setCompoundDrawablePadding(0);

    if (configuration.getIconResId() != -1) {

        imageView = new ImageView(context);
        imageView.setId(R.id.alert_view_icon);

        imageView.setImageResource(configuration.getIconResId());
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        imageView.setLayoutParams(layoutParams);

        switch (configuration.getIconPosition()) {
        case LEFT_TEXT:
            textView.setCompoundDrawablesWithIntrinsicBounds(configuration.getIconResId(), 0, 0, 0);
            textView.setCompoundDrawablePadding(10);
            break;

        case RIGHT_TEXT:
            textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, configuration.getIconResId(), 0);
            textView.setCompoundDrawablePadding(10);
            break;

        case LEFT:
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
            }

            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            layoutParams.setMargins(25, 0, 0, 0);
            mainView.addView(imageView);

            // We redefine the layout params otherwise the image is not well aligned
            textView.setLayoutParams(
                    new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            break;
        case RIGHT:
        default:
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
            }

            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            layoutParams.setMargins(0, 0, 25, 0);
            mainView.addView(imageView);

            // We redefine the layout params otherwise the image is not well aligned
            textView.setLayoutParams(
                    new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            break;
        }
    }
}

From source file:com.example.awesomedogs.ui.DogDetailActivity.java

private void showDetails() {
    setText(R.id.name, mDog.getName());// w  w w . ja va 2 s .  co  m
    setText(R.id.breed, mDog.getBreed());
    setText(R.id.age, String.format(getString(R.string.age_template), mDog.getAge()));

    String description = mDog.getDescription(this);
    if (description.isEmpty()) { // Uh oh, this API is only supported on Android 2.2+
        description = getString(R.string.no_description);
    } else {
        description += " " + getString(R.string.lorem_ipsum);
    }
    setText(R.id.description, description);

    Country country = mDog.getCountry();
    TextView tv = (TextView) findViewById(R.id.country);
    tv.setText(country.getName());
    tv.setCompoundDrawablesWithIntrinsicBounds(country.getIcon(), 0, 0, 0);

    ((ImageView) findViewById(R.id.photo)).setImageResource(mDog.getPhoto());
}

From source file:com.example.awesomedogs.ui.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);/* w w w  .j  a v a2  s . c  o m*/

    ArrayAdapter<Dog> adapter = new ArrayAdapter<Dog>(this, R.layout.list_row, DoggyData.getDogs()) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.list_row, null, false);
            }

            Dog dog = getItem(position);

            TextView textView = (TextView) convertView;
            textView.setText(getItem(position).getName());
            textView.setCompoundDrawablesWithIntrinsicBounds(dog.getIcon(), 0, 0, 0);

            return textView;
        }
    };
    setListAdapter(adapter);
    getListView().setOnItemClickListener(this);
}