How do I align values inside my listview list item? - android

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.

Related

How can I place the text on the image in variable devices?

Hi, I make application with this Image.
I want place the text on red dot points at lines left side and right side.
So I place the text on point, but when I change the device Galaxy S6 to S8,
text placement depart from lines.
I seperate the lines and put weight on it, but still out.
How I can place the text on that image in variable device?
Please help me.
This is my layout.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:src="#drawable/nursecategory"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!--first line-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="32dp"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:textColor="#color/colorCategory"
android:textStyle="bold"
android:layout_alignParentRight="true"
/>
<TextView
android:id="#+id/nursetree_textview_Date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="6dp"
android:textAlignment="center"/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:textColor="#color/colorCategory"
android:textStyle="bold" />
<TextView
android:id="#+id/nursetree_textview_Age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="6dp"
android:textAlignment="center"
/>
</LinearLayout>
</LinearLayout>
<!--second line-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="24dp"
>
<!--Name-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textColor="#color/colorCategory"
android:textStyle="bold" />
<TextView
android:id="#+id/nursetree_textview_Agency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="6dp"
android:textAlignment="center"
/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<!--width-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Width"
android:textColor="#color/colorCategory"
android:textStyle="bold" />
<TextView
android:id="#+id/nursetree_textview_Width"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="6dp"
android:textAlignment="center"
/>
</LinearLayout>
</LinearLayout>
<!--third line-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="26dp"
>
<!--place-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Place"
android:textColor="#color/colorCategory"
android:textStyle="bold" />
<TextView
android:id="#+id/nursetree_textview_Place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="6dp"
android:textAlignment="center"
/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<!--heigth-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Height"
android:textColor="#color/colorCategory"
android:textStyle="bold" />
<TextView
android:id="#+id/nursetree_textview_Height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="6dp"
android:textAlignment="center"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
use Relativelayout as parent layout then try to design your view.
in Relaytivelayout we can add layouts as tray. and it easily to handle.
also read about Relativlayout.

Android XML code : how to place an image left of Textview?

In my Android Application i have 2 textviews and 1 image to e displayed on the listview. While i used my imageview it is going to the new line..
I want one textview and imageview on the same line. Every time i place the imageview the whole alignment is changing ..
my XML code is
<?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="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="left"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/album1" />
<TextView
android:layout_toRightOf="#id/icon"
android:id="#+id/textViewSongName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#android:color/white"
android:textSize="22dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textViewArtist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_weight="1"
android:textColor="#android:color/white"
android:textSize="15dp" />
<TextView
android:id="#+id/textViewDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_marginLeft="10dp"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
use drawableleft in your xml of textview
android:drawableLeft="#drawable/settingmenuright"
you can use this layout i have set weightSum property.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3">
<ImageView
android:id="#+id/icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.7">
<TextView
android:layout_toRightOf="#id/icon"
android:id="#+id/textViewSongName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="iconTextview"
android:textColor="#android:color/background_dark"
android:textSize="22dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<TextView
android:id="#+id/textViewArtist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="textViewArtist"
android:textColor="#android:color/background_dark"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<TextView
android:id="#+id/textViewDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/background_dark"
android:layout_marginLeft="10dp"
android:text="textViewDuration"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
The layout messes up because you have the ImageView and the TextView in a vertical LinearLayout. Do this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="left"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/album1" />
<TextView
android:layout_toRightOf="#id/icon"
android:id="#+id/textViewSongName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#android:color/white"
android:textSize="22dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textViewArtist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_weight="1"
android:textColor="#android:color/white"
android:textSize="15dp" />
<TextView
android:id="#+id/textViewDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_marginLeft="10dp"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
I have moved the ImageView and TextView in another horizontal LinearLayout

Can't adjust weights in linear layout - android

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.

How to fill the layout to entire available width in Android

