Example usage for android.widget TextView setBackgroundResource

List of usage examples for android.widget TextView setBackgroundResource

Introduction

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

Prototype

@RemotableViewMethod
public void setBackgroundResource(@DrawableRes int resid) 

Source Link

Document

Set the background to a given resource.

Usage

From source file:com.vgaw.androidtest.view.SlidingTabStrip.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}.//from   w ww . j a v a  2  s.c o m
 */
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT, 1f);
    textView.setLayoutParams(params);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    return textView;
}

From source file:com.me.resume.MainActivity.java

/**
 * /*from   www .  j  ava  2  s . c o  m*/
 * @param tag
 */
private void fillTagFlowView(String tag) {
    String[] tags = tag.split(";");
    List<String> ll = Arrays.asList(tags);
    if (ll != null && ll.size() > 0) {
        MarginLayoutParams lp = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.leftMargin = 5;
        lp.rightMargin = 5;
        lp.topMargin = 5;
        lp.bottomMargin = 5;
        for (int i = 0, cun = ll.size(); i < cun; i++) {
            TextView tview = new TextView(this);
            tview.setText(ll.get(i).toString().trim());
            tview.setTextSize(CommUtil.px2sp(self, CommUtil.getFloatValue(self, R.dimen.main_tiny_text)));
            tview.setTextColor(Color.parseColor(getRanColor().get(new Random().nextInt(10))));
            tview.setTypeface(Typeface.SERIF);
            tview.setBackgroundResource(R.drawable.home_tag_text_corner);
            tagFlowLayout.addView(tview, lp);
        }
    }
}

From source file:com.jei.widgettest.view.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}./*  w  ww .  j av a2 s .c o m*/
 */
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);

    // Weight ?? .
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    textView.setWidth(size.x / mViewPager.getAdapter().getCount());

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    return textView;
}

From source file:com.dtd.thevyshka.Utils.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}./*from  www.j  av a  2s  . c  om*/
 */
@SuppressLint("NewApi")
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    textView.setWidth(size.x / mTabCount);

    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(12);//TODO adapting to all
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setTextColor(ContextCompat.getColor(context, R.color.white));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding / 2, padding, padding / 2, padding);

    return textView;
}

From source file:com.guerinet.materialtabs.TabLayout.java

/**
 * Creates a default view to be used for tabs. This is called if a custom tab view is not set
 * via {@link #setCustomTabView(int, int)}.
 *
 * @return The default view to use//from w  w w  .  j  av a  2 s  .  com
 */
protected TextView createDefaultTabView() {
    TextView textView = new TextView(getContext());
    prepareTextView(textView);
    textView.setGravity(Gravity.CENTER);
    textView.setEllipsize(TextUtils.TruncateAt.END);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    //Set the text color if there is one
    if (this.mDefaultTextColorId != null) {
        textView.setTextColor(getResources().getColor(mDefaultTextColorId));
    }
    textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    textView.setBackgroundResource(getTabBackground());

    //Padding
    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    return textView;
}

From source file:org.onebusaway.android.ui.ArrivalsListHeader.java

/**
 * Sets the popup for the status//from  w  w  w  . j  ava  2 s  .  c  o  m
 *
 * @param index      0 if this is for the top ETA row, 1 if it is for the second
 * @param color      color resource id to use for the popup background
 * @param statusText text to show in the status popup
 * @return a new PopupWindow initialized based on the provided parameters
 */
private PopupWindow setupPopup(final int index, int color, String statusText) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    TextView statusView = (TextView) inflater.inflate(R.layout.arrivals_list_tv_template_style_b_status_large,
            null);
    statusView.setBackgroundResource(R.drawable.round_corners_style_b_status);
    GradientDrawable d = (GradientDrawable) statusView.getBackground();
    if (color != R.color.stop_info_ontime) {
        // Show early/late color
        d.setColor(mResources.getColor(color));
    } else {
        // For on-time, use header default color
        d.setColor(mResources.getColor(R.color.theme_primary));
    }
    d.setStroke(UIUtils.dpToPixels(mContext, 1), mResources.getColor(R.color.header_text_color));
    int pSides = UIUtils.dpToPixels(mContext, 5);
    int pTopBottom = UIUtils.dpToPixels(mContext, 2);
    statusView.setPadding(pSides, pTopBottom, pSides, pTopBottom);
    statusView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    statusView.measure(TextView.MeasureSpec.UNSPECIFIED, TextView.MeasureSpec.UNSPECIFIED);
    statusView.setText(statusText);
    PopupWindow p = new PopupWindow(statusView, statusView.getWidth(), statusView.getHeight());
    p.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    p.setBackgroundDrawable(new ColorDrawable(mResources.getColor(android.R.color.transparent)));
    p.setOutsideTouchable(true);
    p.setTouchInterceptor(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            boolean touchInView;
            if (index == 0) {
                touchInView = UIUtils.isTouchInView(mEtaAndMin1, event);
            } else {
                touchInView = UIUtils.isTouchInView(mEtaAndMin2, event);
            }
            return touchInView;
        }
    });
    return p;
}

