Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Heres an example. Its really possible to just use a radio button. But this is an example that should help. Of course I did not give you EVERYTHING but this should show you it is possible </p> <pre><code>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; public class ToggleButton extends TableLayout implements OnClickListener { private static final String TAG = "ToggleButton"; private RadioButton activeRadioButton; /** * @param context */ public ToggleButton(Context context) { super(context); // TODO Auto-generated constructor stub } /** * @param context * @param attrs */ public ToggleButton(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override public void onClick(View v) { final RadioButton rb = (RadioButton) v; ///////// do whatever ///////////// } /* (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 &lt; 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; } } </code></pre> <p>and create a layout like this (of course you need to clean it up but you got the idea)</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;com.example.android.view.ToggleButton xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radGroup1"&gt; &lt;TableRow&gt; &lt;RadioButton android:id="@+id/rad1" android:text="Button1" android:layout_width="105px" android:layout_height="wrap_content" android:textSize="13px" /&gt; &lt;RadioButton android:id="@+id/rad2" android:text="Button2" android:layout_width="105px" android:textSize="13px" android:layout_height="wrap_content" /&gt; &lt;RadioButton android:id="@+id/rad3" android:text="Button3" android:layout_width="105px" android:textSize="13px" android:layout_height="wrap_content" /&gt; &lt;/TableRow&gt; &lt;TableRow&gt; &lt;RadioButton android:id="@+id/rad1" android:text="Button1" android:layout_width="105px" android:layout_height="wrap_content" android:textSize="13px" /&gt; &lt;RadioButton android:id="@+id/rad2" android:text="Button2" android:layout_width="105px" android:textSize="13px" android:layout_height="wrap_content" /&gt; &lt;RadioButton android:id="@+id/rad3" android:text="Button3" android:layout_width="105px" android:textSize="13px" android:layout_height="wrap_content" /&gt; &lt;/TableRow&gt; &lt;TableRow&gt; &lt;RadioButton android:id="@+id/rad1" android:text="Button1" android:layout_width="105px" android:layout_height="wrap_content" android:textSize="13px" /&gt; &lt;RadioButton android:id="@+id/rad2" android:text="Button2" android:layout_width="105px" android:textSize="13px" android:layout_height="wrap_content" /&gt; &lt;RadioButton android:id="@+id/rad3" android:text="Button3" android:layout_width="105px" android:textSize="13px" android:layout_height="wrap_content" /&gt; &lt;/TableRow&gt; &lt;/com.example.android.view.ToggleButton&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload