How to create column-like layout for ListView rows? - android

I have a relative layout which looks like this:
Here is the code:
<?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"
>
<TextView
android:id="#+id/nameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:layout_alignParentLeft="true"
android:text="Symbol"
/>
<TextView
android:id="#+id/priceText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:layout_toRightOf="#+id/nameText"
android:gravity="right"
android:text="100"
/>
<TextView
android:id="#+id/changeText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:layout_toRightOf="#+id/priceText"
android:gravity="right"
android:text="1.3"
/>
</RelativeLayout>
How can I get the company name to line up next to the number 1.3?

Following up on Mayra's answer, here's one way to do it using layout_weight:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow>
<TextView
android:id="#+id/left_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:padding="5dip"
android:layout_weight="0"
android:text="Code"
/>
<TextView
android:id="#+id/middle_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right|center_vertical"
android:padding="5dip"
android:layout_weight="1"
android:text="Name of Company"
/>
<TextView
android:id="#+id/right_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:gravity="right|center_vertical"
android:layout_weight="0"
android:text="1.3"
/>
</TableRow>
</TableLayout>

It's not clear exactly what you want to have happen, but here are a few options:
If you want columns to be aligned across rows, you might consider using a TableLayout.
You can also use the "weight" atribute to affect what percentage of the screen each TextView takes up. If you assign a value of 1 on the left-most text view, and 0 on the other 2, this will cause the left text view to take up all the extra space, and thus push the middle text view to the right. This will probably cause the middle text view to look right aligned though. You could instead give the left one 20%, the middle one 50% and the right one 30% by assign 2, 5 and 3.
You could also just set an explicit size for the text views (in dpi), but this might be problamatic with different sized screens.

I agree with Mayra! Use the TableView, you'll get the formatting your looking for as well as keep almost all functionality of a ListView (scolling, and list oriented functionality)

Related

Android Nested LinearLayout, Gravity not having expected results

Having a problem with nested LinearLayouts to use as a custom row in a ListView.
App is a kind of internal corporate phone book and this row will be repeated in the ListView.
The "Details Witheld" on the right looks fine when gravity is set to left for it, but if gravity is set to center then it appears half off the page.
I'd guess my parent LinearLayout is too wide for the screen but it's set as fill_parent so I don't see how.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
Full XML source is below.
Aligned Left looks like this and I'd like Details to be a space in so it's centered above the Witheld.
Aligned Center looks like this
Full XML Source
<?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" >
<!-- Left side name & post / parish -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="80"
android:orientation="vertical" >
<TextView
android:id="#+id/txtrowname"
android:layout_marginBottom="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Smtih, Mr John"
android:textSize="24sp"
android:textColor="#color/darkblue_text"
android:textStyle="normal" />
<TextView
android:id="#+id/txtrowpost"
android:layout_marginBottom="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12 Some Street, SomeTown. SS11 1SS"
android:textSize="12sp"
android:textColor="#000000"
android:textStyle="normal" />
</LinearLayout>
<!-- Details withheld on right -->
<TextView
android:id="#+id/txtrowwitheld"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="Details Witheld"
android:layout_weight="20"
android:textSize="18sp"
android:gravity="center"
android:lines="2"
android:maxLines="2"
android:textColor="#FF0000"
android:layout_marginBottom="2dp"
android:textStyle="normal" />
</LinearLayout>
Image with layout_gravity as suggested by Ramesh
try layout_gravity in place of gravity that will work and dont use line="2". it will automatically fit into 2 lines if width exceeds.
I ended up just making do without it being centered.

How can I align two EditText views when I do not know the amount of text ahead of time for both views?

