Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tried to solve your issue and done from my side.</p> <p>I explain approach below and for more details and complete Source code go through this blog below.</p> <p><a href="http://amitandroid.blogspot.in/2013/03/android-custon-single-choice-lsitview.html" rel="nofollow noreferrer">http://amitandroid.blogspot.in/2013/03/android-custon-single-choice-lsitview.html</a></p> <p>Hope this will solve your problem.</p> <p><img src="https://i.stack.imgur.com/U4pn6.png" alt="enter image description here"></p> <p>I created Custom single Choice list. You can change the layout and add your widget accordingly.</p> <p>Step1)</p> <pre><code>package com.custom.view; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.Checkable; import android.widget.RelativeLayout; public class CheckableRelativeLayout extends RelativeLayout implements Checkable { private boolean isChecked; private List&lt;Checkable&gt; checkableViews; public CheckableRelativeLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initialise(attrs); } public CheckableRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); initialise(attrs); } public CheckableRelativeLayout(Context context, int checkableId) { super(context); initialise(null); } public boolean isChecked() { return isChecked; } public void setChecked(boolean isChecked) { this.isChecked = isChecked; for (Checkable c : checkableViews) { c.setChecked(isChecked); } } public void toggle() { this.isChecked = !this.isChecked; for (Checkable c : checkableViews) { c.toggle(); } } @Override protected void onFinishInflate() { super.onFinishInflate(); final int childCount = this.getChildCount(); for (int i = 0; i &lt; childCount; ++i) { findCheckableChildren(this.getChildAt(i)); } } /** * Read the custom XML attributes */ private void initialise(AttributeSet attrs) { this.isChecked = false; this.checkableViews = new ArrayList&lt;Checkable&gt;(5); } /** * Add to our checkable list all the children of the view that implement the * interface Checkable */ private void findCheckableChildren(View v) { if (v instanceof Checkable) { this.checkableViews.add((Checkable) v); } if (v instanceof ViewGroup) { final ViewGroup vg = (ViewGroup) v; final int childCount = vg.getChildCount(); for (int i = 0; i &lt; childCount; ++i) { findCheckableChildren(vg.getChildAt(i)); } } } } </code></pre> <p>Step2)</p> <pre><code>package com.custom.view; import android.content.Context; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.MotionEvent; import android.widget.CheckBox; public class InertCheckBox extends CheckBox { public InertCheckBox(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public InertCheckBox(Context context, AttributeSet attrs) { super(context, attrs); } public InertCheckBox(Context context) { super(context); } @Override public boolean onTouchEvent(MotionEvent event) { return false; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return false; } @Override public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { return false; } @Override public boolean onKeyPreIme(int keyCode, KeyEvent event) { return false; } @Override public boolean onKeyShortcut(int keyCode, KeyEvent event) { return false; } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { return false; } @Override public boolean onTrackballEvent(MotionEvent event) { return false; } } </code></pre> <p>Step3)</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;com.custom.view.CheckableRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginRight="10dp" android:paddingTop="5dp" android:paddingBottom="5dp"&gt; &lt;com.custom.view.InertCheckBox android:id="@+id/singleitemCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:focusable="false" android:button="@drawable/single_radio_chice" /&gt; &lt;TextView android:id="@+id/singleitemId" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_toRightOf="@+id/singleitemCheckBox" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:textSize="14sp" android:focusable="false" /&gt; &lt;TextView android:id="@+id/nextitem" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_toRightOf="@+id/singleitemId" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:textSize="14sp" android:focusable="false" /&gt; &lt;/com.custom.view.CheckableRelativeLayout&gt; Step4) &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" &gt; &lt;ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cacheColorHint="#00000000" android:divider="#b5b5b5" android:dividerHeight="1dp" android:choiceMode="singleChoice" android:listSelector="#00000000" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Step5) single_radio_chice.xml</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_checked="false" android:drawable="@drawable/radio_off" /&gt; &lt;item android:state_checked="true" android:drawable="@drawable/radio_on" /&gt; &lt;/selector&gt; </code></pre>
    singulars
    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. 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