How to adjust horizontal views in layout - android

I am trying to adjust the sizes of my textviews based on the size of the device. However it's partial adjustments. Let me show you what i mean:
The following picture depicts the desired effect. TextView B length determines the length of textview A, that is to say that part C is completely independent and is at the end with fixed size. If the textview A gets it's size reduced,then the end is elipsized. I have tried shifting from using LinearLayout with weights as well as RelativeLayout but haven't achieved the desired effect. Thoughts?
What i have tried:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foobar="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingBottom="3dp"
android:paddingTop="3dp">
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ddd"
android:scaleType="centerCrop"
android:visibility="invisible" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.ctrlplusz.anytextview.AnyTextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/input"
android:ellipsize="end"
android:maxLength="20"
android:maxLines="1"
android:paddingLeft="7dp"
android:singleLine="true"
android:text="cshbcadhbhjvbbvjfbvcsdhabcsdjbvjsdbvjsfdbvsfnvk"
android:textColor="#color/black"
android:textSize="20dp"
foobar:typeface="Roboto-Light.ttf" />
<!--android:text="cshbcadhbhjvbbvjfbvcsdhabcsdjbvjsdbvjsfdbvsfnvk"-->
<RelativeLayout
android:id="#+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical">
<com.ctrlplusz.anytextview.AnyTextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/toggle"
android:gravity="end"
android:paddingRight="4dp"
android:text="3900"
android:textColor="#color/gray"
android:textSize="20dp"
foobar:typeface="Roboto-Light.ttf" />
<RelativeLayout
android:id="#+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:gravity="end"
android:orientation="horizontal">
<RelativeLayout
android:id="#+id/minus"
android:layout_width="38dp"
android:layout_height="35dp"
android:layout_centerVertical="true"
android:background="#drawable/minus_product_icon">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_minus" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/add"
android:layout_width="38dp"
android:layout_height="35dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/num"
android:background="#drawable/add_product_icon">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_add_icon" />
</RelativeLayout>
<EditText
android:id="#+id/num"
android:layout_width="40dp"
android:layout_height="35dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/minus"
android:background="#drawable/product_custom_edit_text"
android:digits="1234567890"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
android:padding="2dp"
android:singleLine="true"
android:text="0"
android:textColor="#000"
android:textCursorDrawable="#drawable/mycursor" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
<com.ctrlplusz.anytextview.AnyTextView
android:id="#+id/p"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="11dp"
android:textColor="#color/gray"
android:textSize="20dp"
foobar:typeface="Roboto-Light.ttf" />
</RelativeLayout>

Something like this?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<TextView
android:id="#+id/a"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue"
android:textColor="#android:color/white"
android:text="hi dear"
android:layout_toLeftOf="#+id/b"/>
<TextView
android:id="#+id/b"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/black"
android:textColor="#android:color/white"
android:text="hello world"
android:layout_toLeftOf="#+id/c"/>
<TextView
android:id="#+id/c"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:textColor="#android:color/white"
android:text="hi darling"
android:background="#color/green"/>
</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<TextView
android:id="#+id/a"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/blue"
android:textColor="#android:color/white"
android:text="hi dear"
/>
<TextView
android:id="#+id/b"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/black"
android:textColor="#android:color/white"
android:text="hello world"
android:layout_toRightOf="#+id/a"
android:layout_toLeftOf="#+id/c"/>
<TextView
android:id="#+id/c"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:textColor="#android:color/white"
android:text="hi darling"
android:background="#color/green"/>
</RelativeLayout>
As B Is Dependent On A,This Layout Will Work Fine For You. :)

Related

How to place a Textview to the right and center of the Imageview in Relative Layout

