Example usage for android.widget RadioGroup addView

List of usage examples for android.widget RadioGroup addView

Introduction

In this page you can find the example usage for android.widget RadioGroup addView.

Prototype

public void addView(View child, int index) 

Source Link

Document

Adds a child view.

Usage

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);

    /*/*from w  w w.ja v a 2  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;
        }
    }
}