Android limit a text view length - android

I have a layout containing two text views and an image button, like below figure.
This is the code of the layout
<LinearLayout
android:id="#+id/llGlobalControl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/gc_background"
android:gravity="center"
android:padding="6dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical"
android:weightSum="2" >
<TextView
android:id="#+id/tvGlobalControlTitle"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green"
android:textStyle="bold" />
<TextView
android:id="#+id/tvGlobalControlArtist"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="vertical" >
<ImageButton
android:id="#+id/ibGlobalControlPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_pause_over_video" />
</LinearLayout>
</LinearLayout>
My problem is that if the text in either of the text views gets too long, the button "gets pushed away", like the figure below.
Instead, I would like that the text view limits the text contained, something like the red underline in the figure below.
How do I do this?

The trick is to use android:ellipsize="end" and limit number of lines with android:maxLines="1".
Also, to prevent your ImageButton from getting pushed away, the easiest way is to set android:layout_weight="1" to the LinearLayout with TextViews and android:layout_width="wrap_content" to the LinearLayout containing the button.
This is the result:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llGlobalControl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/gc_background"
android:gravity="center"
android:padding="6dp" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical"
android:layout_weight="1"
android:weightSum="2" >
<TextView
android:id="#+id/tvGlobalControlTitle"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="This is a long text that will take up a lot of space and push the button away"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#color/green"
android:textStyle="bold" />
<TextView
android:id="#+id/tvGlobalControlArtist"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#color/green" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="vertical" >
<ImageButton
android:id="#+id/ibGlobalControlPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_pause_over_video" />
</LinearLayout>
</LinearLayout>

try to set to your TextView set together
android:ellipsize="end"
android:singleLine="true"

try this
Add
android:ellipsize="end"
android:singleLine="true"
to TextView you want to be single line and with "..." at end.

try like this,
<LinearLayout
android:id="#+id/llGlobalControl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/gc_background"
android:gravity="center"
android:padding="6dp" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left"
android:orientation="vertical"
android:weightSum="2" >
<TextView
android:id="#+id/tvGlobalControlTitle"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green"
android:textStyle="bold" />
<TextView
android:id="#+id/tvGlobalControlArtist"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="vertical" >
<ImageButton
android:id="#+id/ibGlobalControlPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_pause_over_video" />
</LinearLayout>
</LinearLayout>

Try this way.. Added weight to layout..
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:layout_weight="1"
android:orientation="vertical"
android:weightSum="2" >
<TextView
android:id="#+id/tvGlobalControlTitle"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green"
android:textStyle="bold" />
<TextView
android:id="#+id/tvGlobalControlArtist"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:layout_weight="3"
android:orientation="vertical" >
<ImageButton
android:id="#+id/ibGlobalControlPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_pause_over_video" />
</LinearLayout>

Try using this layout:
<RelativeLayout
android:id="#+id/llGlobalControl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/gc_background"
android:padding="6dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2"
android:layout_toLeftOf="#id/ibGlobalControlPlayPause"
android:layout_toStartOf="#id/ibGlobalControlPlayPause" >
<TextView
android:id="#+id/tvGlobalControlTitle"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green"
android:textStyle="bold"
android:singleLine="true"
android:ellipsize="end"
android:maxLines="1"/>
<TextView
android:id="#+id/tvGlobalControlArtist"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
android:text="Text View"
android:textColor="#color/green"
android:singleLine="true"
android:ellipsize="end"
android:maxLines="1" />
</LinearLayout>
<ImageButton
android:id="#+id/ibGlobalControlPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_pause_over_video" />
</RelativeLayout>

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.

Listview Row is not showing full element

