user2468425
user2468425

Reputation: 419

Center a View inside a LinearLayout that contains another View

This is the xml of the LinearLayout which contains the two Views:

<RelativeLayout
        android:id="@+id/addItems"
        style="@android:style/Widget.ImageButton"
        android:layout_weight="5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/add_product_selector"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:padding="10dp" >

        <ImageView
            android:id="@+id/addIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:background="@drawable/mbs_add_blue"
            android:layout_marginLeft="45dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/addIcon"
            android:paddingLeft="5dp"
            android:text="@string/add_product"
            android:textSize="18dp"
            android:textColor="@color/black"
            android:textStyle="bold" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/startDictation"
        style="@android:style/Widget.ImageButton"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@drawable/add_product_selector"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:padding="10dp">

        <ImageView
            android:id="@+id/recordIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:src="@drawable/microphone"
            android:scaleType="fitCenter"/>
    </RelativeLayout>

</LinearLayout>

And it produces this output:

enter image description here

Since I need to set programmatically the second RelativeLayout (id=startDictation) to "gone" in some cases, I want the first RelativeLayout (id=addItems) and its children to be centered in those cases as well.

Since I defined layout_marginLeft="45dp" on the first ImageView (id=addIcon), when the second RelativeLayout (id=startDictation) is gone I should change it to keep the ImageView centered.

I have to change something in the layout because now it produces this output if I remove layout_marginLeft="45dp" from the first ImageView (id=addIcon) :

enter image description here

Upvotes: 0

Views: 74

Answers (4)

user2468425
user2468425

Reputation: 419

The problem was solved setting the visibility gone in the ImageView and not in the RelativeLayout

Upvotes: 1

Ajinkya
Ajinkya

Reputation: 1077

try to set layout gravity programatically. When you call startDictation visibilty to gone use this code

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.weight = 1.0f;
params.gravity = Gravity.CENTER_VERTICAL;

addItems.setLayoutParams(params);

also do not forget to reaarange params when you visible your startdictation layout

Upvotes: 0

Thilek
Thilek

Reputation: 676

have you tried using android:layout_gravity="center_vertical" ? You can choose different values inside.

Upvotes: 0

gilgil28
gilgil28

Reputation: 540

Why not use setVisibility(View.INVISIBLE) instead of View.GONE?

Upvotes: 0

Related Questions