Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make a selection border that tracks the checked radio button?
    primarykey
    data
    text
    <p>I have a set of <code>RadioButton</code>s with a custom style. I want to display a border around the button that is currently checked. This should be easy enough using XML, but now I want the border to be animated. If a new radio button is checked, the border should "fly" to its new location with a fancy animation:</p> <pre><code>+------+ |* btn1| o btn2 +------+ +------+ o b|n1 * |tn2 +------+ +------+ o btn1 |* btn2| +------+ </code></pre> <p>Because of this, I decided to make the border into a separate <code>View</code> object, so I can animate it properly. The trouble is in tracking the location of the corresponding radio button on the screen.</p> <p>I'm trying to get it to work <em>without</em> animations first. My current attempt looks something like this (only showing the relevant attributes):</p> <pre><code> &lt;RelativeLayout android:layout_alignParentLeft="true" android:layout_alignParentTop="true"&gt; &lt;RadioGroup android:id="@+id/radio_group"&gt; &lt;RadioButton/&gt; &lt;RadioButton/&gt; &lt;RadioButton/&gt; &lt;/RadioGroup&gt; &lt;View android:id="@+id/selection_border" android:layout_alignParentLeft="true" android:layout_alignParentTop="true"/&gt; &lt;/RelativeLayout&gt; </code></pre> <p>In the <code>OnCheckedChangeListener</code> of the <code>RadioGroup</code>, I move the selection border around by setting its margin (I could set the position, but that's a little harder with a <code>RelativeLayout</code>):</p> <pre><code>View radioButton = findViewById(checkedId); View selectionBorder = findViewById(R.id.selection_border); ViewGroup radioGroup = (ViewGroup)findViewById(R.id.radio_group); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(selectionBorder.getLayoutParams()); params.leftMargin = radioGroup.getLeft() + radioButton.getLeft(); params.topMargin = radioGroup.getTop() + radioButton.getTop(); selection.setLayoutParams(params); selection.requestLayout(); </code></pre> <p>Trouble happens, however, on initialization. Because no layouting has been done yet, the position of the border is set incorrectly. It doesn't seem to be possible to force a relayout immediately, and it also doesn't seem to be possible to get an event after layouting has been done.</p> <p>All this hassle leads me to believe that there must be a cleaner way to accomplish what I want. Any bright ideas?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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