Layout File - Positioning Item to Right of Another Item (Android) - android

I'm trying to position one item in a RelativeLayout to the right of another. In the example below I want the "," to be placed to the right of the description field. However the problem is it puts the comma on the first line next to the name field. Can anyone help?
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10sp">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/name" />
<TextView
android:id="#+id/descriptioncomma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=", "
android:layout_toRightOf="#id/description" />

Try this code, it should work:
Button used layout_toRightOf attribute with id of Textview widget.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10sp">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/name" />
<TextView
android:id="#+id/descriptioncomma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=","
android:layout_below="#id/name"
android:layout_toRightOf="#id/description" />
</RelativeLayout>
In Both TextView you add text and than check it.
OK

use this one
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10sp">
<TextView
android:id="#+id/name"
android:text="na"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/description"
android:text="desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/name" />
<TextView
android:id="#+id/descriptioncomma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=", "
android:layout_toRightOf="#id/description"
android:layout_below="#id/name" />
</RelativeLayout>

Here it will help full : Button used layout_toRightOf attribute with id of Textview widget.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="TextView"
android:textSize="18sp"
android:padding="10dp"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_toRightOf="#id/textView"
android:text="Button" />
</RelativeLayout>
Here you can find Complete examples of linear layout positioning

Related

Android layout : trying to add an edittext above a listview

I am trying to add an edittext above a listview. The edittext field gets displayed, however, it is repeated for each row in the listview as well, which I don't want.
I have tried placing it outside the first inner LinearLayout but there is no difference in the behaviour.
Can someone please help me to figure out what I might be doing wrong.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:orientation="vertical">
<EditText
android:id="#+id/searchString"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="#drawable/abc_ic_clear_holo_light"
android:ems="10"
android:hint="Search for..."
android:text="" >
</EditText>
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="3sp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvCategory"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingRight="1sp"
android:text=""
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvCategory"
android:paddingRight="1sp"
android:text=""
android:textColor="#000000"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView7"
android:paddingRight="1sp"
android:text=""
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/textView8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/textView5"
android:gravity="right"
android:paddingRight="1sp"
android:text=""
android:textColor="#000000"
android:textSize="16sp" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/idAdView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_weight="1"
android:orientation="vertical" />
</LinearLayout>
Try adding the edit text as header view to the list.
public void addHeaderView (View v)
Ref : http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)

How to center align TextView below another TextView in a ListView

Currently my XML looks like the following:
What I'm trying to do is have 'Day' (which gets replaced with actual day (e.g. 03, 24)) horizontally centered below month (where in 'Month' also gets replaced with a MMM formatted month string) such that the horizontal center of day matches the horizontal center of month. To the side are some other TextView objects such as the event name and event time.
I based my XML in part off of How to center align a textview below imageview as a title in a List view but day is slightly too far to the right and I'm not quite sure how to remedy the issue despite fiddling around with attributes from the RelativeLayout docs.
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
events_info.xml (layout for ListView objects):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/eventsRelLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip">
<TextView
android:id="#+id/month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Month" />
<TextView
android:id="#+id/day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/month"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:singleLine="true"
android:text="Day" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/month"
android:paddingLeft="10dp"
android:paddingRight="0dp"
android:text="Name" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:layout_toRightOf="#id/month"
android:paddingLeft="10dp"
android:paddingRight="0dp"
android:text="Time" />
fragment_event_list.xml (ListView):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="fish.glow.EventFragment">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Try this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/rl_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="month month" />
<TextView
android:id="#+id/day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/month"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="day" />
</RelativeLayout>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/rl_left"
android:text="name" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/rl_left"
android:text="time" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/eventsRelLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip">
<LinearLayout
android:id="#+id/date"
android:orienation="vertical"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Month" />
<TextView
android:id="#+id/day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/month"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:singleLine="true"
android:text="Day" />
</LinearLayout>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/date"
android:paddingLeft="10dp"
android:paddingRight="0dp"
android:text="Name" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:layout_toRightOf="#id/date"
android:paddingLeft="10dp"
android:paddingRight="0dp"
android:text="Time" />

Android SDK - Positioning elements in listview row

For some reason I'm having a hell of a time with designing using eclipse and android SDK. Making layouts is painfully hard and not as easy as WPF/Visual Studio.
I am trying to accomplish the following:
Top: what I have
Bottom: what I want to achieve
http://imgur.com/AsaTuGq
Current code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dip"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:textIsSelectable="false"
android:id="#+id/line1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:textSize="18sp"
android:layout_weight="1"
android:textStyle="bold"
/>
<TextView
android:textIsSelectable="false"
android:id="#+id/line2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="18sp"
/>
<TextView
android:textIsSelectable="false"
android:id="#+id/line3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:textSize="18sp"
/>
</LinearLayout>
Edit:
I've also tried using Vertical LinearLayout within a Horizontal LinearLayout but that just produced a lot of errors.
Edit2:
What's with the negative rating?
Edit3:
Tried this link: http://android-developers.blogspot.ca/2009/02/android-layout-tricks-1.html
Replacing the image with a textview but the date just aligns with the top row.. hmm
You should use a RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="false"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
I would suggest a RelativeLayout for this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dip"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:textIsSelectable="false"
android:id="#+id/line1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:textSize="18sp"
android:layout_weight="1"
android:textStyle="bold"
android:layout_alignParentLeft="true"
/>
<TextView
android:textIsSelectable="false"
android:id="#+id/line2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="18sp"
android:layout_alignParentLeft="true"
android:layout_below="#_id/line1"
/>
<TextView
android:textIsSelectable="false"
android:id="#+id/line3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_alignParentRight="true"
android:textSize="18sp"
/>
</RelativeLayout>
Changes:
Changed LinearLayout to RelativeLayout used android:layout_alignParentLeft="true" for the first two lines and android:layout_below="#+id/line1" for the second line. Moved the date to the right with android:layout_alignParentRight="true" and android:gravity="center_vertical" Hope this works for what you need.

