Example usage for android.widget RadioButton setButtonDrawable

List of usage examples for android.widget RadioButton setButtonDrawable

Introduction

In this page you can find the example usage for android.widget RadioButton setButtonDrawable.

Prototype

public void setButtonDrawable(@DrawableRes int resId) 

Source Link

Document

Sets a drawable as the compound button image given its resource identifier.

Usage

From source file:com.fa.mastodon.fragment.ComposeOptionsFragment.java

private static void setRadioButtonDrawable(Context context, RadioButton button, @DrawableRes int id) {
    ColorStateList list = new ColorStateList(
            new int[][] { new int[] { -android.R.attr.state_checked },
                    new int[] { android.R.attr.state_checked } },
            new int[] { ThemeUtils.getColor(context, R.attr.compose_image_button_tint),
                    ThemeUtils.getColor(context, R.attr.colorAccent) });
    Drawable drawable = VectorDrawableCompat.create(context.getResources(), id, context.getTheme());
    if (drawable == null) {
        return;//  www. j  a va  2 s . c o  m
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        button.setButtonTintList(list);
    } else {
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(drawable, list);
    }
    button.setButtonDrawable(drawable);
}

From source file:com.lokiy.widget.BannerView.java

private RadioButton generateRadioButton(int i, int w) {
    RadioButton rb = new RadioButton(getContext());
    rb.setBackgroundResource(mIndicatorId);
    rb.setButtonDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
    rb.setId(i);/*from  w w  w  .  ja  v a2  s .  c  o m*/
    rb.setPadding(0, 0, 0, 0);
    RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(w, w);
    params.leftMargin = w / 2;
    params.rightMargin = w / 2;
    rb.setLayoutParams(params);
    if (i == 0) {
        rb.setChecked(true);
    }
    return rb;
}

From source file:com.astuetz.PagerSlidingTabStripWithStyle.java

private void addTextTab(final int position, String title) {
    RadioButton tab = new RadioButton(getContext());
    tab.setButtonDrawable(android.R.color.transparent);
    tab.setBackgroundDrawable(null);//from   w  ww. ja  v a  2s .  c om
    tab.setText(title);
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();
    addTab(position, tab);
}

From source file:com.trimph.toprand.trimphrxandroid.trimph.ui.main.news.view.PagerSlidingTabStrip.java

private void addIconTab(final int position, int resId) {
    RadioButton tab = new RadioButton(getContext());
    tab.setButtonDrawable(null);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        tab.setCompoundDrawables(null, getContext().getDrawable(resId), null, null);
    }/*from   www . ja  v a  2  s. c  o  m*/

    if (position == currentPosition) {
        tab.setChecked(true);
    } else {
        tab.setChecked(false);
    }

    addTab(position, tab);
}

From source file:com.cs.widget.tab.PagerSlidingTabStrip1.java

private void addIconTab(final int position, TextImageRes res) {

    RadioButton tab = new RadioButton(getContext());
    tab.setCompoundDrawablesWithIntrinsicBounds(0, res.getResId(), 0, 0);
    tab.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
    tab.setGravity(Gravity.CENTER);/*  w  w  w .j  a v a  2 s  . c  om*/
    tab.setText(res.getText());
    tab.setTextColor(getContext().getResources().getColorStateList(res.getTextColorId()));
    addTab(position, tab);

}

From source file:com.luckybuy.ctrls.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   ww  w. j ava 2  s .c  om
 */
protected RadioButton createDefaultTabView(Context context) {
    RadioButton rbTab = new RadioButton(context);
    rbTab.setGravity(Gravity.CENTER);
    rbTab.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTabTextSize);
    rbTab.setTypeface(Typeface.DEFAULT_BOLD);
    rbTab.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
    if (mTabLayoutEven) {
        LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(0,
                ViewGroup.LayoutParams.MATCH_PARENT, 1);
        rbTab.setLayoutParams(layoutParams);
    }
    if (mTabColorStateList != -1) {
        rbTab.setTextColor(getResources().getColorStateList(mTabColorStateList));
    }

    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);
        rbTab.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
        rbTab.setAllCaps(true);
    }

    int padding = (int) (mTabTextPadding * getResources().getDisplayMetrics().density);
    rbTab.setPadding(padding, 0, padding, 0);

    return rbTab;
}

From source file:com.kkbox.toolkit.app.KKTabFragment.java