I have two EditText views whose height I wish to align. The problem is that I cannot predict which view will have more text and hence using android:layout_alignTop and android:layout_alignBottom will not work. The layout I am experimenting with is below. I have already tried android:layout_alignTop and android:layoutAlignBottom but they do not yield satisfactory results.
Layout 1
<?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">
<EditText
android:id="#+id/text1"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="5dip"
android:text="Text 1 bvhg hbjbj kjkbkj kjnjkn hlihj lklkkl"
/>
<EditText
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/text1"
android:padding="5dip"
android:text="Text 2 lasndklsa lkmsclslk klmsldmlk lksdl"
/>
</RelativeLayout>
Figure for Layout 1: http://imgur.com/P6KV7.png
Layout 2 with android:layout_alignTop and android:layout_alignBottom
<?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">
<EditText
android:id="#+id/text1"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/text2"
android:layout_alignBottom="#+id/text2"
android:padding="5dip"
android:text="Text 1 bvhg hbjbj kjkbkj kjnjkn hlihj lklkkl"
/>
<EditText
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/text1"
android:padding="5dip"
android:text="Text 2 lasndklsa lkmsclslk klmsldmlk lksdl"
/>
</RelativeLayout>
Figure for Layout 2: http://imgur.com/x6fyI.png
But, if I were to change the text on the first EditText then some of the text in that box gets cut-off. See Layout 3 and Figure 3 below:
Layout 3
<?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">
<EditText
android:id="#+id/text1"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/text2"
android:layout_alignBottom="#+id/text2"
android:padding="5dip"
android:text="Text 1 bvhg hbjbj kjkbkj kjnjkn hlihj lklkkl
1 bvhg hbjbj kjkbkj kjnjkn hlihj lklkkl ncksjcksn
lkslksldcsdc
the end"
/>
<EditText
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/text1"
android:padding="5dip"
android:text="Text 2 lasndklsa lkmsclslk klmsldmlk lksdl"
/>
</RelativeLayout>
Figure 3 for Layout 3 : (Sorry, cannot post more than two hyperlinks because of rep issues.)
Is there a way to align the heights of EditText views when the amount of text in either view is not known ahead of time?
Well, I have to say, this is the first time I've ever found a TableLayout useful. Note the text LOOKS cut off in the preview (as it's really big), but when you compile and run, you'll see that the text is scrollable. The TableView does all the tricky stuff for you. I think this will work for you.
Just realised that you want the textviews to look the same size also - that's easy - give them a transparent background and make the background of the table have the background.
EDIT - One more thing - due to a framework bug, I had to use this:
android:inputType="none"
android:editable="false"
to get the text both scrollable and non-editable. Also, a good point from #mandel - use "fill_parent" not "match_parent" if programming for android devices of an api level less than 8.
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:id="#+id/tableRow1"
android:gravity="center_vertical">
<EditText
android:inputType="none"
android:editable="false"
android:text="I have two EditText to align. The problem is that I cannot predict which view will have more tetText to align. The problem is that I cannot predict which view will have more text and hencem I have two EditText views whose height I wish to align. The problem is that I cannot predict which view will have more text and hence"
android:id="#+id/editText1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
</EditText>
<EditText
android:inputType="none"
android:editable="false"
android:text="I have two EditText views whose height I wish to alignhave more text and hence "
android:id="#+id/editText2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"></EditText>
</TableRow>
</TableLayout>

Android - Make a background image not stretch it's assigned view out

Hooray for pictures! Here's what it looks like when assigning the picture to the background, and when not.
I'd very much like for it to not stretch out the top TableView if the image is larger than the table. I've included an empty "view" to give a little bit of extra space for the table's background already as well, as you can see in the XML to follow. Tried to mess around with ScaleType, but that was a wash.
How can I get the BG to stretch to fit the layout? I'm trying to handle the "user xxx has a smaller screen than WVGA" gracefully. I lack grace, apparently.
<TableLayout
android:id="#+id/widget29"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scaleType="fitXY"
android:background="#drawable/minetownx480"
>
<View
android:id="#+id/blankSpace"
android:layout_width="fill_parent"
android:layout_height="60dip" />
<TableRow android:id="#+id/widget40" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal">
<TextView android:id="#+id/moneyText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Money: $$$$$$$$$" />
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/savingsText" android:gravity="right" android:text="Savings: $$$$$$$" android:layout_weight="3" android:textSize="12sp"/>
</TableRow>
<TableRow android:id="#+id/widget41" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal">
<TextView android:id="#+id/wagonText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Wagon Space: XXX/XXX" />
<TextView android:id="#+id/loanText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Loans: $$$$$$$$" android:gravity="right" android:layout_weight="3" android:textSize="12sp"/>
</TableRow>
<TableRow android:id="#+id/widget42" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" >
<TextView android:id="#+id/daysText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Days Left: XX/XX" />
<TextView android:id="#+id/armedText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Unarmed!" android:gravity="right" android:layout_weight="3" />
</TableRow>
</TableLayout>
The tablelayout is the first element of the root linearLayout, if that matters at all.
Edit: Alternatively, if you know how I could measure the tableLayout's X and Y space before assigning the image, I could scale it programmatically before assigning it.... Either solution would be good if anyone knows!
I would use FrameLayout and create two layers (i.e. add two views inside). First would be an ImageView and the second, therefore the top one, would be your table. In this way I would not rely on the table to control my image scaling but it would be independent from the ImageView which one can then hopefully more easily manipulate as it is not showing the image as 'background' but as the source.

How to guarantee space for an ImageView, keep it from shrinking?