From source file:com.xda.one.ui.widget.TabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set
 * via/*from   w w  w  .ja v a2  s .c  o m*/
 * {@link #setCustomTabView(int, int)}.
 */
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);

    // Modified for XDA One
    final TypedValue typedValue = new TypedValue();
    getContext().getTheme().resolveAttribute(android.R.attr.textAppearanceSmallInverse, typedValue, true);
    textView.setTextAppearance(getContext(), typedValue.resourceId);
    getContext().getTheme().resolveAttribute(android.R.attr.textColorPrimaryInverse, typedValue, true);
    textView.setTextColor(getResources().getColor(typedValue.resourceId));
    textView.setTypeface(null, Typeface.BOLD);

    // Customized for app
    textView.setLayoutParams(
            new LinearLayout.LayoutParams(0, MATCH_PARENT, 1f / mViewPager.getAdapter().getCount()));
    textView.setSingleLine();
    AutofitHelper.create(textView);

    // Modified for XDA One
    getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);
    textView.setBackgroundResource(typedValue.resourceId);
    textView.setAllCaps(true);

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    return textView;
}

From source file:com.shichai.www.choume.view.tab.SmartTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}.// w  ww. j  a va  2 s .c  o  m
 */
protected TextView createDefaultTabView(CharSequence title) {
    TextView textView = new TextView(getContext());
    textView.setGravity(Gravity.CENTER);
    textView.setText(title);
    textView.setTextColor(mTabViewTextColor);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTabViewTextSize);
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.MATCH_PARENT));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(mTabViewTextAllCaps);
    }

    textView.setPadding(mTabViewTextHorizontalPadding, 0, mTabViewTextHorizontalPadding, 0);

    if (mTabViewTextMinWidth > 0) {
        textView.setMinWidth(mTabViewTextMinWidth);
    }

    return textView;
}

From source file:com.socialinfotech.tabbar.smart.SmartTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}.//from  w ww. j  av a 2  s .co  m
 */
protected TextView createDefaultTabView(CharSequence title) {
    TextView textView = new TextView(getContext());
    textView.setGravity(Gravity.CENTER);
    textView.setText(title);
    textView.setTextColor(mTabViewTextColors);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTabViewTextSize);
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.MATCH_PARENT));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(mTabViewTextAllCaps);
    }

    textView.setPadding(mTabViewTextHorizontalPadding, 0, mTabViewTextHorizontalPadding, 0);

    if (mTabViewTextMinWidth > 0) {
        textView.setMinWidth(mTabViewTextMinWidth);
    }

    return textView;
}

From source file:com.kingdon.view.PagerSlidingTabStrip.java

private void updateTabStyles() {

    for (int i = 0; i < tabCount; i++) {

        View v = tabsContainer.getChildAt(i);

        v.setBackgroundResource(tabBackgroundResId);

        if (v instanceof TextView) {

            TextView tab = (TextView) v;
            tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, tabTextSize);
            tab.setTypeface(tabTypeface, tabTypefaceStyle);
            tab.setTextColor(tabTextColor);

            // setAllCaps() is only available from API 14, so the upper case
            // is made manually if we are on a
            // pre-ICS-build
            if (textAllCaps) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                    tab.setAllCaps(true);
                } else {
                    tab.setText(tab.getText().toString().toUpperCase(locale));
                }//  w  w  w.jav  a  2s  .c  o  m
            }
            if (i == selectedPosition) {
                tab.setTextColor(selectedTabTextColor);
                tab.setBackgroundResource(selectedTabBGColor);
            }
        }
    }

}