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.
Related
I'm trying to create xml for my ListView element.
Here is my *.xml file code:
<?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">
<TextView
android:id="#+id/contactId"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:visibility="gone" />
<TextView
android:id="#+id/contactName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/contactLastName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textSize="15sp"
android:textStyle="bold" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="#+id/contactPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="3dp"
android:textSize="15sp"
android:textStyle="bold" />
</TableRow>
<TableRow>
<TextView
android:id="#+id/contactEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="3dp"
android:textSize="15sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
Every cell will have different value, and below is the picture of what I get, and what I'm trying to do:
layout.xml
Almost sure that solution is to use android:weight, but I have already try almost everything... I'll appreciate any help or hint
Maybe this is what you want to achieve
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="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="#+id/contactId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_blue_bright"
android:gravity="center"
android:text="Hello"
android:visibility="visible" />
<TextView
android:id="#+id/contactName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_green_dark"
android:gravity="center"
android:text="Hello2"
android:textSize="15sp"
android:textStyle="bold" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TableRow>
<TextView
android:id="#+id/contactPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/holo_red_dark"
android:padding="3dp"
android:text="Sample text 1"
android:textSize="15sp"
android:textStyle="bold" />
</TableRow>
<TableRow>
<TextView
android:id="#+id/contactEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/holo_orange_dark"
android:padding="3dp"
android:text="Sample text 2"
android:textSize="15sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
</LinearLayout>
Your linear layout needs to have the weightSum attribute of 100. Then the other elements for example 30 30 40 to achieve what youre looking for.
Try this,
<?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">
<TextView
android:id="#+id/contactName"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:gravity="center"
android:text="TEST NAME"
android:textSize="15sp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/contactLastName"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:gravity="center"
android:text="TEST LAST NAME"
android:textSize="15sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2">
<TextView
android:id="#+id/contactPhone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="0000000000"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/contactEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/contactPhone"
android:padding="3dp"
android:text="test#gmail.com"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
</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>
How can I align "Leave Type" and "Available Leaves" to the center of the list and align their values to the center of the respective heading? Please help me. This is the xml code I have made but it doesn't fullfill my purpose
<?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:layout_gravity="center"
android:background="#drawable/list_selector"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/leave_type_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="left"
android:textColor="#000000" />
<TextView
android:id="#+id/available_leaves_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="right"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left|right">
<TextView
android:id="#+id/leave_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="left"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="#+id/available_leaves"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="right"
android:textColor="#000000"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
Best option is to use weightsum:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightsum="2"
android:orientation="horizontal"
android:gravity="left|center">
<TextView
android:id="#+id/leave_type_header"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="left"
android:textColor="#000000" />
<TextView
android:id="#+id/available_leaves_header"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="right"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightsum="2"
android:gravity="left|center">
<TextView
android:id="#+id/leave_type"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="left"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="#+id/available_leaves"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="left"
android:textColor="#000000"
android:textStyle="bold" />
</LinearLayout>
How can I align "Leave Type" and "Available Leaves" to the center of
the list and align their values to the center of the respective
heading?
Set gravity attribute for both LinearLayout's to center :
android:gravity="center"
replace your code with
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:id="#+id/leave_type_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="type"
android:textColor="#000000" />
<TextView
android:id="#+id/leave_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="#000000"
android:text="casual"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:id="#+id/available_leaves_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="available"
android:textColor="#000000" />
<TextView
android:id="#+id/available_leaves"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="#000000"
android:text="5.0"
android:textStyle="bold" />
</LinearLayout>
You can use android:weight and android:gravity="center" for getting the required 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="wrap_content"
android:layout_gravity="center"
android:background="#drawable/bg_grey"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="#+id/leave_type_header"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Leave Type"
android:textColor="#000000" />
<TextView
android:id="#+id/available_leaves_header"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Available Leaves"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|right"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="#+id/leave_type"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Privilege"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="#+id/available_leaves"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="20.0"
android:textColor="#000000"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
I got the listview item like
since you have use layout_gravity = "left" and layout_gravity = "right"
So you just have to divide them from center. I suggest you should use android:weight
Just add it to your layout tags.
<?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:layout_gravity="center"
android:background="#drawable/list_selector"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/leave_type_header"
android:layout_width="0dp"
android:weight = "1"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:textColor="#000000" />
<TextView
android:id="#+id/available_leaves_header"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:weight = "1"
android:layout_margin="10dp"
android:layout_gravity="center"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/leave_type"
android:layout_width="0dp"
android:weight = "1"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="#+id/available_leaves"
android:layout_width="0dp"
android:weight = "1"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:textColor="#000000"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
Since I am setting android:weight = "1" for both textviews inside LinearLayout it will divide the available horizontal width into half for each, and android:layout_gravity = "center" will set them to center.
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>
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.