I need to create an XML with the following content:
* Two TextViews with varying text (1- 3 rows), one TextView below the other.
* One ImageView to the right of the 2 TextViews, centered vertically, 30x30 px.
One major limitation is that it can't take the whole screen when inflated into a PopupWindow, which means that I cannot use the attributes fill_parent in many places.
I tried a lot of different layouts, but the ImageView keeps getting pushed away by the TextViews when the text is long. When the text is "half long" the image gets tiny but is still visible. I want it to always be 30x30 px, the text can wrap and use another line instead.
I tried to specify values for width and minWidth. Also tried layout_weight="1" for the ImageView. Also tried wrapping the ImageView into a LinearLayout and give that the layout_weight="1" parameter. Nothing works.
Here's an example of what is not working:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:orientation="vertical">
<TextView android:id="#+id/popupTitle" android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/popupContent"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="#drawable/img_30x30_px" />
</LinearLayout>
I've had a similar problem and i found that the TableView layout worked for me. It took a while but you can play with the stretch and strink columns proeprties to change the behaviour of how the columns expand to match there content.
Note that you should be able to set the linearLayout of your text to fill_parent (to fill the column space).
Read this on how TableRow works http://developer.android.com/resources/tutorials/views/hello-tablelayout.html
<TableRow>
<!-- Column 1 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical">
<TextView
android:layout_column="1"
android:text="Open..."
android:padding="3dip" />
<TextView
android:text="Ctrl-O"
android:gravity="right"
android:padding="3dip" />
</LinearLayout>
<!-- Column 2 -->
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="#drawable/img_30x30_px" />
</TableRow>
(Afraid i'm not on a machine with Android so i couldn't test the above code).
Thank you so much Emile! It works! You made my weekend! :)
I had to try it right away (althought it's friday evening), and here's what my xml now looks like:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:shrinkColumns="0">
<TableRow>
<!-- Column 1 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical">
<TextView android:id="#+id/popupTitle"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:id="#+id/popupContent"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<!-- Column 2 -->
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:src="#drawable/img_30x30_px" />
</TableRow>
</TableLayout>

Indent bullet list in TextView

I have a TextView which I fill with text from a string resources in strings.xml. The string resource contains < li > elements to create a bullet list inside the TextView. My problem is that I want to control the indention of lines in the bullet list that span over more than one line. Default the text isn't indented past the bullet so it looks kind of ugly. Is it possible to do this with style parameters or to create bullets in some other way?
Thanks in advance.
edit:
Is it really answered? I don't have any problems producing the bullet list, as described in those links but I'm having problems getting the layout correct. The indentation is like this:
text that go beyond the width
of the line.
And I want the "of the line" to at least start indented as far as the text after the bullet. That's what I try to achieve.
I'm suprised that there seems to be noone with this problem. I mean, bullet list can't be that uncommon in about-dialogs, FAQ etc and a bullet doesn't have to contain too much text to span more than one row and run into this problem.
Anyway, I got to solve it like this for now:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="#+id/ScrollViewTipsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TipsLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:layout_height="wrap_content"
android:id="#+id/TableLayout01"
android:layout_width="wrap_content"
>
<TableRow>
<TextView android:id="#+id/tvIngress"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#+string/tv_picking_content_ingress"
android:layout_span="2"
android:singleLine="false"
android:layout_weight="1"
/>
</TableRow>
<TableRow>
<TextView android:id="#+id/tvCleaningDot1"
android:layout_height="wrap_content"
android:text="•"
android:singleLine="false"
/>
<TextView android:id="#+id/tvCleaningFirst"
android:layout_height="wrap_content"
android:text="#+string/tv_picking_content_first"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="left"
android:singleLine="false"
/>
</TableRow>
<TextView android:id="#+id/tvCleaningDot2"
android:layout_height="wrap_content"
android:text="•"
android:singleLine="false"
/>
<TextView android:id="#+id/tvCleaningSecond"
android:layout_height="wrap_content"
android:text="#+string/tv_picking_content_second"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="left"
android:singleLine="false"
/>
</TableRow>
</TableLayout>
</RelativeLayout>
I use it to present static text in a bullet list so I don't bother to create the bullet + text dynamically in code. If anyone have any suggestion how to accomplish the same thing in a better way, please enlight me.
Btw, if going with the solution suggested in second link above:
android:text="<ol><li>item 1\n</li><li>item 2\n</li></ol>
The second, third etc. row in a bullet that span over more than one row won't get same indention as first line, which is quite ugly.
Thank you #Bjorn
You can also do something like bellow.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/point"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Text Here"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="14sp" />
</LinearLayout>
The way I solved this problem was by using a RelativeLayout and marginLeft. The marginLeft will put a blank margin between it and the previous item.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarTrackVertical="#drawable/scrollbar_vertical_track"
android:scrollbarThumbVertical="#drawable/scrollbar_vertical_thumb"
android:scrollbarSize="12dip">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10sp">
<TextView
android:id="#+id/body_text3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lineSpacingExtra="3sp"
android:text="Main paragraph of text, before the bulleted list"
android:textSize="15sp" />
<TextView
android:id="#+id/type1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lineSpacingExtra="3sp"
android:text="• First bullet"
android:textSize="15sp"
android:layout_below="#+id/body_text3"
android:layout_marginLeft="25dp" />
<TextView
android:id="#+id/type2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lineSpacingExtra="3sp"
android:text="• Second bullet"
android:textSize="15sp"
android:layout_below="#+id/type1"
android:layout_marginLeft="25dp" />
</RelativeLayout>
</ScrollView>
Just trying to point out the key answer of the question:
to create an bullet list, use TableLayout with two columns for each row. One column for TextView of a bullet and the other one for the text
to make text TextView fill the rest empty TableRow and indented at new line, set the weight to 1. You can set the bullet weight to zero or just simply not set the bullet weight and let it empty
based on my experience, changing width parameter do not affect the text and the bullet. So you can leave it empty or set it to anything you want.
Example:
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/bullet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/bullet"/>
<TextView
android:id="#+id/TextView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="this is the text of a bullet list"/>
</TableRow>
</TableLayout>

Categories

Resources