michael_
michael_

Reputation: 145

Weird behavior with buttons and visibility gone

I am having a very weird problem with buttons, vertical centering and visibility gone. In my app I have a list of books, and I am using the swipelistview library to create buttons behind each row. I have 3 buttons: return, send reminder and delete. The return and send reminder buttons visibility is set to Visibility.GONE dynamically if the book isn't lended. Now here's my problem. I have the following xml layout for the back of the rows.

<LinearLayout
        android:id="@+id/swipelist_backview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:tag="swipelist_backview"
        android:background="#101010">
                <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="55px"
                android:layout_gravity="center_vertical"
                android:id="@+id/swipe_button1"
                android:text="@string/markAsReturned"/>

                <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:id="@+id/swipe_button2"
                android:layout_gravity="center_vertical"
                android:text="@string/sendReminder"/>

                <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:id="@+id/swipe_button3"
                android:layout_gravity="center_vertical"
                android:text="@string/delete"/>


</LinearLayout>

Now the expected result from this is that the buttons will be vertically centered in the middle of each row, which does happen if the book is lended, and all the buttons are visible. But if the book isn't lended, the only button shown is delete, and it isn't aligned .

I am also setting the left margin of the delete button to 55 px using the following code:

if(!item.isLended())
{
    btnReturn.setVisibility(View.GONE);
    btnSendReminder.setVisibility(View.GONE);
    LayoutParams lp = new LayoutParams(btnDelete.getLayoutParams());
    lp.setMargins(55, 0, 0, 0);
    btnDelete.setLayoutParams(lp);
}

I thought this might be removing the verical alignment, but LayoutParams doesn't seem to have a way to set layout-gravity, so it doesn't look like it.

Upvotes: 0

Views: 267

Answers (1)

michael_
michael_

Reputation: 145

Well, I guess it was the LayoutParams. If anyone is intersted, here's how I solved it:

if(!item.isLended())
{
    btnReturn.setVisibility(View.GONE);
    btnSendReminder.setVisibility(View.GONE);
    LayoutParams lp = new LayoutParams(btnDelete.getLayoutParams());
    lp.setMargins(55, 0, 0, 0);
    lp.gravity = Gravity.CENTER_VERTICAL;
    btnDelete.setLayoutParams(lp);
}

Upvotes: 1

Related Questions