While developing a layout today I found some weird thing my Linear layout is not filling the available space .
//Image removed
My left layout is re-sizing according to content but i don't want that. What I want is the right edge of left hand side layout should touch the left edge of right layout.
I have tried both fill parent and wrap content with layout_weight but nothing is happening what i want is something like this.
// Image removed
My layout code :
<?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:paddingTop="6dp"
android:paddingBottom="6dp"
android:paddingLeft="7dp"
android:paddingRight="7dp">
<RelativeLayout
android:id="#+id/total_top_layout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:paddingBottom="10dp"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/ammount_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="First Text Here"
android:textSize="20sp"
android:layout_centerVertical="true" />
<EditText
android:id="#+id/total_ammount_input"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:singleLine="true"
android:layout_alignParentRight="true"
android:background="#color/hologreennewdark"
android:text="second Text"
android:layout_centerVertical="true"
android:padding="8dp"
android:gravity="center"
android:textColor="#color/WhiteSmoke" />
</RelativeLayout>
<View
android:id="#+id/divideaftertotal"
android:layout_width="#dimen/divide_width"
android:layout_height="#dimen/divider_height"
android:background="#color/YellowGreen"
android:layout_below="#+id/total_top_layout"
/>
<RelativeLayout
android:id="#+id/tens_view_top"
android:layout_below="#id/divideaftertotal"
android:layout_height="75dp"
android:layout_width="wrap_content"
>
<LinearLayout
android:id="#+id/tens_view_left"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
>
<LinearLayout
android:id="#+id/firstcoloumn"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:id="#+id/amount_rupee"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Num1"
android:layout_centerVertical="true"
android:textSize="25sp" />
<TextView
android:id="#+id/multiply_sign"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="X"
android:layout_centerVertical="true"
android:textSize="26sp"
android:layout_toRightOf="#id/amount_rupee"
android:layout_marginLeft="40dp" />
<TextView
android:id="#+id/multiple_digit"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Num2"
android:layout_centerVertical="true"
android:textSize="25sp"
android:layout_toRightOf="#+id/multiply_sign"
android:layout_marginLeft="40dp" />
</LinearLayout>
<SeekBar
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#+id/multiple_digit"
android:layout_marginTop="8dp"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_height="100dp"
android:layout_width="wrap_content"
android:background="#color/hologreendark"
android:layout_alignParentRight="true">
<TextView
android:id="#+id/amount_rupee"
android:layout_height="fill_parent"
android:layout_width="100dp"
android:text="Num3"
android:layout_centerVertical="true"
android:textSize="25sp"
android:gravity="center"
/>
</LinearLayout>
</RelativeLayout>
<View
android:id="#+id/divideaftertens"
android:layout_width="#dimen/divide_width"
android:layout_height="#dimen/divider_height"
android:background="#color/YellowGreen"
android:layout_below="#+id/tens_view_top"
/>
</RelativeLayout>
On the layout android:id="#+id/tens_view_left", you could add this 2 atributes:
android:layout_alignParentLeft="true"
so that its left side is anchored in its parent's left border, and
android:layout_toLeftOf="#id/id_from_the_linear_layout_on_the_right"
And that layout is:
<LinearLayout
android:id="#+id/id_from_the_linear_layout_on_the_right"
android:layout_height="100dp"
android:layout_width="wrap_content"
android:background="#color/hologreendark"
android:layout_alignParentRight="true">
<TextView
android:id="#+id/amount_rupee"
android:layout_height="fill_parent"
android:layout_width="100dp"
android:text="Num3"
android:layout_centerVertical="true"
android:textSize="25sp"
android:gravity="center"
/>
</LinearLayout>
By the way, you might need to set android:orientation="horizontal"
on the layout android:id="#+id/tens_view_top"
When using layout_weight, set the layout_width to 0dp (for horizontal orientation).
it could be like,
<LinearLayout
android:id="#+id/tens_view_left"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
>
<LinearLayout
android:id="#+id/firstcoloumn"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="horizontal"
>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="0dp"
android:orientation="vertical"
android:layout_weight="1">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:id="#+id/amount_rupee"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Num1"
android:layout_centerVertical="true"
android:textSize="25sp" />
<TextView
android:id="#+id/multiply_sign"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="X"
android:layout_centerVertical="true"
android:textSize="26sp"
android:layout_toRightOf="#id/amount_rupee"
android:layout_marginLeft="40dp" />
<TextView
android:id="#+id/multiple_digit"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Num2"
android:layout_centerVertical="true"
android:textSize="25sp"
android:layout_toRightOf="#+id/multiply_sign"
android:layout_marginLeft="40dp" />
</LinearLayout>
<SeekBar
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#+id/multiple_digit"
android:layout_marginTop="8dp"
/>
</LinearLayout>
<LinearLayout
android:layout_height="100dp"
android:layout_width="wrap_content"
android:background="#color/hologreendark"
android:layout_alignParentRight="true">
<TextView
android:id="#+id/amount_rupee"
android:layout_height="fill_parent"
android:layout_width="100dp"
android:text="Num3"
android:layout_centerVertical="true"
android:textSize="25sp"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Change the RelativeLayout to LinearLayout that contain two layout you want to have close, and add layout_weight=1 to first LinearLayout with layout_width=0dip like
<LinearLayout
android:id="#+id/tens_view_top"
android:layout_below="#id/divideaftertotal"
android:layout_height="75dp"
android:layout_width="wrap_content"
>
<LinearLayout
android:layout_weight="1"
android:id="#+id/tens_view_left"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="0dip"
>
<LinearLayout
android:id="#+id/firstcoloumn"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:id="#+id/amount_rupee"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Num1"
android:layout_centerVertical="true"
android:textSize="25sp" />
<TextView
android:id="#+id/multiply_sign"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="X"
android:layout_centerVertical="true"
android:textSize="26sp"
android:layout_toRightOf="#id/amount_rupee"
android:layout_marginLeft="40dp" />
<TextView
android:id="#+id/multiple_digit"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Num2"
android:layout_centerVertical="true"
android:textSize="25sp"
android:layout_toRightOf="#+id/multiply_sign"
android:layout_marginLeft="40dp" />
</LinearLayout>
<SeekBar
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#+id/multiple_digit"
android:layout_marginTop="8dp"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_height="100dp"
android:layout_width="wrap_content"
android:background="#FFCCDD"
android:layout_alignParentRight="true">
<TextView
android:id="#+id/amount_rupee"
android:layout_height="fill_parent"
android:layout_width="100dp"
android:text="Num3"
android:layout_centerVertical="true"
android:textSize="25sp"
android:gravity="center"
/>
</LinearLayout>
// try this i have used LinearLayout rather than RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="7dp">
<LinearLayout
android:id="#+id/total_top_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center"
android:paddingBottom="10dp">
<TextView
android:id="#+id/ammount_view"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="First Text Here"
android:textSize="20sp"/>
<EditText
android:id="#+id/total_ammount_input"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:singleLine="true"
android:text="second Text"
android:padding="8dp"
android:gravity="center"/>
</LinearLayout>
<View
android:id="#+id/divideaftertotal"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:id="#+id/tens_view_left"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:id="#+id/firstcoloumn"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center">
<LinearLayout
android:layout_height="match_parent"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/amount_rupee"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Num1"
android:textSize="25sp" />
<TextView
android:id="#+id/multiply_sign"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="X"
android:layout_centerVertical="true"
android:textSize="26sp"
android:layout_marginLeft="40dp" />
<TextView
android:id="#+id/multiple_digit"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Num2"
android:textSize="25sp"
android:layout_marginLeft="40dp" />
</LinearLayout>
</LinearLayout>
<SeekBar
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#+id/multiple_digit"
android:layout_marginTop="8dp"/>
</LinearLayout>
<TextView
android:id="#+id/amount_rupee"
android:layout_height="match_parent"
android:layout_width="100dp"
android:text="Num3"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
<View
android:id="#+id/divideaftertens"
android:layout_width="match_parent"
android:layout_height="1dp"/>
</LinearLayout>

