TextView Layout 2 lines and right aligned text - android

I want to achieve this layout as in screenshot: ImageView + 2 lines of Textview -> left aligned, and in the same row I want to add another textView, but at the right end of the line.
What I tried:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="vertical">
<ImageView
android:id="#+id/imageViewIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:layout_alignParentStart="true"
android:paddingEnd="10dp"
android:paddingStart="10dp"
/>
<TextView
android:id="#+id/book_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/imageViewIcon"
android:textSize="16sp"
android:textColor="#468bff"
android:layout_toEndOf="#+id/imageViewIcon"
/>
<TextView
android:id="#+id/author_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/book_title"
android:layout_toRightOf="#+id/imageViewIcon"
android:layout_toEndOf="#+id/imageViewIcon" />
<TextView
android:id="#+id/dist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/book_title"
android:layout_toEndOf="#+id/book_title"
android:layout_centerVertical="true"
android:gravity="end"
/>
As you can see in the last TextView I used toRightOf, then gravity end. This causes however to position the last TextView over the 2 lines, so it is not aligned to the right.

Solution can be multiple . Try something like this .
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="vertical">
<ImageView
android:id="#+id/imageViewIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher"
android:layout_alignParentStart="true"
android:paddingEnd="10dp"
android:paddingStart="10dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_toRightOf="#+id/imageViewIcon"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/dist"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="#+id/book_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/imageViewIcon"
android:textSize="16sp"
android:textColor="#468bff"
android:layout_toEndOf="#+id/imageViewIcon"
/>
<TextView
android:id="#+id/author_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/book_title"
android:layout_toRightOf="#+id/imageViewIcon"
android:layout_toEndOf="#+id/imageViewIcon" />
</LinearLayout>
<TextView
android:id="#+id/dist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="end"
/>
</RelativeLayout>

Try the layout below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/iv"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/icon" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:weightSum="100"
android:layout_marginStart="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_1"
android:textSize="20sp"
android:layout_weight="60"
android:text="SOME TITLE"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_2"
android:layout_weight="40"
android:text="Subtitle"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="150m"
android:layout_marginStart="30dp"
android:gravity="center"
android:id="#+id/tv_3"/>
</LinearLayout>
Output:

Related

Binding of two TextViews on vertical

