android - What is the best way to implement dynamic borders in LinearLayout? -


i have list of items each of 2 checkboxes separated view divider.

<linearlayout     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="horizontal">      <!-- view 1 -->     <checkbox android:id="@+id/view1"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_weight="1"         android:text="view 1"         android:background="@drawable/checkbox_background"/>      <!-- separator -->     <view android:layout_width="2dp"         android:layout_height="match_parent"         android:background="@android:color/holo_green_light"         android:layout_margintop="8dp"         android:layout_marginbottom="8dp"/>      <!-- view 2 -->     <checkbox android:id="@+id/view2"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_weight="1"         android:text="view 2"         android:background="@drawable/checkbox_background"/> </linearlayout> 

drawable/checkbox_background.xml selector 2 solid shapes - white unchecked state , red checked one.

this list view looks like:

views_before

what i'm trying achieve get rid of divider when either of adjacent checkboxes checked (in fact, have more two):

views_after

as per understanding, options are:

  1. set separator's visibility view.gone when checkbox sets in checked state. forces view1 , view2 redraw , shift texts little closer center, doesn't nice.

  2. overlap view1 , view2 @ edges using negative margins width of separator, , put separator on them. make view.invisible. technique requires relativelayout, , can't use relativelayout because need space checkboxes evenly across screen.

  3. add (red) separator , show 1 @ time. ugly.

  4. change separator's layoutparams (height) , background. best bet far.

are there better solutions? maybe don't require things programatically?

thanks.

you may nest linearlayout check buttons in relativelayout , put divider on them:

<?xml version="1.0" encoding="utf-8"?> <relativelayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content">      <linearlayout         android:id="@+id/parent"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="horizontal">          <!-- view 1 -->         <checkbox             android:id="@+id/view1"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_weight="1"             android:background="@drawable/checkbox_background"             android:text="view 1"/>          <!-- view 2 -->         <checkbox             android:id="@+id/view2"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_weight="1"             android:background="@drawable/checkbox_background"             android:text="view 2"/>      <!-- separator -->     <view         android:layout_width="2dp"         android:layout_height="0dp"         android:layout_alignbottom="@id/parent"         android:layout_aligntop="@id/parent"         android:layout_centerinparent="true"         android:layout_gravity="center"         android:layout_marginbottom="8dp"         android:layout_margintop="8dp"         android:background="@android:color/holo_green_light"/>  </relativelayout> 

if don't need have 8dp margins , can set fixed height divider may use framelayout. has better performance, because it's less complicated view:

<?xml version="1.0" encoding="utf-8"?> <framelayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content">      <linearlayout         android:id="@+id/parent"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="horizontal">          <!-- checkboxes -->      </linearlayout>      <!-- separator -->     <view         android:layout_width="2dp"         android:layout_height="16dp"         android:layout_gravity="center"         android:layout_marginbottom="8dp"         android:layout_margintop="8dp"         android:background="@android:color/holo_green_light"/>  </framelayout> 

Comments