I have listview in which I am setting six different things. And that is working awesome on normal size devices. But its gets chopped off from the last item of the single row that is Text view.
I do not understand why it is happening I have Used linear layouts. And why it is only getting chopped off in the large size devices like Samsung Tab 7" and 10"
although normal devices are showing it good.
Edit One : This is design of my Listview Row
Here is what my Layout of listview looks a like.
<?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:paddingLeft="10dp"
android:paddingRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:background="#drawable/box"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:weightSum="20">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="0dp"
android:layout_weight="5"
android:orientation="vertical">
<TextView
android:id="#+id/tv_row_customer_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/accepted_offer_text_size"
android:layout_marginTop="2dp"
android:text="Customer Name Goes Here"
android:textColor="#ffa123"
android:textSize="#dimen/accepted_offer_text_size" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp">
<TextView
android:id="#+id/row_offer_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/dialog_offer_name"
android:gravity="center_horizontal|center"
android:paddingLeft="5dp"
android:text="Offer Name Goes here"
android:textColor="#AAAAAA" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp">
<TextView
android:id="#+id/row_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/mail"
android:gravity="left|center"
android:paddingLeft="5dp"
android:text="Email Goes here"
android:textColor="#AAAAAA" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp">
<TextView
android:id="#+id/row_phone_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/call"
android:gravity="center_horizontal|center"
android:paddingLeft="5dp"
android:text="Phone Number Goes here"
android:textColor="#AAAAAA" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp">
<TextView
android:id="#+id/row_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/pin"
android:ellipsize="end"
android:gravity="left|center"
android:minLines="1"
android:paddingLeft="5dp"
android:singleLine="true"
android:text="Address Goes here"
android:textColor="#AAAAAA" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp">
<TextView
android:id="#+id/row_c_brand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/cofin"
android:gravity="center_horizontal|center"
android:paddingLeft="5dp"
android:text=" Brand Goes here"
android:textColor="#AAAAAA" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp">
<TextView
android:id="#+id/row_offer_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/clock"
android:paddingLeft="5dp"
android:text="Start-End Time Goes here"
android:textColor="#AAAAAA" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="15dp"
android:orientation="horizontal"
android:paddingLeft="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text=" "
android:textColor="#AAAAAA" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_weight="15"
android:orientation="horizontal"
android:paddingTop="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_marginBottom="5dp"
android:gravity="right"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="3">
<TextView
android:id="#+id/tv_row_total_person"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/no_of_persons"
android:gravity="center"
android:text="aa" />
<TextView
android:id="#+id/tv_row_total_acceptors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/acceptors"
android:gravity="center"
android:text="aa" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#null"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/calander_upper"
android:gravity="center">
<TextView
android:id="#+id/tv_row_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:gravity="center"
android:hint="aa"
android:paddingTop="2dp"
android:textSize="#dimen/dialog_accept_offer_tvcontent_size" />
</LinearLayout>
<TextView
android:id="#+id/tv_row_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_row_month"
android:layout_alignStart="#+id/tv_row_month"
android:layout_below="#+id/tv_row_month"
android:layout_gravity="center"
android:background="#drawable/calander_lower"
android:gravity="center"
android:padding="0dp"
android:text="aa"
android:textSize="#dimen/dialog_accept_offer_tvcontent_size" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:focusable="false" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I added the Last textview empty so that it can be gets chopped off. but has no effect. I have debug my code and saw that in adapter the last textview is truly initialized and then the last textview gets the data. but its still not visible or chopped off.
Please help me just this minor problem is keeping me away to complete the app.
try to add "scrollView" so if your layout is not fit on any device it will scroll otherwise use weight in your layout so it will adjust according to each screen size. if you not understand you can ask question in comments, I will explain about that.
<?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:paddingLeft="10dp"
android:paddingRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="0dp"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:weightSum="20">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="0dp"
android:layout_weight="5"
android:orientation="vertical">
<TextView
android:id="#+id/tv_row_customer_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Customer Name Goes Here"
android:textColor="#ffa123" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp">
<TextView
android:id="#+id/row_offer_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
android:paddingLeft="5dp"
android:text="Offer Name Goes here"
android:textColor="#AAAAAA" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp">
<TextView
android:id="#+id/row_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center"
android:paddingLeft="5dp"
android:text="Email Goes here"
android:textColor="#AAAAAA" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp">
<TextView
android:id="#+id/row_phone_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
android:paddingLeft="5dp"
android:text="Phone Number Goes here"
android:textColor="#AAAAAA" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp">
<TextView
android:id="#+id/row_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="left|center"
android:minLines="1"
android:paddingLeft="5dp"
android:singleLine="true"
android:text="Address Goes here"
android:textColor="#AAAAAA" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp">
<TextView
android:id="#+id/row_c_brand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
android:paddingLeft="5dp"
android:text=" Brand Goes here"
android:textColor="#AAAAAA" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp">
<TextView
android:id="#+id/row_offer_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="Start-End Time Goes here"
android:textColor="#AAAAAA" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="15dp"
android:orientation="horizontal"
android:paddingLeft="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text=" "
android:textColor="#AAAAAA" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_weight="15"
android:orientation="horizontal"
android:paddingTop="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_marginBottom="5dp"
android:gravity="right"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="3">
<TextView
android:id="#+id/tv_row_total_person"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="aa" />
<TextView
android:id="#+id/tv_row_total_acceptors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="aa" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#null"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="#+id/tv_row_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:gravity="center"
android:hint="aa"
android:paddingTop="2dp" />
</LinearLayout>
<TextView
android:id="#+id/tv_row_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_row_month"
android:layout_alignStart="#+id/tv_row_month"
android:layout_below="#+id/tv_row_month"
android:layout_gravity="center"
android:gravity="center"
android:padding="0dp"
android:text="aa" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:focusable="false" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I changed something