Place TextView at right of other one and make it always visible

It is easy to place one TextView at right of another TextView but when width on base TextView more than screen size right TextView became not visible.
My XML layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/messages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:text="Messages" />
<TextView
android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#drawable/bg_white_r30p0pl10"
android:drawableRight="#drawable/arrow_orange"
android:text="800" />
</LinearLayout>
How to make right TextView screen even if base TextView width is huge?
UPD:
In other words I need:
If first TextView is short:
|[ShotrTextView][TextView] |
If first TextView is long:
|[LooooooongTextVi...][TextView]|
The solution is TableLayout usage with shrinkColumns = 0
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="0" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/messages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:text="Messages" />
<TextView
android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#drawable/bg_white_r30p0pl10"
android:drawableRight="#drawable/arrow_orange"
android:text="800" />
</TableRow>
</TableLayout>
Try this
<?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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:inputType="text"
android:text="" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0"
android:inputType="text"
android:text="12345" />
</LinearLayout>
Posting this as thinking line the only way to do the so...
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:singleLine="true"
android:layout_height="wrap_content"
android:text="Medium TextMedium TextMedium TextMedium Text"
android:maxWidth="280dp"
android:ellipsize="end"
/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="HHH"
/>
And create one dimension file like this and use it.
Check the answer in: Two TextViews side by side, only one to ellipsize?
<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/messages" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"
android:ellipsize="end" android:lines="1" android:text="Message" />
<TextView android:id="#+id/counter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp"
android:layout_weight="1" android:background="#drawable/bg_white_r30p0pl10" android:drawableRight="#drawable/arrow_orange" android:text="800" />
</LinearLayout>

dynamic textview display in android

In my application I am showing list of datas from database in a list view.For that listview i have 1 header with 4 text views arranged horizontally..But i want this to be arranged with some space exactly between and also it should fit into every screen. Please help me.Thanks in advance.By the following code it is arranging 1 after the other without space.
My code:
<?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="fill_parent"
android:orientation="horizontal"
android:background="#ffffffff">
<TextView android:id="#+id/hdrdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"/>
<TextView android:id="#+id/hdrdateasc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView android:id="#+id/hdrdes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item"
/>
<TextView android:id="#+id/hdritemasc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView android:id="#+id/hdrprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"
/>
<TextView android:id="#+id/hdrpriceasc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
/>
<TextView android:id="#+id/hdrtot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total"
/>
<TextView android:id="#+id/hdrtotasc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
/>
</LinearLayout>
You can use layout margin for spacing between view like
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
To fit them on the screen you can use layout_weight.Give every view weight 1 so all will be equal.You already use fill_parent for the width of your LinearLayout.
android:layout_weight="1" for every view
try this use android:layout_weight="1"in all TextView
<?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="fill_parent"
android:orientation="horizontal"
android:background="#ffffffff">
<TextView android:id="#+id/hdrdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#ff00ff00"
android:text="Date"/>
<TextView android:id="#+id/hdrdateasc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="gffgfg"
android:textColor="#ffff0000"
android:layout_weight="1"
/>
<TextView android:id="#+id/hdrdes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FF4500"
android:text="Item"
/>
<TextView android:id="#+id/hdritemasc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="gdgdfgsg"
android:textColor="#ff0000ff"
android:layout_weight="1"
/>
<TextView android:id="#+id/hdrprice"
android:layout_weight="1"
android:layout_width="wrap_content"
android:textColor="#ff00ff00"
android:layout_height="wrap_content"
android:text="Price"
/>
</LinearLayout>
Instead of wrap_content for widht you should use weight on your textview and weightsum on linear layout:
<?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="fill_parent"
android:orientation="horizontal"
android:background="#ffffffff"
android:weightSum="100.0">
<TextView android:id="#+id/hdrdate"
android:layout_width="0dp"
android:layout_weight="100/number of text views"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="Date"/>
....
</LinearLayout>
If you do not want to use exact values for your margins you should add some blank text views and set the weight smaller than the one of the ones in which you have text.
Here is the code,
<?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="fill_parent"
android:orientation="horizontal"
android:background="#ffffffff">
<TextView android:id="#+id/hdrdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:layout_weight="1"
android:gravity="center_horizontal"/>
<TextView android:id="#+id/hdrdateasc"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#+id/hdrdes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item"
android:layout_weight="1"
android:gravity="center_horizontal"/>
<TextView android:id="#+id/hdritemasc"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#+id/hdrprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"
android:layout_weight="1"
android:gravity="center_horizontal"/>
<TextView android:id="#+id/hdrpriceasc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
<TextView android:id="#+id/hdrtot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total"
android:layout_weight="1"
android:gravity="center_horizontal"/>
<TextView android:id="#+id/hdrtotasc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
</LinearLayout>

Categories

Resources