I want to place a textview to the right and gravity centre of the Image View. I am using Relative Layout for that. Using Relative Layout, i am able to set the Textview to the right side of the Image view.But not able to set it to the centre. I mean here center is not inside of the Imageview, i want to place Textview to the right centre of Imageview(outside centre of Imageview).I am not able to set to centre.
Anybody please help me out.I want to do this only in relative layout.
Here is my xml,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_width="match_parent">
<TextView
android:id="#+id/name_text_view"
android:textSize="20sp"
android:layout_toRightOf="#id/image_view"
android:layout_width="wrap_content"
android:textColor="#color/black"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
<ImageView
android:id="#+id/image_view"
android:layout_marginLeft="30dp"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:id="#+id/button"
android:layout_centerHorizontal="true"
android:background="#color/blue"
android:textColor="#color/color_white"
android:layout_marginTop="20dp"
android:layout_below="#id/name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/val_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:layout_marginTop="20dp"
android:textSize="18sp"
android:layout_centerHorizontal="true"
android:layout_below="#id/button"/>
</RelativeLayout>
You can do it with this code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp">
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="center"
android:src="#drawable/ic_launcher_background" />
<TextView
android:id="#+id/name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/image_view"
android:layout_centerInParent="true"
android:text="hello world"
android:textColor="#color/black"
android:textSize="12sp" />
</RelativeLayout>
Use this code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<ImageView
android:id="#+id/image_view"
android:layout_marginLeft="30dp"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="#+id/name_text_view"
android:textSize="20sp"
android:layout_toRightOf="#id/image_view"
android:layout_width="wrap_content"
android:textColor="#color/black"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_centerHorizontal="true"
android:background="#color/blue"
android:textColor="#color/color_white"
android:layout_marginTop="20dp"
android:layout_below="#id/name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/val_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:layout_marginTop="20dp"
android:textSize="18sp"
android:layout_centerHorizontal="true"
android:layout_below="#id/button"/>
</RelativeLayout>
You can add this in your textview
android:layout_centerInParent="true"
Do You want this view? then try this code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_width="match_parent">
<LinearLayout
android:id="#+id/leaner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="#+id/image_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:contentDescription="TODO"
app:srcCompat="#mipmap/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:textColor="#android:color/black"
android:textSize="20sp" />
<TextView
android:id="#+id/val_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Value"
android:textColor="#android:color/black"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/leaner1"
android:layout_marginTop="1dp"
android:background="#android:color/holo_blue_dark"
android:text="button"
android:textColor="#android:color/white" />
</RelativeLayout>

Format issue placing two views side by side in RelativeLayout

I have two views side by side inside a relative layout. I want the format of both views to be like the one on the left ("Today's Special"). I've assigned both views the same attributes though they are different.
here is my xml.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="ExtraText">
<RelativeLayout
android:id="#+id/view_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/bg_row_background">
<ImageView
android:id="#+id/delete_icon"
android:layout_width="#dimen/ic_delete"
android:layout_height="#dimen/ic_delete"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/padd_10"
android:contentDescription="#string/deleteIcon"
android:src="#drawable/ic_delete_white_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/padd_10"
android:layout_toStartOf="#id/delete_icon"
android:text="#string/delete"
android:textColor="#fff"
android:textSize="12sp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/view_foreground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:padding="#dimen/padd_5">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="#dimen/ic_delete"
android:background="#color/description"
android:textColor="#color/item_name"
android:textSize="12sp" />
<TextView
android:id="#+id/namecat"
android:layout_width="wrap_content"
android:layout_height="#dimen/ic_delete"
android:layout_alignParentEnd="true"
android:layout_toEndOf="#id/name"
android:background="#color/description"
android:paddingStart="#dimen/padd_10"
android:textColor="#color/item_name"
android:textSize="12sp" />
</RelativeLayout>
</FrameLayout>
Thanks for your help.
Replace FrameLayout with LinearLayout and weightsum property
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
tools:ignore="ExtraText">
<RelativeLayout
android:id="#+id/view_background"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#978c8c">
<ImageView
android:id="#+id/delete_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_lock" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="#id/delete_icon"
android:text="#string/dummy_button"
android:textColor="#fff"
android:textSize="12sp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/view_foreground"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#android:color/white">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#color/colorAccent"
android:textColor="#color/colorPrimary"
android:textSize="12sp" />
<TextView
android:id="#+id/namecat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_toEndOf="#id/name"
android:background="#color/colorAccent"
android:textColor="#color/colorPrimaryDark"
android:textSize="12sp" />
</RelativeLayout>
</LinearLayout>

Two LinearLayouts side by side, one with minimum-width

