Example usage for android.widget TextView setCompoundDrawables

List of usage examples for android.widget TextView setCompoundDrawables

Introduction

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

Prototype

public void setCompoundDrawables(@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:Main.java

@SuppressWarnings("deprecation")
public static void setTvLeftPic(Context context, int resId, TextView tv) {
    Drawable drawable = context.getResources().getDrawable(resId);
    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
    tv.setCompoundDrawables(drawable, null, null, null);
}

From source file:Main.java

public static void textViewSetImg(Context context, TextView tv, int resId) {
    Drawable drawable = context.getResources().getDrawable(resId);
    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
    tv.setCompoundDrawables(drawable, null, null, null);
}

From source file:Main.java

/**
 * @see android.widget.TextView#setCompoundDrawablesRelative(Drawable, Drawable, Drawable,
 *      Drawable)//  ww w .  j a  v  a 2s. c om
 */
public static void setCompoundDrawablesRelative(TextView textView, Drawable start, Drawable top, Drawable end,
        Drawable bottom) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        textView.setCompoundDrawablesRelative(start, top, bottom, end);
    } else {
        textView.setCompoundDrawables(start, top, bottom, end);
    }
}

From source file:Main.java

/**
 * Fixes the top and bottom compound drawables
 *
 * @param tv//from   w  ww  . j  a va  2s  . com
 */
public static void vertical(TextView tv) {
    Drawable[] drawables = tv.getCompoundDrawables();
    Drawable top = drawables[TOP];
    Drawable bottom = drawables[BOTTOM];
    if ((top != null && bottom != null) || (top == null && bottom == null)) {
        return;
    }
    if (top == null) {
        top = createPlaceholder(bottom);
    }
    if (bottom == null) {
        bottom = createPlaceholder(top);
    }
    tv.setCompoundDrawables(drawables[LEFT], top, drawables[RIGHT], bottom);
}

From source file:Main.java

/**
 * @see android.widget.TextView#setCompoundDrawablesRelative(Drawable, Drawable, Drawable,
 *      Drawable)/*from  w w w .j av a2s  .c  o  m*/
 */
public static void setCompoundDrawablesRelative(TextView textView, Drawable start, Drawable top, Drawable end,
        Drawable bottom) {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
        // On JB MR1, due to a platform bug, setCompoundDrawablesRelative() is a no-op if the
        // view has ever been measured. As a workaround, use setCompoundDrawables() directly.
        // See: http://crbug.com/368196 and http://crbug.com/361709
        boolean isRtl = isLayoutRtl(textView);
        textView.setCompoundDrawables(isRtl ? end : start, top, isRtl ? start : end, bottom);
    } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
        textView.setCompoundDrawablesRelative(start, top, end, bottom);
    } else {
        textView.setCompoundDrawables(start, top, end, bottom);
    }
}

From source file:Main.java

/**
 * Fixes the left and right compound drawables
 *
 * @param tv/*  ww  w .  j a va  2s  .c  o m*/
 */
public static void horizontal(TextView tv) {
    Drawable[] drawables = tv.getCompoundDrawables();
    Drawable left = drawables[LEFT];
    Drawable right = drawables[RIGHT];
    if ((left != null && right != null) || (left == null && right == null)) {
        return;
    }
    if (right == null) {
        right = createPlaceholder(left);

        // create a placeholder drawable of the same size on the right
    }
    if (left == null) {
        left = createPlaceholder(right);
    }
    tv.setCompoundDrawables(left, drawables[TOP], right, drawables[BOTTOM]);
}

From source file:com.stepstone.stepper.internal.util.TintUtil.java

/**
 * Tints TextView's text color and it's compound drawables
 * @param textview text view to tint//from   w ww  .j a v  a2s  . c om
 * @param tintColor color state list to use for tinting
 */