How do I align values inside my listview list item?

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.

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 have multiple TextView and EditText in one row each

I'm trying to get a form such a way that on each row the TextView is on the left and the EditText is on the right.
The below code works fine for one row, however,r if I have multiple rows then it doesn't draw TextView and EditText on each row but instead tries to jam everything together.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dip"
android:background="#FFFFFF"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Full Name"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#000000"
/>
<EditText
android:id="#+id/name"
android:gravity="center"
android:hint="John Doe"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Age"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#000000"
/>
<EditText
android:id="#+id/age"
android:gravity="center"
android:hint="age"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
</LinearLayout>
This is what it looks like
There are so many ways to achieve the Layout which you been looking for. You were on track by adding layout_weight="1" in Views. Here's the one which uses your Layout only with little bit of orientation changes.
Explaination:
LinearLayout //ParentView Orientation - Vertical
LinearLayout //childView Orientation - Horizontal
TextView & EditText // Taking equal space with help of layout_weight="1"
LinearLayout //childView Orientation - Horizontal
TextView & EditText // Taking equal space with help of layout_weight="1"
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip"
android:background="#FFFFFF"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Full Name"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#000000"
/>
<EditText
android:id="#+id/name"
android:gravity="center"
android:hint="John Doe"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Age"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#000000"
/>
<EditText
android:id="#+id/age"
android:gravity="center"
android:hint="age"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
// try this way,hope this will help you...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#FFFFFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.40"
android:text="Full Name"
android:textSize="20sp"
android:gravity="right"
android:textColor="#000000"/>
<EditText
android:id="#+id/name"
android:gravity="center"
android:hint="John Doe"
android:layout_width="0dp"
android:layout_weight="0.60"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:layout_marginLeft="5dp">
<TextView
android:layout_width="0dp"
android:layout_weight="0.40"
android:layout_height="wrap_content"
android:text="Age"
android:gravity="right"
android:textSize="20sp"
android:textColor="#000000"/>
<EditText
android:id="#+id/age"
android:gravity="center"
android:hint="Age"
android:layout_width="0dp"
android:layout_weight="0.60"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

Categories

Resources