Botón de alineación de Android según la visibilidad

I have a little issue with setting align of a button in my application. I have 4 different buttons in one layout and depending on some values from database I have to set the visibility of one button. Here is the example of xml code which I'm using :

 <RelativeLayout 
    android:id="@+id/coll_buttons"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center"
    android:layout_marginBottom="10dp">


  <Button
        android:id="@+id/sync"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Synchronize"
        android:textSize="9dp"
        android:background="@drawable/ic_white_button"
        android:textColor="#333333"
        android:layout_margin="10dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:layout_alignParentLeft="true" />   

    <Button
        android:id="@+id/activate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activate"
        android:textSize="9dp"
        android:background="@drawable/ic_white_button"
        android:textColor="#333333"
        android:layout_margin="10dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:layout_alignParentLeft="true" />

    <Button 
        android:id="@+id/buy_stampii"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Buy Stampii"
        android:textSize="9dp"
        android:background="@drawable/ic_white_button"
        android:textColor="#333333"
        android:layout_marginTop="10dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:layout_toRightOf="@+id/activate"
        android:layout_marginLeft="5dp" />

    <Button
        android:id="@+id/deactivate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Deactivate"
        android:textSize="9dp"
        android:background="@drawable/ic_white_button"
        android:textColor="#333333"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:layout_alignParentRight="true" />

</RelativeLayout>

In the first case I have to set the visibility of Button Activate to false and set the visibility of Button Sync to true. But when I do that my Buy Stampii button is overlaying Sync Button because I'm setting Buy Stampii . toRightOf Activate Button. So is there any way that I can set to Buy Stampii Botón toRightOf property for two different buttons or how can I solve that issue?

¡Muchas gracias!

preguntado el 08 de noviembre de 11 a las 15:11

3 Respuestas

NOp I do not think that you can set the toRightOf property pointing to 2 widgets in the same time.

Yes there is one workaround that you can do. Instead of putting the view to visibility gone , you can put the property to INVISIBLE. in this case if you say A to be to the right of B it will works just fine if the A is VISIBLE. But the problem is when the A is gone, gone means that B would not know where to be placed (to the right of 'nothing' cause A is gone). The trick is to put the A to be INVISIBLE. in this case you can still say B to be the right of A and the B will know where to be placed.

The drawback is that when A is invisible the place will still be occupied but you can easily solve this issue with playing with the size of the layout. For example when INVISIBLE you can set the width to 1dip and when VISIBLE you can set to wrap_content or what ever that suit you

This works I have tried ones but the solution is UGLY and basically you shouldn't do workaround like this.

respondido 08 nov., 11:19

I think the solution is to change RelativeLayout.LayoutParams with approptiate rules when you set your buttons Visible/Invisible(Gone). Just get current parameters of the button and .addRule(RelativeLayout.ALIGN_RIGHT, <new anchor id>)

respondido 08 nov., 11:19

A better way to handle this would be to have two different layout files for the two different layouts, vertical and horizontal, respectively.

En realidad hay un built in way to do this.

I suggest you read that documentation, it's a quite easy way to fix your problem!

Espero que esto ayude.

respondido 08 nov., 11:19

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.