There is a RelativeLayout with a nested LinearLayout that contains two TextViews positioned horizontally relative to each other. This is DiscountAmount and AmountWithDiscount.
I need to keep the DiscountAmount and AmountWithDiscount always at the same level vertically, for this I put the android property in LinearLayout: android:layout_below = "# + id / textViewRowQuantity". But if the Name is too long and takes three lines, then it will go to DiscountAmount. If Name takes no more than 2 lines, then all is well.
Tell me, please, how can I solve this problem?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayoutSaleListRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:orientation="horizontal"
android:padding="5dp" >
<ImageView
android:id="#+id/imageViewRemoveRowSale"
android:layout_width="#dimen/size_30px"
android:layout_height="#dimen/size_30px"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="#drawable/ic_action_remove"/>
<RelativeLayout
android:id="#+id/relativeLayoutIncQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_toRightOf="#+id/imageViewRemoveRowSale"
android:layout_toLeftOf="#+id/imageViewEditRowSale"
android:orientation="horizontal">
<TextView
android:id="#+id/textViewRowPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="#+id/textViewRowQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textViewRowPrice"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="#+id/textViewRowName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/textViewRowPrice"
android:text="TextView" />
<LinearLayout
android:id="#+id/layoutDiscount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textViewRowQuantity"
android:orientation="horizontal">
<TextView
android:id="#+id/textViewRowDiscountAmount"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="#+id/textViewRowAmountWithDiscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:text="TextView" />
</LinearLayout>
</RelativeLayout>
<ImageView
android:id="#+id/imageViewEditRowSale"
android:layout_width="#dimen/size_45px"
android:layout_height="#dimen/size_45px"
android:layout_marginTop="5dp"
android:layout_alignParentRight="true"
android:src="#drawable/ic_action_edit" />
Add result:
Implemented through ConstraintLayout. Thanks to all.
You can limit the number of lines in the TextView to 2 by adding this lines:
android:ellipsize="end"
android:lines="2"
End ellipsize end means if the text is so long then it will be replaced with three dots ...
if you want to show AmountWithDiscount is below to discount then change liner layout orientation like below .
android:orientation="vertical"
Try this :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayoutSaleListRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:orientation="horizontal"
android:padding="5dp" >
<ImageView
android:id="#+id/imageViewRemoveRowSale"
android:layout_width="#dimen/size_30px"
android:layout_height="#dimen/size_30px"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="#drawable/ic_action_remove"/>
<RelativeLayout
android:id="#+id/relativeLayoutIncQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_toRightOf="#+id/imageViewRemoveRowSale"
android:layout_toLeftOf="#+id/imageViewEditRowSale"
android:orientation="horizontal">
<TextView
android:id="#+id/textViewRowPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="#+id/textViewRowQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textViewRowPrice"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="#+id/textViewRowName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/textViewRowPrice"
android:text="text"
android:minLines="2"/>
<LinearLayout
android:id="#+id/layoutDiscount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textViewRowName"
android:orientation="horizontal">
<TextView
android:id="#+id/textViewRowDiscountAmount"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="#+id/textViewRowAmountWithDiscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:text="TextView" />
</LinearLayout>
</RelativeLayout>
<ImageView
android:id="#+id/imageViewEditRowSale"
android:layout_width="#dimen/size_45px"
android:layout_height="#dimen/size_45px"
android:layout_marginTop="5dp"
android:layout_alignParentRight="true"
android:src="#drawable/ic_action_edit" />
</RelativeLayout>
Hope this helps
make some change in liner layout..
<LinearLayout
android:id="#+id/layoutDiscount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textViewRowQuantity"
android:orientation="horizontal"
android:weightSum="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3">
<TextView
android:id="#+id/textViewRowDiscountAmount"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:singleLine="true"
android:text="TextViewsdfsdfdsgfd" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.7">
<TextView
android:id="#+id/textViewRowAmountWithDiscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:singleLine="true"
android:layout_marginRight="5dp"
android:text="textview" />
</LinearLayout>
</LinearLayout>
You can try out this below code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayoutSaleListRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:orientation="horizontal"
android:padding="5dp" >
<ImageView
android:id="#+id/imageViewRemoveRowSale"
android:layout_width="#dimen/size_30px"
android:layout_height="#dimen/size_30px"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="#drawable/ic_action_remove"/>
<RelativeLayout
android:id="#+id/relativeLayoutIncQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_toRightOf="#+id/imageViewRemoveRowSale"
android:layout_toLeftOf="#+id/imageViewEditRowSale"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rr_layout">
<TextView
android:id="#+id/textViewRowPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="#+id/textViewRowQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textViewRowPrice"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="#+id/textViewRowName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/textViewRowPrice"
android:text="TextView" />
</RelativeLayout>
<LinearLayout
android:id="#+id/layoutDiscount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rr_layout"
android:orientation="horizontal">
<TextView
android:id="#+id/textViewRowDiscountAmount"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="#+id/textViewRowAmountWithDiscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:text="TextView" />
</LinearLayout>
</RelativeLayout>
<ImageView
android:id="#+id/imageViewEditRowSale"
android:layout_width="#dimen/size_45px"
android:layout_height="#dimen/size_45px"
android:layout_marginTop="5dp"
android:layout_alignParentRight="true"
android:src="#drawable/ic_action_edit" /></RelativeLayout>

Table layout xml for leaderboard template - Android