protected void initView(View view, int[] buttonTextResourcetId, boolean showSubFragmentAnimation,
        int currentIndex) {
    fixStateForNestedFragment();//from  w  w  w .j  a v a 2s .  co m
    initView(view);
    if (this.currentIndex == -1) {
        this.currentIndex = currentIndex;
    }
    this.showSubFragmentAnimation = showSubFragmentAnimation;
    radioGroup = (RadioGroup) view.findViewById(R.id.button_radiogroup);

    TypedValue typedValue = new TypedValue();
    getActivity().getTheme().resolveAttribute(R.attr.KKTabFragmentStyle, typedValue, true);
    TypedArray array = getActivity().obtainStyledAttributes(typedValue.resourceId,
            new int[] { R.attr.KKTabButtonBackgroundLeft, R.attr.KKTabButtonBackgroundMiddle,
                    R.attr.KKTabButtonBackgroundRight });
    int tabButtonBackgroundLeftResourceId = array.getResourceId(0, -1);
    int tabButtonBackgroundMiddleResourceId = array.getResourceId(1, -1);
    int tabButtonBackgroundRightResourceId = array.getResourceId(2, -1);
    array.recycle();
    array = getActivity().obtainStyledAttributes(typedValue.resourceId, new int[] { R.attr.KKTabBackground });
    int backgroundResourceId = array.getResourceId(0, -1);
    FrameLayout layoutRadioBar = (FrameLayout) view.findViewById(R.id.layout_radio_bar);
    if (backgroundResourceId != -1) {
        layoutRadioBar.setBackgroundResource(backgroundResourceId);
    }
    array.recycle();

    array = getActivity().obtainStyledAttributes(typedValue.resourceId, new int[] { R.attr.KKTabOverlay });
    int overlayResourceId = array.getResourceId(0, -1);
    ImageView viewOverlay = (ImageView) view.findViewById(R.id.view_overlay);
    if (overlayResourceId != -1) {
        viewOverlay.setBackgroundResource(overlayResourceId);
    }
    array.recycle();

    array = getActivity().obtainStyledAttributes(typedValue.resourceId, new int[] { android.R.attr.textSize });
    int textSize = array.getDimensionPixelSize(0, -1);
    array.recycle();
    array = getActivity().obtainStyledAttributes(typedValue.resourceId, new int[] { android.R.attr.textColor });
    ColorStateList textColor = array.getColorStateList(0);
    array.recycle();
    array = getActivity().obtainStyledAttributes(typedValue.resourceId, new int[] { android.R.attr.height });
    int height = array.getDimensionPixelSize(0, -1);
    array.recycle();
    for (int i = 0; i < buttonTextResourcetId.length; i++) {
        RadioButton radioButton = new RadioButton(getActivity());
        if (i == 0 && tabButtonBackgroundLeftResourceId != -1) {
            radioButton.setBackgroundResource(tabButtonBackgroundLeftResourceId);
        } else if (i == buttonTextResourcetId.length - 1 && tabButtonBackgroundRightResourceId != -1) {
            radioButton.setBackgroundResource(tabButtonBackgroundRightResourceId);
        } else if (tabButtonBackgroundMiddleResourceId != -1) {
            radioButton.setBackgroundResource(tabButtonBackgroundMiddleResourceId);
        }
        radioButton.setText(buttonTextResourcetId[i]);
        radioButton.setButtonDrawable(new StateListDrawable());
        radioButton.setGravity(Gravity.CENTER);
        radioButton.setTextColor(textColor);
        radioButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        radioButton.setLayoutParams(new RadioGroup.LayoutParams(0, height, 1));
        radioButton.setOnClickListener(buttonClickListener);
        radioGroup.addView(radioButton);
    }
    currentFragment = null;
    radioGroup.getChildAt(this.currentIndex).performClick();

}

From source file:com.landenlabs.all_UiDemo.frag.RadioBtnFrag.java

private void addTabBar(RadioGroup tabHolder, float weight, int padding) {
    final int maxTabs = 4;
    // tabHolder.removeAllViews();

    int[][] states = new int[][] { new int[] { android.R.attr.state_checked }, new int[] {} };
    ColorStateList colorStateList = new ColorStateList(states, new int[] { 0xff00ff00, 0x80ff0000 });

    RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT, weight);

    /*// www . j a va2 s .  co m
    ViewOutlineProvider outlineBoundary;
    if (Build.VERSION.SDK_INT >= 21) {
    outlineBoundary = new ViewOutlineProvider() {
        @Override
        public void getOutline(View view, Outline outline) {
            outline.setRect(0, 0, 200, 60); // view.getMeasuredWidth(), view.getMeasuredHeight());
        }
    };
    }
    */

    String[] pageNames = new String[] { "Home", "Map", "Hourly", "Daily" };
    int tabCnt = 0;
    for (String pageName : pageNames) {
        RadioButton button = new RadioButton(tabHolder.getContext());

        String resName = "tab_" + pageName.toLowerCase();
        int resID = getResources().getIdentifier(resName, "drawable", this.getContext().getPackageName());
        Drawable tabBtnIcon = getResources().getDrawable(resID);
        if (tabBtnIcon != null) {

            if (tabBtnIcon != null && Build.VERSION.SDK_INT >= 21) {
                tabBtnIcon.setTintMode(PorterDuff.Mode.MULTIPLY);
                tabBtnIcon.setTintList(colorStateList);
            } else {
                tabBtnIcon = DrawableCompat.wrap(tabBtnIcon);
                DrawableCompat.setTintList(tabBtnIcon.mutate(), colorStateList);
                DrawableCompat.setTintMode(tabBtnIcon.mutate(), PorterDuff.Mode.MULTIPLY);
            }

            // tabBtnIcon = tabHolder.getResources().getDrawable(android.R.drawable.btn_radio);
            if (Build.VERSION.SDK_INT >= 21) {
                // Hide standard radio button but leave ripple effect
                button.setButtonDrawable(null);
            } else {
                // Hide standard radio button
                button.setButtonDrawable(new StateListDrawable());
            }
            button.setCompoundDrawablesWithIntrinsicBounds(null, tabBtnIcon, null, null);

            /*
            // button.setBackgroundResource(R.drawable.ripple_boarderless);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    
            button.setCompoundDrawablesWithIntrinsicBounds(null, tabBtnIcon, null, null);
            // button.setCompoundDrawablesRelativeWithIntrinsicBounds(null, tabBtnIcon, null, null);
            // tabBtnIcon.setBounds(0, 0, 50, 100); // img.getMinimumWidth(), img.getMinimumHeight());
            // button.setCompoundDrawables(null, tabBtnIcon, null, null);
            } else {
            button.setCompoundDrawablesWithIntrinsicBounds(null, tabBtnIcon, null, null);
            }
            */

            button.setBackgroundResource(R.drawable.ripple_boarderless);
            button.setPadding(padding, padding, padding, padding);
            button.setGravity(Gravity.CENTER);
            button.setTextColor(colorStateList);
            button.setText(pageName);

            tabHolder.addView(button, lp);

            if (++tabCnt == maxTabs)
                break;
        }
    }
}