1

I have to do some flattening on my layout, below piece of it after. Percent TextView and RadioButton are in same place on the left and are switching using animations. Second TextView shows some text fulfilling width. Problem is that RadioButton is shorter than percent TextView, but I'm using alignLeft and alignRight for same width, and even with gravity set to center radio drawable is aligned left. I've found this topic, but is a bit old, before Material, and currently this class is more complicated. Any advice how to center this radio in own width (or width of percent textview) without! adding another ViewGroup? (precent and radio might be in another RelativeLayout and set centerInParent, as I had before tries for flattening)

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/option_percent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:singleLine="true"
        android:gravity="center"
        android:ems="3"
        android:visibility="invisible"/>
    <RadioButton
        android:id="@+id/option_radio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@id/option_percent"
        android:layout_alignRight="@id/option_percent"
        android:gravity="center"/>
    <TextView
        android:id="@+id/option_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/option_percent"/>
</RelativeLayout>
Community
  • 1
  • 1
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Have you tried using marginLeft in the radiobutton? – Ricardo Apr 12 '16 at 10:56
  • no, because it's not centering `RadioButton`, only moving. radio Drawables have different width on different devices. For now my only idea is to programmatically measure percent `TextView` and set properly calculated `marginLeft` for `RadioButton`, looking for easier way, maybe xml-only. Anyway thanks for suggestion :) – snachmsm Apr 12 '16 at 11:03

1 Answers1

0

OK, I have some workaround for my case. I'm not accepting this post, because its not a proper answer. Im still counting on another way, more elegant ;)

from above xml I've set fixed width for percent and removed ems, practically no difference. also removed aligning left and right to percent in radio attributes, so it have own size in both dimensions (wrap_content). next step is in code:

if(radioMarginLeft<0) {
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(
                    percentTextView.getLayoutParams().width, View.MeasureSpec.AT_MOST);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

    radioButton.measure(widthMeasureSpec, heightMeasureSpec);

    radioMarginLeft = (percentTextView.getLayoutParams().width -
                    radioButton.getMeasuredWidth())/2;
}

((RelativeLayout.LayoutParams) radioButton.getLayoutParams()).leftMargin=radioMarginLeft;

measuring radio, percent now has fixed width, so no need to measure. radioMarginLeft is calculated only once and kept outside method, in my case it may be even static.

additional: be careful with setting padding for radio, because of Add margin between a RadioButton and its label in Android? (check most upvoted answer)

Community
  • 1
  • 1
snachmsm
  • 17,866
  • 3
  • 32
  • 74