I'm trying to create a layout for a user leader-board but I don't know how to make every "row" equal in proportion.
Here's the list of users:
And this is the code of the child layout (that populates a recyclerview):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/topUsersLeaderboardItem_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="12"
android:orientation="horizontal"
android:paddingBottom="#dimen/margin_extra_small"
android:paddingTop="#dimen/margin_extra_small">
<TextView
android:id="#+id/topUsersLeaderboardItem_position"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:paddingStart="#dimen/margin_small"
android:textColor="#color/colorBlack"
android:layout_weight="2"
android:layout_gravity="center_vertical"
android:textSize="#dimen/font_size_small"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8">
<RelativeLayout
android:id="#+id/topUsersLeaderboardItem_layoutPicUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
<ImageView
android:id="#+id/topUsersLeaderboardItem_userIcon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:src="#drawable/circular_profile"
app:srcCompat="#drawable/circular_profile" />
<TextView
android:id="#+id/topUsersLeaderboardItem_userInitial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center_vertical|center_horizontal|center"
android:text="U"
android:textSize="#dimen/font_size_medium"/>
</RelativeLayout>
<TextView
android:id="#+id/topUsersLeaderboardItem_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_centerVertical="true"
android:gravity="center_vertical|center_horizontal"
android:text="uername"
android:textColor="#color/colorBlack"
android:layout_marginStart="8dp"
android:layout_toEndOf="#+id/topUsersLeaderboardItem_layoutPicUser"
android:textSize="#dimen/font_size_small"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/topUsersLeaderboardItem_scoreLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:gravity="end">
<TextView
android:id="#+id/topUsersLeaderboardItem_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:text="150"
android:textSize="#dimen/font_size_small"
android:textColor="#color/colorBlack" />
<ImageView
android:id="#+id/topUsersLeaderboardItem_starImage"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="false"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/margin_small"
android:layout_toEndOf="#+id/topUsersLeaderboardItem_score"
android:src="#drawable/ic_star_yellow_24dp"
app:srcCompat="#drawable/ic_star_yellow_24dp" />
</RelativeLayout>
As you can see from the picture, the space taken by the numbers on the left increases when I would like it to be fixed. Is there a way to make this sort of "table layout"? Thanks everybody!
Actually, it is because of android:layout_width="wrap_content"
of first TextView, give it some predefined value like android:layout_width="10dp". Don't forget to remove gravity from TextView.
please use this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/topUsersLeaderboardItem_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="12"
android:orientation="horizontal"
android:paddingBottom="#dimen/margin_extra_small"
android:paddingTop="#dimen/margin_extra_small">
<TextView
android:id="#+id/topUsersLeaderboardItem_position"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="0"
android:paddingStart="#dimen/margin_small"
android:textColor="#color/colorBlack"
android:layout_weight="2"
android:layout_gravity="center_vertical"
android:textSize="#dimen/font_size_small"/>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="8">
<RelativeLayout
android:id="#+id/topUsersLeaderboardItem_layoutPicUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
<ImageView
android:id="#+id/topUsersLeaderboardItem_userIcon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:src="#drawable/circular_profile"
app:srcCompat="#drawable/circular_profile" />
<TextView
android:id="#+id/topUsersLeaderboardItem_userInitial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center_vertical|center_horizontal|center"
android:text="U"
android:textSize="#dimen/font_size_medium"/>
</RelativeLayout>
<TextView
android:id="#+id/topUsersLeaderboardItem_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_centerVertical="true"
android:gravity="center_vertical|center_horizontal"
android:text="uername"
android:textColor="#color/colorBlack"
android:layout_marginStart="8dp"
android:layout_toEndOf="#+id/topUsersLeaderboardItem_layoutPicUser"
android:textSize="#dimen/font_size_small"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/topUsersLeaderboardItem_scoreLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2">
<TextView
android:id="#+id/topUsersLeaderboardItem_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:text="150"
android:textSize="#dimen/font_size_small"
android:textColor="#color/colorBlack" />
<ImageView
android:id="#+id/topUsersLeaderboardItem_starImage"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="false"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/margin_small"
android:layout_toEndOf="#+id/topUsersLeaderboardItem_score"
android:src="#drawable/ic_star_yellow_24dp"
app:srcCompat="#drawable/ic_star_yellow_24dp" />
</RelativeLayout>

How to align textView on layout center without conflict with other view?

I have an issue trying align textView to the center of layout: when I try to do this - it has a conflict with a button on the left - if text is too long - it become hidden under the button. Text length can be very different and I need it in the center of layout but not under/on the button.
Here is the xml file of activity layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/item_content_action_bar_layout"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/colorBlue"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/item_content_title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Title"
android:textColor="#color/colorWhite"
android:textAlignment="center"
android:singleLine="true"
android:layout_gravity="center_vertical"
android:textStyle="bold"
android:autoText="false"
android:layout_centerInParent="true"
android:layout_alignWithParentIfMissing="false"
android:layout_alignParentRight="false" />
<ImageButton
android:layout_width="120dp"
android:layout_height="45dp"
android:id="#+id/item_content_back_button"
android:background="#drawable/back_button_selector"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginRight="5dp" />
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/item_content_scroll_view"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:fillViewport="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="125dp"
android:layout_height="125dp"
android:id="#+id/item_content_image"
android:src="#drawable/ic_no_thumbnail"
android:maxHeight="125dp"
android:maxWidth="125dp"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="true"
android:layout_margin="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/item_content_subtitle"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Subtitle"
android:layout_alignTop="#+id/item_content_image"
android:layout_toRightOf="#+id/item_content_image"
android:layout_toEndOf="#+id/item_content_image"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/item_content_pubdate"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Date"
android:layout_below="#+id/item_content_subtitle"
android:layout_alignLeft="#+id/item_content_subtitle"
android:layout_alignStart="#+id/item_content_subtitle"
android:textSize="10dp"
android:layout_marginTop="10dp" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/item_content_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="blabla"
android:layout_below="#+id/item_content_pubdate"
android:layout_alignStart="#+id/item_content_pubdate"
android:layout_alignParentStart="true"
android:layout_margin="10dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Try using LinearLayout with layout-weight for this to avoid conflict. Change your first relativelayout to below one
<LinearLayout
android:id="#+id/item_content_action_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/_white"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="7">
<ImageButton
android:id="#+id/item_content_back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="2"
android:background="#drawable/chemist" />
<TextView
android:id="#+id/item_content_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="5"
android:autoText="false"
android:gravity="center"
android:maxLines="2"
android:singleLine="true"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/Black"
android:textStyle="bold" />
</LinearLayout>
Change Your RelativeLayout to LinearLayout and use android:layout_weight attribute of layout to do this.
<LinearLayout
android:id="#+id/item_content_action_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/_white"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="1"
>
<ImageButton
android:id="#+id/item_content_back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/chemist" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/item_content_title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Title"
android:textColor="#color/colorWhite"
android:textAlignment="center"
android:singleLine="true"
android:layout_gravity="center_vertical"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="1"
android:autoText="false"/>
</LinearLayout>
Make the layout_gravity of the textview item_content_title as "center_horizontal".
And add android:layout_toLeftOf="#+id/item_content_title" under the ImageButton item_content_back_button.
Hope it was what you wanted.
Your Action bar layout has to be like this-
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/item_content_action_bar_layout"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/colorBlue"
android:padding="10dp">
<ImageButton
android:layout_width="120dp"
android:layout_height="45dp"
android:id="#+id/item_content_back_button"
android:background="#drawable/back_button_selector"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dp" />
<TextView
android:toRightOf="#id/item_content_back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/item_content_title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Title"
android:textColor="#color/colorWhite"
android:textAlignment="center"
android:singleLine="true"
android:layout_gravity="center_vertical"
android:textStyle="bold"
android:autoText="false"
android:layout_centerInParent="true" />
</RelativeLayout>