Setting width ratio in relative layout

Im pretty new to Android Layouts.
Im trying to set Age:EditText:Year in ratio 1:3:1 and same size on Height.
Should I use TableLayout? If yes how can i prevent TableRow1 from changing width etc when I do some stuff in other rows?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/txtAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/search_text"
android:layout_alignBottom="#+id/search_text"
android:layout_alignParentLeft="true"
android:text="Age"
android:layout_weight="0.3"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/txtYears"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/search_text"
android:layout_alignBottom="#+id/search_text"
android:layout_alignParentRight="true"
android:text="Years"
android:layout_weight="0.3"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/search_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#id/txtYears"
android:layout_toRightOf="#id/txtAge"
android:ems="10"
android:hint="Enter your age"
android:inputType="text|number"
android:lines="1"
android:layout_weight="0.7"
android:maxLength="2"
android:maxLines="1" />
<TextView
android:id="#+id/txtHeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignBaseline="#+id/enterHeight"
android:layout_below="#+id/search_text"
android:text="Height"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1"/>
<Spinner
android:id="#+id/spinHeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/txtHeight"
android:layout_weight="1" />
<EditText
android:id="#+id/enterHeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/search_text"
android:layout_toLeftOf="#+id/spinHeight"
android:layout_toRightOf="#+id/txtHeight"
android:ems="10"
android:hint="Enter your height"
android:inputType="text|number"
android:lines="1"
android:layout_weight="3"
android:maxLength="3"
android:maxLines="1" >
<requestFocus />
</EditText>
</RelativeLayout>
I think this is what you want. see image below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:gravity="center_vertical|center_horizontal"
android:text="Age"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold" />
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="60" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:gravity="center_vertical|center_horizontal"
android:text="Years"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:gravity="center_vertical|center_horizontal"
android:text="Height"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold" />
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="60" />
<Spinner
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20" />
</LinearLayout>
</LinearLayout>
I think you should use linear layout.you can set android:weightSum="10" properties.
Its total layout. now you have to set for chilled object using android:layout_weight="2".
So that will control your object in layout.
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_weight="2"
android:text="Age" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="6" >
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:layout_weight="2"
android:text="Years" />
Have something like this.. Pseudo code untested..
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:gravity = "center_horizontal"
android:id="#+id/firstLinearLayout"
android:orientation = "horizontal"
android:weightSum="10">
<TextView
android:weight = "2"
/>
<TextView
android:weight = "6"
/>
<EditText
android:weight = "2"
/>
</LinearLayout>
<LinearLayout
android:gravity = "center_horizontal"
android:layout_below="#id/loginButtonLayout"
android:orientation = "horizontal"
android:weightSum="10">
</LinearLayout>

Categories

Resources