Aim : Align my Checkbox's view to the left of my LinearLayout (id=linearlayout) !
The Actual look :
item.xml
<?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="horizontal"
android:layout_gravity="center_vertical"
android:weightSum="14">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="13"
>
<TextView
android:id="#+id/tv_ville_secteur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:gravity="center_vertical"
android:paddingTop="4dp"
android:text="TextView"
android:textColor="#android:color/holo_blue_dark"
android:textSize="26dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left"
android:gravity="left"
>
<CheckBox
android:id="#+id/cb_ville_secteur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26dp"
android:layout_gravity="left"
android:gravity="left"
android:layout_marginRight="2dp"
android:text="text" />
</LinearLayout>
</LinearLayout>
I believe this is what you are looking for. You will need to determine if 100dp is wide enough or too wide for your checkboxes (is it only ever going to be 1 digit, 2 digits, 5 digits?). I also moved margins/padding and gravity to more obvious elements. When using weights, always keep in mind to set the corresponding width or height to 0dp, wrap_content will compete with the weight when the layout is being calculated, and I believe this to be the main reason your checkboxes are not lining up.
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="8dp"
android:paddingRight="2dp" >
<TextView
android:id="#+id/tv_ville_secteur"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="4dp"
android:text="Agadir"
android:textSize="26dp" />
<CheckBox
android:id="#+id/cb_ville_secteur"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="1"
android:textSize="26dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="8dp"
android:paddingRight="2dp" >
<TextView
android:id="#+id/tv_ville_secteur"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="4dp"
android:text="Casablanca"
android:textSize="26dp" />
<CheckBox
android:id="#+id/cb_ville_secteur"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="2"
android:textSize="26dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="8dp"
android:paddingRight="2dp" >
<TextView
android:id="#+id/tv_ville_secteur"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="4dp"
android:text="El Jadida"
android:textSize="26dp" />
<CheckBox
android:id="#+id/cb_ville_secteur"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="3"
android:textSize="26dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="8dp"
android:paddingRight="2dp" >
<TextView
android:id="#+id/tv_ville_secteur"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="4dp"
android:text="Lots of text in this one to stretch the limits of the text view"
android:textSize="26dp" />
<CheckBox
android:id="#+id/cb_ville_secteur"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="999"
android:textSize="26dp" />
</LinearLayout>
</LinearLayout>
Obviously you only need to pull out one row from the code above, I added multiple to show/test the alignment.
Move your checkbox in the same LinearLayout of the TextView, so it will appear at next of the TextView.
<?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="horizontal"
android:layout_gravity="center_vertical"
android:weightSum="14">
<TextView
android:id="#+id/tv_ville_secteur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:gravity="center_vertical"
android:paddingTop="4dp"
android:text="TextView"
android:textColor="#android:color/holo_blue_dark"
android:textSize="26dp" />
<CheckBox
android:id="#+id/cb_ville_secteur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26dp"
android:layout_marginRight="2dp"
android:text="text" />
</LinearLayout>
I removed all the useless LinearLayout wraps, do you really need them?
If for some strange reason the answer is: Yes.
<?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="horizontal"
android:layout_gravity="center_vertical"
android:weightSum="14">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="13"
>
<TextView
android:id="#+id/tv_ville_secteur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:gravity="center_vertical"
android:paddingTop="4dp"
android:text="TextView"
android:textColor="#android:color/holo_blue_dark"
android:textSize="26dp" />
<CheckBox
android:id="#+id/cb_ville_secteur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26dp"
android:layout_gravity="left"
android:gravity="left"
android:layout_marginRight="2dp"
android:text="text" />
</LinearLayout>
</LinearLayout>
Try to set fix size for the layout with checkbox
If you want your CheckBox to the left of your TextView , you should do like this (just swap two LinearLayouts, as you have Horizontal Orientation parent layout,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center_vertical"
android:weightSum="14">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left"
android:gravity="left"
>
<CheckBox
android:id="#+id/cb_ville_secteur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26dp"
android:layout_gravity="left"
android:gravity="left"
android:layout_marginRight="2dp"
android:text="text" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="13"
>
<TextView
android:id="#+id/tv_ville_secteur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:gravity="center_vertical"
android:paddingTop="4dp"
android:text="TextView"
android:textSize="26dp" />
</LinearLayout>
Related
I'm trying to get two linear layout side by side and put content inside of it.
I want the two linear layouts to be side by side, but the left one should take about 3/4 of the screen and the right one the rest of it.
This is my code: https://gitlab.com/snippets/1682040
But the two linear layouts always change the width and the height depending on the content.
How can I have two linear layouts side by side with fixed width and height?
A better solution with RelativeLayout is always welcome.
This is the result:
Use layout_weight property of LinearLayout and android:weightSum property for parent layout.
Do Something like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="15dp"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".75" // For First layout to 3/4
android:background="#color/colorPrimary"
android:gravity="left|center"
android:orientation="vertical">
//First Layout content Here
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".25" // For Second layout to rest of screen
android:background="#color/colorAccent"
android:gravity="center"
android:orientation="vertical">
//Second Layout Content Here
</LinearLayout>
</LinearLayout>
I know that you asked for a solution with the LinearLayout but I suggest you to solve this problem with the ConstraintLayout. You will get incredible performance improvements without nested view.
In this sample I added a vertical guideline at 75% of from the left of the screen. I have only to remove the nested LinearLayouts and add the constraints to your views!
Try it out!
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="628dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="#color/colorPrimary"
android:gravity="left|center"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/repository_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAA"
android:textColor="#color/blue"
android:textSize="20dp" />
<TextView
android:id="#+id/repository_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="AAAAA"
android:textColor="#color/gray"
android:textSize="15dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera" />
<TextView
android:id="#+id/repository_fork_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#color/orange"
android:textSize="20dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:src="#drawable/ic_menu_camera" />
<TextView
android:id="#+id/repository_star_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#color/orange"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="0dp"
android:layout_height="628dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="#color/colorAccent"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline4"
app:layout_constraintTop_toTopOf="parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/user_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAA"
android:textColor="#color/blue" />
<TextView
android:id="#+id/first_last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mock Name"
android:textColor="#color/lightGray" />
</LinearLayout>
<android.support.constraint.Guideline
android:id="#+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.75" />
Try this give Your first LinearLayout android:layout_weight=".75" so it can take 3/4 space in screen
than assign android:layout_weight="0.25" to Your second LinearLayout so it can take remaining space
<?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="horizontal"
android:padding="15dp"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.75"
android:background="#color/colorPrimary"
android:orientation="vertical">
<TextView
android:id="#+id/repository_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAA"
android:textColor="#color/colorBlue"
android:textSize="20dp" />
<TextView
android:id="#+id/repository_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="AAAAA"
android:textColor="#color/colorGreen"
android:textSize="15dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera" />
<TextView
android:id="#+id/repository_fork_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#color/colorPrimary"
android:textSize="20dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:src="#drawable/ic_menu_camera" />
<TextView
android:id="#+id/repository_star_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#color/colorPrimary"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#color/colorAccent"
android:gravity="center"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/user_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAA"
android:textColor="#color/colorBlue" />
<TextView
android:id="#+id/first_last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mock Name"
android:textColor="#color/colorRed" />
</LinearLayout>
</LinearLayout>
Seems like both of the weights are set to "1". Have you tried ".5"?
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorPrimary"
android:layout_weight="0.50"
android:gravity="left|center">
Try this I have added weight
<?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="horizontal"
android:padding="15dp"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".75"
android:background="#color/colorPrimary"
android:gravity="left|center"
android:orientation="vertical">
<TextView
android:id="#+id/repository_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAA"
android:textColor="#color/blue"
android:textSize="20dp" />
<TextView
android:id="#+id/repository_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="AAAAA"
android:textColor="#color/gray"
android:textSize="15dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera" />
<TextView
android:id="#+id/repository_fork_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#color/orange"
android:textSize="20dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:src="#drawable/ic_menu_camera" />
<TextView
android:id="#+id/repository_star_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#color/orange"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".25"
android:background="#color/colorAccent"
android:gravity="center"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/user_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAA"
android:textColor="#color/blue" />
<TextView
android:id="#+id/first_last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mock Name"
android:textColor="#color/lightGray" />
</LinearLayout>
</LinearLayout>
First remove weightSum attribute from parent layout.
If you want your first layout to be 3/4 of you should put weight 0.75 and 0.25 (1/4) for the other one like here:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorPrimary"
android:layout_weight=".75"
android:layout_gravity="left"
android:gravity="left|center">
<TextView
android:id="#+id/repository_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAA"
android:textColor="#color/blue"
android:textSize="20dp"/>
<TextView
android:id="#+id/repository_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:ellipsize="end"
android:text="AAAAA"
android:textColor="#color/gray"
android:textSize="15dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera"/>
<TextView
android:id="#+id/repository_fork_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#color/orange"
android:textSize="20dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera"
android:layout_marginLeft="15dp"/>
<TextView
android:id="#+id/repository_star_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#color/orange"
android:textSize="20dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorAccent"
android:layout_weight=".25"
android:gravity="center">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/user_image"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAA"
android:textColor="#color/blue"/>
<TextView
android:id="#+id/first_last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mock Name"
android:textColor="#color/lightGray"/>
</LinearLayout>
try this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".75"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000"
android:textSize="16dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shape_txt"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".25"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000"
android:textSize="16dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shape_txt"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
Change the weight sum to 4 in the parent layout and add weight as 3 for child linear layout so it can take 3/4 space and 1 to child linear layout so that it will take the remaining space.
<?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="horizontal"
android:padding="15dp"
android:weightSum="4">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#color/colorPrimary"
android:gravity="left|center"
android:orientation="vertical">
//Your contents
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/colorAccent"
android:gravity="center"
android:orientation="vertical">
//Your contents
</LinearLayout>
</LinearLayout>
I am trying to align two TextViews OR LinearLayouts side by side in order to reach this cardView:
I have used layout_gravity and layout_weight attributes inorder to solve this situation, but with no luck.
I am open to any other suggestions that may accomplish my goal as log as I can generate cardview dynamicaly using BaseAdapter with ListView in the Activity Code.
I am looking for XML solution.
Here is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:padding="5dp">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="5sp"
card_view:cardElevation="5sp"
card_view:contentPadding="16dp"
android:orientation="horizontal"
android:layout_weight="2">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="2">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="left"
android:gravity="left"
android:layout_weight="1">
<TextView
style="#style/Base.TextAppearance.AppCompat.Headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16dp"
android:textColor="#ec1c24"
android:text="Model Name" />
<TextView
style="#style/Base.TextAppearance.AppCompat.Body1"
android:id="#+id/plc_ModelName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="36dp"
android:textColor="#666666"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="right"
android:gravity="right"
android:layout_weight="1">
<TextView
android:id="#+id/plc_localIp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp" />
<TextView
android:id="#+id/plc_machineType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16dp" />
<TextView
android:id="#+id/plc_ModelId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16dp" />
<TextView
android:id="#+id/plc_Version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Many thanks.
For the part on the right you can use a TableLayout.
Try to play with nesting layouts or switch to ConstraintLayout for better performance. Too many nested layout weights will quickly add up and create performance issues.
You can start fine tuning from the XML provided below. I removed the layout_weight from the CardView, if needed, add it back.
In preview it looks like this:
Layout XML:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal"
card_view:cardCornerRadius="5sp"
card_view:cardElevation="5sp"
card_view:contentPadding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
style="#style/Base.TextAppearance.AppCompat.Headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Model Name"
android:textColor="#ec1c24"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="#+id/plc_ModelName"
style="#style/Base.TextAppearance.AppCompat.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#666666"
android:textSize="36dp"
android:textStyle="bold"
tools:text="T-Max" />
</LinearLayout>
<TableLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="Model ID:"
android:textColor="#ec1c24"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="#+id/plc_ModelId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:textSize="14dp"
tools:text="12345678" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="IP:"
android:textColor="#ec1c24"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="#+id/plc_localIp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:textSize="14dp"
tools:text="192.168.0.1" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="BS Version:"
android:textColor="#ec1c24"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="#+id/plc_Version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:textSize="14dp"
tools:text="3.0.2.111" />
</TableRow>
</TableLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
I would like to make the text in "Status" text view centered horizontally and vertically. It works in HTC ONE X+ but not in Samsung Tab S and Huawei Honor 6 Plus such larger screen. How can I make the text centered in all screen?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/ServiceUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="24sp"
android:text="" />
<TextView
android:id="#+id/Task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/Status"
android:layout_toStartOf="#id/Status"
android:layout_toRightOf="#id/ServiceUser"
android:layout_toEndOf="#id/ServiceUser"
android:gravity="center_horizontal"
android:textSize="24sp"
android:text="" />
<TextView
android:id="#+id/Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#id/ServiceUser"
android:layout_alignEnd="#id/ServiceUser"
android:layout_below="#id/ServiceUser"
android:textSize="24sp"
android:text="" />
<TextView
android:id="#+id/Remark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/Task"
android:layout_alignStart="#id/Task"
android:layout_alignRight="#id/Task"
android:layout_alignEnd="#id/Task"
android:layout_below="#id/Task"
android:gravity="center_horizontal"
android:textSize="24sp"
android:text="" />
<TextView
android:id="#id/Status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/Task"
android:layout_alignBottom="#id/Time"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:gravity="center"
android:textSize="24sp"
android:text="" />
</RelativeLayout>
I think it would be easier to use nested LinearLayouts in this case. The outermost one here could be omitted but gives you a place for the main content.
<?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">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="0.33"
android:layout_height="wrap_content">
<TextView
android:id="#+id/ServiceUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="ServiceUser" />
<TextView
android:id="#+id/Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Time" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="0.33"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="#+id/Task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Task" />
<TextView
android:id="#+id/Remark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Remark" />
</LinearLayout>
<TextView
android:id="#+id/Status"
android:layout_width="0dp"
android:layout_weight="0.33"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="24sp"
android:text="Status" />
</LinearLayout>
<!-- More content here -->
</LinearLayout>
I've searched but couldn't figure out the problem.
I want to make exactly like the above picture. I've used linear layout in vertical for the "Day Teams Score Status"
And then for the content containing the values of above headings, I've used a listview.
What I've achieved is that headings "Day Teams Score Status" are being shown with spaces but the contents ( which are in seperate .xml file ) are not being shown with spaces, so what I'm getting is:
The adapter xml file for the list view is:
adapter_my_matches.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Date"
android:id="#+id/textViewDate"
android:layout_weight="0.1"
android:layout_gravity="center_vertical" />
<LinearLayout
android:id="#+id/ll"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="0.4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Teams"
android:id="#+id/textViewTeams1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Teams"
android:id="#+id/textViewTeams2"/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Score"
android:id="#+id/textViewScore"
android:layout_weight="0.1"
android:layout_gravity="center_vertical" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Status"
android:id="#+id/textViewStatus"
android:layout_weight="0.2"
android:layout_gravity="center_vertical" />
</LinearLayout>
I think this is the layout you are looking for.Let me know if anything going bad. Here the weight i set for a sample, you can change it according to your need.
<?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"
>
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum= "6">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Date"
android:id="#+id/textViewDate"
android:layout_weight="1.2"
android:gravity="center" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Teams"
android:id="#+id/textViewTeams1"
android:gravity="center"
android:layout_weight="2"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Score"
android:id="#+id/textViewScore"
android:layout_weight="1.4"
android:gravity="center"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Status"
android:id="#+id/textViewStatus"
android:layout_weight="1.4"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum= "6">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="12 Feb"
android:id="#+id/textViewDate1"
android:layout_weight="1.2"
android:gravity="center" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="IND VS PAK"
android:id="#+id/textViewTeams11"
android:gravity="center"
android:layout_weight="2"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="256"
android:id="#+id/textViewScore1"
android:layout_weight="1.4"
android:gravity="center"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="WIN"
android:id="#+id/textViewStatus1"
android:layout_weight="1.4"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
Add this to your linearlayout code
android:weightSum="0.8"
Use this One :
<?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="horizontal"
android:weightSum="8" >
<TextView
android:id="#+id/textViewDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="Date"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:id="#+id/ll"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="vertical" >
<TextView
android:id="#+id/textViewTeams1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Teams"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textViewTeams2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Teams"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<TextView
android:id="#+id/textViewScore"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="Score"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textViewStatus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:text="Status"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
give layout_weight to parent layout and give weight to child element and make sure that layout_weight should be sum of its all child's weight total. make width of child as warp_content.
I have relative layout and inside it there are two linear layouts
the program works without the first linear layout. can any one explain why ?
<?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="match_parent"
android:background="#drawable/bngp" >
<TextView
android:id="#+id/Cart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:paddingTop="10dp"
android:text="Cart"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:textStyle="bold" />
// this is the linear layout causes error
<LinearLayout
android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/Cart"
android:orientation="horizontal" >
<TextView
android:id="#+id/Product"
android:layout_height="wrap_content"
android:layout_weight="6"
android:text="Product"
android:textColor="#000000" />
<TextView
android:id="#+id/Quantity"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="Quantity"
android:textColor="#000000" />
<TextView
android:id="#+id/Price"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="Price"
android:textColor="#000000" />
<TextView
android:id="#+id/Value"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="Value"
android:textColor="#000000" />
</LinearLayout>
// end of linear layout
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/textView1"
android:layout_below="#id/table" />
<TextView
android:id="#id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linear"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:text="Total Value: "
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/Final_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/linear"
android:layout_toRightOf="#id/textView1"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:textColor="#FFFFFF" />
<LinearLayout
android:id="#id/linear"
style="#android:style/ButtonBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Confirm" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clear" />
</LinearLayout>
</RelativeLayout>
Check your layout now. I have edited some lines. use #+id instead of #id. You must know the difference between #id , #+id and #android:id.
ie,
"#android:id" which means you are referencing an item in Android namespace.
"#id" means you have defined ids in your application itself,
eg:-
===========================================================
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="TextView1" type="id"/>
</resources>
in this case you have defined a textview id in your resources. Now you can use ,
<TextView
android:id="#id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
===========================================================
"#+id" means you are created a view (textview , layouts , etc..) in your layout and you wanted to add the id to R.java.
Check your layout now,
<?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="match_parent"
android:background="#drawable/bngp" >
<TextView
android:id="#+id/Cart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:paddingTop="10dp"
android:text="Cart"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/Cart"
android:orientation="horizontal" >
<TextView
android:id="#+id/Product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="6"
android:text="Product"
android:textColor="#000000" />
<TextView
android:id="#+id/Quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="Quantity"
android:textColor="#000000" />
<TextView
android:id="#+id/Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="Price"
android:textColor="#000000" />
<TextView
android:id="#+id/Value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="Value"
android:textColor="#000000" />
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/textView1"
android:layout_below="#+id/table" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linear"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:text="Total Value: "
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/Final_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linear"
android:layout_toRightOf="#+id/textView1"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:textColor="#FFFFFF" />
<LinearLayout
android:id="#+id/linear"
style="#android:style/ButtonBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Confirm" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clear" />
</LinearLayout>
</RelativeLayout>
For starters, instead of using #id/blah, always use #+id/blah (even if you are not defining the view). It doesn't hurt, and can prevent some really hard to track down errors.
For more details, please provide the error you are receiving.