I got a RelativeLayout and inside there are two LinearLayouts side by side. Now I want that the right one has a minimum width of 125dp. So when the left LinearLayout gets too big, it should stop before the other LinearLayout and not push that one away.
How can I achieve this? This is my current approach:
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/item_list_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<ImageButton
android:id="#+id/basket_item_list_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#android:color/transparent"
android:minWidth="50dp"
android:minHeight="50dp"
app:srcCompat="#drawable/ic_garbage"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_toRightOf="#+id/basket_item_list_delete"
android:layout_toEndOf="#+id/basket_item_list_delete">
<LinearLayout
android:id="#+id/basket_item_list_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/basket_item_list_itemname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:text="ItemName"
android:textColor="#color/generalText"
android:textSize="18sp" />
<TextView
android:id="#+id/basket_item_list_options"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:text="Options"/>
</LinearLayout>
<LinearLayout
android:id="#+id/priceLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="125dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:layout_toRightOf="#id/basket_item_list_description">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/basket_item_list_increment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/basket_item_list_count"
android:layout_toStartOf="#+id/basket_item_list_count"
android:minWidth="45dp"
android:minHeight="45dp"
android:text="+"
android:textSize="20sp"
android:textColor="#color/colorPrimary"
android:background="#android:color/transparent" />
<TextView
android:id="#+id/basket_item_list_count"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="false"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/basket_item_list_decrement"
android:layout_toStartOf="#+id/basket_item_list_decrement"
android:gravity="center_horizontal"
android:text="nx"
android:textColor="#color/generalText"
android:textSize="14sp" />
<Button
android:id="#+id/basket_item_list_decrement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:minWidth="45dp"
android:minHeight="45dp"
android:text="–"
android:textSize="20sp"
android:textColor="#color/colorPrimary"
android:background="#android:color/transparent" />
</RelativeLayout>
<TextView
android:id="#+id/basket_item_list_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:text="1€"
android:gravity="right"
android:layout_gravity="right"
android:textColor="#color/generalText"
android:textSize="14sp" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I want to have it like this:
But this is what happens, when the left LinearLayout gets too big:
Try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/item_list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<ImageButton
android:id="#+id/basket_item_list_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:background="#android:color/transparent"
android:minHeight="50dp"
android:minWidth="50dp"
app:srcCompat="#drawable/ic_menu_gallery" />
<LinearLayout
android:id="#+id/basket_item_list_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/basket_item_list_itemname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:text="ItemName"
android:textColor="#color/colorPrimary"
android:textSize="18sp" />
<TextView
android:id="#+id/basket_item_list_options"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:text="Options" />
</LinearLayout>
<LinearLayout
android:id="#+id/priceLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:gravity="right"
android:minWidth="125dp"
android:orientation="vertical"
android:paddingBottom="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="#+id/basket_item_list_increment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:minHeight="45dp"
android:minWidth="45dp"
android:text="+"
android:textColor="#color/colorPrimary"
android:textSize="20sp" />
<TextView
android:id="#+id/basket_item_list_count"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="nx"
android:textColor="#color/colorPrimary"
android:textSize="14sp" />
<Button
android:id="#+id/basket_item_list_decrement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:minHeight="45dp"
android:minWidth="45dp"
android:text="–"
android:textColor="#color/colorPrimary"
android:textSize="20sp" />
</LinearLayout>
<TextView
android:id="#+id/basket_item_list_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="center"
android:minWidth="50dp"
android:text="1€"
android:textColor="#color/colorPrimary"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
Let try this ,
It will divide a screen Half and Half.
<LinearLayout
android:id="#+id/priceLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/priceLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="vertical">
///Your views
</LinearLayout>
<LinearLayout
android:id="#+id/priceLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="vertical">
///Your views
</LinearLayout>
</LinearLayout>
If You want first Wrap another one take remaining width take this
<LinearLayout
android:id="#+id/priceLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/priceLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
///Your views
</LinearLayout>
<LinearLayout
android:id="#+id/priceLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
///Your views
</LinearLayout>
</LinearLayout>

Cannot right align a checkbox in android studio

