CDrosos CDrosos - 7 days ago 6
Android Question

Align view in the center of another view

I have this layout:

enter image description here

Which i have create with this code:

<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/BusinessLogoCircleImageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="36dp"
android:layout_marginBottom="36dp"
android:layout_gravity="right|end"
android:background="#ff4fc3f7" />
<LinearLayout
android:layout_toRightOf="@+id/BusinessLogoCircleImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessNameTextView"
android:background="@color/primary_color"
android:textSize="@dimen/Address_Text_size"
android:textColor="@color/white"
android:typeface="normal" />
<TextView
android:text="Address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessAddressTextView"
android:layout_below="@+id/BusinessNameTextView"
android:background="@color/primary_color"
android:textSize="@dimen/Address_Text_size"
android:textColor="@color/white"
android:typeface="normal" />
<TextView
android:text="Website"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessWebsiteTextView"
android:layout_below="@+id/BusinessAddressTextView"
android:background="@color/primary_color"
android:textSize="@dimen/Address_Text_size"
android:textColor="@color/white"
android:typeface="normal" />
</LinearLayout>
<TextView
android:text="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/CommentsCountTextView"
android:layout_below="@+id/BusinessLogoCircleImageView"
android:gravity="center"
android:background="@color/primary_color"
android:textSize="@dimen/Counter_Default_size"
android:textColor="@color/white"
android:typeface="normal" />
<TextView
android:text="overall thanks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/CommentsCountTextView"
android:id="@+id/CommentsTextTextView"
android:gravity="center"
android:padding="3dp"
android:textSize="@dimen/SubCounter_Text_size"
android:textColor="@color/white"
android:background="@color/subtitle_color"
android:typeface="normal" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/CommentsTextTextView"
android:id="@+id/StaffTeamListView" />
<TextView
android:id="@+id/emptyMessagesListTextView"
android:text=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center" />




The problem is that i want the 3 textview to be in the vertical center of the imageview on the left. but i dont know how to do that, i have try with relative layout and also wrapping those textview on a linearlayout

but the problem is always the same, how i can set the linearlayout to center the content according to the image? if i can do this without linearlayout i would prefer it, keep in mind also that i want to display a small image (icon) on the left of every textview.

One solution that i have in mind is if i put the imageview and the linearlayout with the textviews into another linearlayout and allign then center vertical but is this the best solution?

Answer

Here's some "pseudo" code:

linearlayout orientation=horizontal, width=wrap_content
      imageview

      linearlayout orientation=vertial, width=0dp, weight=1
          textview1
          textview2
          textview 3

A weight of 1 will give the textviews' parent the remaining space.

Comments