Tuesday, 26 May 2015

Custom class for radio group using tablelayout

//add your package name
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.TableLayout;
import android.widget.TableRow;

/**
 * @author diego
 *
 */
public class ToggleButtonGroupTableLayout extends TableLayout  implements OnClickListener {

    private static final String TAG = "ToggleButtonGroupTableLayout";
    private RadioButton activeRadioButton;

    /** 
     * @param context
     */
    public ToggleButtonGroupTableLayout(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    /**
     * @param context
     * @param attrs
     */
    public ToggleButtonGroupTableLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onClick(View v) {
        final RadioButton rb = (RadioButton) v;
        if ( activeRadioButton != null ) {
            activeRadioButton.setChecked(false);
        }
        rb.setChecked(true);
        activeRadioButton = rb;
    }

    /* (non-Javadoc)
     * @see android.widget.TableLayout#addView(android.view.View, int, android.view.ViewGroup.LayoutParams)
     */
    @Override
    public void addView(View child, int index,
            android.view.ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        setChildrenOnClickListener((TableRow)child);
    }


    /* (non-Javadoc)
     * @see android.widget.TableLayout#addView(android.view.View, android.view.ViewGroup.LayoutParams)
     */
    @Override
    public void addView(View child, android.view.ViewGroup.LayoutParams params) {
        super.addView(child, params);
        setChildrenOnClickListener((TableRow)child);
    }


    private void setChildrenOnClickListener(TableRow tr) {
        final int c = tr.getChildCount();
        for (int i=0; i < c; i++) {
            final View v = tr.getChildAt(i);
            if ( v instanceof RadioButton ) {
                v.setOnClickListener(this);
            }
        }
    }

    public int getCheckedRadioButtonId() {
        if ( activeRadioButton != null ) {
            return activeRadioButton.getId();
        }

        return -1;
    }
}



xml  for the multi row radio group




<yourpackagename with class.ToggleButtonGroupTableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/radGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/fullName_menu" >

        <TableRow>

            <RadioButton
                android:id="@+id/chktext"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_weight="1"
                android:button="@null"
                android:fontFamily="sans-serif-light"
                android:paddingLeft="5dip"
                android:background="@drawable/volleyball_menuicon_selector"
                android:textColor="#ffffff"
                android:textSize="14sp" >
            </RadioButton>

            <RadioButton
                android:id="@+id/chkemail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_weight="1"
                android:button="@null"
                android:fontFamily="sans-serif-light"
                android:paddingLeft="5dip"
               android:background="@drawable/futsall_menuicon"
                android:textColor="#ffffff"
                android:textSize="14sp" >
            </RadioButton>

            <RadioButton
                android:id="@+id/chkwhatapp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_weight="1"
                android:button="@null"
                android:fontFamily="sans-serif-light"
                android:paddingLeft="5dip"
                android:background="@drawable/futsall_menuicon"
                android:textColor="#ffffff"
                android:textSize="14sp" >
            </RadioButton>
        </TableRow>
        <TableRow>

                      <RadioButton
                           android:id="@+id/chkfacetime"
                           android:layout_width="match_parent"
                           android:layout_height="wrap_content"
                           android:layout_alignParentLeft="true"
                           android:layout_centerVertical="true"
                           android:layout_weight="1"
                           android:button="@null"
                           android:fontFamily="sans-serif-light"
                           android:paddingLeft="5dip"
                          android:background="@drawable/racing_menuicon"
                           android:textColor="#ffffff"
                           android:textSize="14sp" >
                       </RadioButton>

                       <RadioButton
                           android:id="@+id/chkskpe"
                           android:layout_width="match_parent"
                           android:layout_height="wrap_content"
                           android:layout_alignParentLeft="true"
                           android:layout_centerVertical="true"
                           android:layout_weight="1"
                           android:button="@null"
                           android:fontFamily="sans-serif-light"
                           android:paddingLeft="5dip"
                          android:background="@drawable/racing_menuicon"
                           android:textColor="#ffffff"
                           android:textSize="14sp" >
                       </RadioButton>

                       <RadioButton
                           android:id="@+id/chkall"
                           android:layout_width="match_parent"
                           android:layout_height="wrap_content"
                           android:layout_alignParentLeft="true"
                           android:layout_centerVertical="true"
                           android:layout_weight="1"
                           android:button="@null"
                           android:fontFamily="sans-serif-light"
                           android:paddingLeft="5dip"
                           android:background="@drawable/padel_menuicon"
                           android:textColor="#ffffff"
                           android:textSize="14sp" >
                       </RadioButton>
                   </TableRow>

    </yourpackagename with classToggleButtonGroupTableLayout>



custom radio button class place it in drawable folder  and use it for android:background=@drawable/


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/volleyball_menuicon" android:state_checked="true"/>
    <item android:drawable="@drawable/futsall_menuicon" android:state_checked="false"/>

</selector>






No comments:

Post a Comment