I am trying to design as per the above image. I tried with Grid Layout and also tried with nested Linear Layout. In both ways checkbox is not right aligned. It is displaying just after the text1
Can someone help on this.
The code is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="2dp"
tools:context="com.example.user.recycleview.MainActivity">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="3">
<ImageView
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:layout_row="0"
android:layout_column="0"
android:layout_rowSpan="2"
android:src="#drawable/cappuccino"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="1"
android:layout_margin="8dp"
android:maxLines="3"
android:text="#string/cappuccino"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp"
android:layout_row="1"
android:layout_column="1"
android:layout_margin="8dp"
android:maxLines="1"
android:text="#string/price"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"
android:layout_row="0"
android:layout_column="2"
android:layout_rowSpan="2" />
</GridLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:background="#color/colorPrimary"/>
</LinearLayout>
With the above code it is displaying as in the below:
If the Text1 part is too long, the checkbox is going out of the screen, as in the below image
You can achieve this by using a single RelativeLayout.
Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="80dp"
android:padding="2dp"
tools:context="com.example.user.recycleview.MainActivity">
<ImageView
android:id="#+id/image"
android:layout_width="80dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:scaleType="centerCrop"
android:src="#drawable/somepngfile"/>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"/>
<TextView
android:id="#+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/image"
android:layout_toLeftOf="#id/checkbox"
android:layout_margin="8dp"
android:maxLines="2"
android:ellipsize="end"
android:text="This is two line text for your large title This is two line text for your large title"/>
<TextView
android:id="#+id/text_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/text_title"
android:layout_alignLeft="#id/text_title"
android:layout_alignRight="#id/text_title"
android:maxLines="1"
android:text="Price: 150.00"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary"/>
</RelativeLayout>
OUTPUT:
You can use layout_weight to align checkbox right,
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<TextView
android:id="#+id/itemTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<CheckBox
android:id="#+id/checkItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /></LinearLayout>
You can use relative layout for you list item . Try below code
<ImageView
android:id="#+id/imageView"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"/>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:gravity="end" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/checkbox"
android:layout_toRightOf="#id/imageView"
android:layout_weight=".8"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_margin="8dp"
android:layout_row="0"
android:maxLines="3"
android:text="#string/cappuccino" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_margin="8dp"
android:layout_marginTop="-5dp"
android:layout_row="1"
android:maxLines="1"
android:text="#string/price" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:background="#color/colorPrimary" />
This is may helpful to you.
<?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="vertical"
android:padding="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center">
<ImageView
android:id="#+id/imageView"
android:layout_width="120dp"
android:layout_height="70dp"
android:scaleType="fitXY"
android:src="#drawable/ic_image" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/imageView"
android:layout_toStartOf="#+id/checkBox"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_margin="8dp"
android:layout_row="0"
android:maxLines="3"
android:text="Sample Header" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_margin="8dp"
android:layout_row="1"
android:maxLines="1"
android:text="Sample Desc" />
</LinearLayout>
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:background="#color/colorPrimary" />
</LinearLayout>

Vertically Center a TextView to the right of a FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/container"
android:background="?android:attr/selectableItemBackground"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="wrap_content"
android:id="#+id/frame"
android:layout_marginLeft="16dp"
android:layout_marginTop="18dp"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:minHeight="5dip"
android:minWidth="5dip"
android:maxWidth="20dip"
android:maxHeight="20dip"
android:id="#+id/progress_bar"
android:visibility="visible"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/cover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<TextView
android:id="#+id/changeinscore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.00"
android:shadowColor="#000000"
android:shadowDx="1"
android:shadowDy="1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:shadowRadius="7"
android:textSize="12sp"
android:textColor="#color/background"
android:fontFamily="sans-serif-condensed"/>
</RelativeLayout>
</FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/middle"
android:layout_toRightOf="#+id/frame"
android:layout_centerVertical="true"
android:text="TextView"
/>
</RelativeLayout>
I want to vertically center the middle TextView to the right of the FrameLayout, as the changeinscore TextView is inside the FrameLayout, if that makes sense. Currently it's off center because the centerVertical is centering it in the entire RelativeLayout, not specifically to the FrameLayout, if I understand it correctly. Basically I want the middle textview to be empty but to actually anchor other textviews so I can have 2 textviews slightly offcenter to the framelayout/imageview.
If I follow what San says, here is the result, but not exactly what I want
I finally got what you wanted and here is my sample of it.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/container"
android:background="?android:attr/selectableItemBackground"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/frame"
android:layout_margin="10dp"
android:gravity="center">
<ImageView
android:id="#+id/cover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/im"
/>
<TextView
android:id="#+id/changeinscore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.00"
android:shadowColor="#000000"
android:shadowDx="1"
android:shadowDy="1"
android:visibility="gone"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:shadowRadius="7"
android:textSize="12sp"
android:layout_margin="5dp"
android:textColor="#color/background"
android:fontFamily="sans-serif-condensed"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:minHeight="5dip"
android:minWidth="5dip"
android:maxWidth="20dip"
android:maxHeight="20dip"
android:layout_centerInParent="true"
android:id="#+id/progress_bar"
android:visibility="visible"/>
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_alignTop="#+id/frame"
android:layout_alignBottom="#+id/frame"
android:layout_toRightOf="#id/frame"
android:gravity="center"
android:weightSum="3"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aldnoah.Zero"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:paddingLeft="5dp"
android:gravity="bottom"
android:fontFamily="sans-serif-slab"
android:textSize="16sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/middle"
android:text="TextView"
android:layout_weight="1"
android:paddingLeft="5dp"
android:textSize="14sp"
/>
<TextView
android:id="#+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Bottom Text"
android:paddingLeft="5dp"
android:textSize="14sp"
android:gravity="top"
android:fontFamily="sans-serif-condensed"/>
</LinearLayout>
</RelativeLayout>
And the layout looks like this

Categories

Resources