How can I achieve this listview row layout in XML?

I can't seem to get the date positioned appropriately in the layout. I am trying to achieve something like this:
I need to have the date appear below the first line of the notifications. I'm just not sure how to incorporate a vertical linear layout here properly.
Here's the XML code I have so far:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/user_avatar_notification"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp" />
<TextView
android:id="#+id/post_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="username"
android:textSize="16dp" />
<TextView
android:id="#+id/post_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="mentioned you in a"
android:textColor="#color/comment_like_grey" />
<TextView
android:id="#+id/notification_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="not_type"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="date of notification"
android:textColor="#color/comment_like_grey"
/>
</LinearLayout>
Try something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/user_avatar_notification"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/post_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="username"
android:textSize="16dp" />
<TextView
android:id="#+id/post_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="mentioned you in a"
android:textColor="#color/comment_like_grey"/>
<TextView
android:id="#+id/notification_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="not_type" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="date of notification"
android:textColor="#color/comment_like_grey"/>
</LinearLayout>
</LinearLayout>

Prevent a view from getting outside of the screen

I'm working with the Google Maps API v2, and I overrided getInfoContents() from InfoWindowAdapter for having a customized InfoWindow. Inside getInfoContents()I inflate an xml containing an ImageView, and three TextViews.
My problem comes when the text that is set inside the snippet is larger than the view, then the count TV it's set outside the view. This is illustrated above with some screenshots.
Here my xml layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/categoryIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignBottom="#+id/count"
android:src="#drawable/ic_launcher"/>
<LinearLayout
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="#+id/categoryIcon"
android:layout_centerVertical="true"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textStyle="bold" />
<TextView
android:id="#+id/snippet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Snippet" />
</LinearLayout>
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#111177"
android:text="0"
android:textSize="20sp"
android:padding="10dp"
android:textColor="#FFF"
android:layout_toRightOf="#+id/text"
android:singleLine="true" />
</RelativeLayout>
How can I change this layout for getting the count TextView inside of the screen, and the snippet in multiline?
I want to always show the count TextView.
UPDATE:
Finally with the answer of Simon Marquis I realized a way to accomplish this. Here the new code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/categoryIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignBottom="#+id/text"
android:src="#drawable/ic_launcher"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_toRightOf="#+id/categoryIcon">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textStyle="bold" />
<TextView
android:id="#+id/snippet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Snippeta" />
</LinearLayout>
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#111177"
android:text="0"
android:textSize="20sp"
android:padding="10dp"
android:textColor="#FFF"
android:singleLine="true" />
</LinearLayout>
</RelativeLayout>
You should add this to the LinearLayout:
android:layout_width="0dp"
android:layout_weight="1"
The new layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/categoryIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignBottom="#+id/count"
android:src="#drawable/ic_launcher"/>
<LinearLayout
android:id="#+id/text"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="#+id/categoryIcon"
android:layout_centerVertical="true"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textStyle="bold" />
<TextView
android:id="#+id/snippet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Snippet" />
</LinearLayout>
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#111177"
android:text="0"
android:textSize="20sp"
android:padding="10dp"
android:textColor="#FFF"
android:layout_toRightOf="#+id/text"
android:singleLine="true" />
</RelativeLayout>
One solution if you want that the count TextView always appears at right is the following:
Put the count Text View before the LinearLayout and add
android:layout_alignParentRight="true"
And add android:layout_toLeftOf="#id/count" in the Linear Layout
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#00F"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#id/count"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/snippet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

Categories

Resources