public static void tintTextView(@NonNull TextView textview, ColorStateList tintColor) {
    textview.setTextColor(tintColor);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Drawable[] drawables = textview.getCompoundDrawablesRelative();
        textview.setCompoundDrawablesRelative(tintDrawable(drawables[0], tintColor),
                tintDrawable(drawables[1], tintColor), tintDrawable(drawables[2], tintColor),
                tintDrawable(drawables[3], tintColor));
    } else {
        Drawable[] drawables = textview.getCompoundDrawables();
        textview.setCompoundDrawables(tintDrawable(drawables[0], tintColor),
                tintDrawable(drawables[1], tintColor), tintDrawable(drawables[2], tintColor),
                tintDrawable(drawables[3], tintColor));
    }
}

From source file:meizhi.meizhi.malin.utils.DestroyCleanUtil.java

@SuppressLint("ObsoleteSdkInt")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void unBindView(View view) {
    if (view == null)
        return;//from w  w w.j a v a2s.  co  m
    Drawable drawable;
    int i;
    //1.
    try {
        view.setOnClickListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //2.
    try {
        view.setOnCreateContextMenuListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //3.
    try {
        view.setOnFocusChangeListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //4.
    try {
        view.setOnKeyListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //5.
    try {
        view.setOnLongClickListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //6.
    try {
        view.setOnTouchListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //7.
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            view.setOnApplyWindowInsetsListener(null);
        }
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //8.
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            view.setOnContextClickListener(null);
        }
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //9.
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            view.setOnScrollChangeListener(null);
        }
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //10.
    try {
        view.setOnDragListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //11.
    try {
        view.setOnGenericMotionListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //12.
    try {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR2) {//13
            view.setOnHoverListener(null);
        }
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //13.
    try {
        view.setOnSystemUiVisibilityChangeListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    /**
     * @see SwipeRefreshLayout#onDetachedFromWindow()
     */
    if (view.getBackground() != null && !view.getClass().getName().equals(CIRCLE_CLASS)) {
        try {
            view.getBackground().setCallback(null);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {//16
                view.setBackgroundDrawable(null);
            } else {
                view.setBackground(null);
            }
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //ImageView
    if (view instanceof ImageView) {
        try {
            ImageView imageView = (ImageView) view;
            drawable = imageView.getDrawable();
            if (drawable != null) {
                drawable.setCallback(null);
            }
            imageView.setImageDrawable(null);
            imageView.setImageBitmap(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //TextView
    if (view instanceof TextView) {
        try {
            TextView textView = (TextView) view;
            textView.setCompoundDrawables(null, null, null, null);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                textView.setCompoundDrawablesRelative(null, null, null, null);
            }
            textView.setCursorVisible(false);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //ImageButton
    if (view instanceof ImageButton) {
        try {
            ImageButton imageButton = (ImageButton) view;
            drawable = imageButton.getDrawable();
            if (drawable != null) {
                drawable.setCallback(null);
            }
            imageButton.setImageDrawable(null);
            imageButton.setImageBitmap(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //ListView
    if (view instanceof ListView) {
        ListView listView = (ListView) view;

        try {
            listView.setAdapter(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }

        try {
            listView.setOnScrollListener(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }

        try {
            listView.setOnItemClickListener(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }

        try {
            listView.setOnItemLongClickListener(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }

        try {
            listView.setOnItemSelectedListener(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //RecyclerView
    if (view instanceof RecyclerView) {
        try {
            RecyclerView recyclerView = (RecyclerView) view;
            recyclerView.setAdapter(null);
            recyclerView.setChildDrawingOrderCallback(null);
            recyclerView.setOnScrollListener(null);
            recyclerView.addOnScrollListener(null);
            recyclerView.removeOnScrollListener(null);
            recyclerView.setRecyclerListener(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //WebView
    if (view instanceof WebView) {

        WebView webView = (WebView) view;
        try {
            webView.stopLoading();
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

        try {
            webView.removeAllViews();
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

        try {
            webView.setWebChromeClient(null);
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

        try {
            webView.setWebViewClient(null);
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

        try {
            webView.destroy();
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

        try {
            if (null != view.getParent() && view.getParent() instanceof ViewGroup) {
                ((ViewGroup) view.getParent()).removeView(view);
            }
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

    }

    //SurfaceView
    if (view instanceof SurfaceView) {
        try {
            SurfaceView surfaceView = (SurfaceView) view;
            SurfaceHolder holder = surfaceView.getHolder();
            if (holder != null) {
                Surface surface = holder.getSurface();
                if (surface != null) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                        surface.release();
                    }
                }
            }
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }
    }

    view.destroyDrawingCache();
    view.clearAnimation();

    if (view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) view;
        int childCount = (viewGroup).getChildCount();
        for (i = 0; i < childCount; i++) {
            unBindView((viewGroup).getChildAt(i));
        }
    }
}

From source file:de.grobox.liberario.fragments.HomePickerDialogFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    View v = inflater.inflate(R.layout.fragment_set_home, container);

    // Adapt Title Icon
    final TextView title = (TextView) v.findViewById(R.id.homeTitleView);
    title.setCompoundDrawables(TransportrUtils.getTintedDrawable(getContext(), title.getCompoundDrawables()[0]),
            null, null, null);//  w  w w  .j  a v  a2s.co m

    // Initialize LocationView
    final LocationView loc = (LocationView) v.findViewById(R.id.location_input);
    loc.initialize(getActivity());

    // OK Button
    Button okButton = (Button) v.findViewById(R.id.okButton);
    okButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (loc.getLocation() != null) {
                // save home location in file
                RecentsDB.setHome(v.getContext(), loc.getLocation());

                // call listener if set
                if (listener != null)
                    listener.onHomeChanged(loc.getLocation());

                getDialog().cancel();
            } else {
                Toast.makeText(v.getContext(),
                        getResources().getString(R.string.error_only_autocomplete_station), Toast.LENGTH_SHORT)
                        .show();
            }
        }
    });

    // Cancel Button
    Button cancelButton = (Button) v.findViewById(R.id.cancelButton);
    cancelButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            getDialog().cancel();
        }
    });
    return v;
}

From source file:com.example.android.supportv4.app.SharingReceiverSupport.java

@Override
protected void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.sharing_receiver_support);

    final float density = getResources().getDisplayMetrics().density;
    final int iconSize = (int) (ICON_SIZE * density + 0.5f);

    ShareCompat.IntentReader intentReader = ShareCompat.IntentReader.from(this);

    // The following provides attribution for the app that shared the data with us.
    TextView info = (TextView) findViewById(R.id.app_info);
    Drawable d = intentReader.getCallingActivityIcon();
    d.setBounds(0, 0, iconSize, iconSize);
    info.setCompoundDrawables(d, null, null, null);
    info.setText(intentReader.getCallingApplicationLabel());

    TextView tv = (TextView) findViewById(R.id.text);
    StringBuilder txt = new StringBuilder("Received share!\nText was: ");

    txt.append(intentReader.getText());/*from  w  ww .  j  ava2s.c  o m*/
    txt.append("\n");

    txt.append("Streams included:\n");
    final int N = intentReader.getStreamCount();
    for (int i = 0; i < N; i++) {
        Uri uri = intentReader.getStream(i);
        txt.append("Share included stream " + i + ": " + uri + "\n");
        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(getContentResolver().openInputStream(uri)));
            try {
                txt.append(reader.readLine() + "\n");
            } catch (IOException e) {
                Log.e(TAG, "Reading stream threw exception", e);
            } finally {
                reader.close();
            }
        } catch (FileNotFoundException e) {
            Log.e(TAG, "File not found from share.", e);
        } catch (IOException e) {
            Log.d(TAG, "I/O Error", e);
        }
    }

    tv.setText(txt.toString());
}