how to create three columns in TableLayout - android

I am developing a screen that uses TableLayout. Here I am easily able to create two columns. but how can I create three columns ?

Here an example:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:text="first"
android:padding="3dip" />
<TextView
android:text="second"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="third"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="first"
android:padding="3dip" />
<TextView
android:text="second"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="third"
android:gravity="right"
android:padding="3dip" />
</TableRow>
</TableLayout>

For each TableRow you have to add three children instead of two. This should be ok for you!
Hope this helps!

Related

Don't allow to TextView fill the entire Column

I'm developing an Android app.
I have this layout:
I don't want to let a textview fill a entire column. As you can see on previous image there is a TextView surrounded with a blue rectangle (in fact is selected). This is the XML code that creates that TableRow:
<TableRow>
<TextView
android:id="#+id/txtFactLoc"
android:layout_span="2"
android:gravity="center_vertical|right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/layout_fac_location"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/factLocVal"
android:layout_span="4"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:layout_height="match_parent"
android:background="#color/textview_background"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtConFactLoc"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/layout_confirm"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/rGroupFactLoc"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rFactLocYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/layout_yes" />
<RadioButton
android:id="#+id/rFactLocNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/layout_no" />
</RadioGroup>
</TableRow>
Is inside a TableLayout with eight columns. This is the beginning for TableLayout:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*"
android:stretchColumns="*">
<TableRow>
<TextView
android:id="#+id/txtPONo"
android:layout_weight=".125"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="#string/layout_po_no"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/pONoVal"
android:layout_weight=".125"
android:layout_width="0dip"
android:paddingLeft="10dp"
android:layout_height="match_parent"
android:background="#color/textview_background"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtIndNo"
android:layout_weight=".125"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="#string/layout_ind_no"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/indNoVal"
android:layout_weight=".125"
android:layout_width="0dip"
android:paddingLeft="10dp"
android:layout_height="match_parent"
android:background="#color/textview_background"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtTimeIn"
android:layout_weight=".1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="#string/layout_time_in"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/timeInVal"
android:layout_weight=".15"
android:layout_width="0dip"
android:paddingLeft="10dp"
android:layout_height="match_parent"
android:background="#color/textview_background"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtTimeOut"
android:layout_weight=".1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="#string/layout_time_out"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/timeOutVal"
android:layout_weight=".15"
android:layout_width="0dip"
android:paddingLeft="10dp"
android:layout_height="match_parent"
android:background="#color/textview_background"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
Some textViews have a background color. As you can see all of them fill its columns and I don't want that because this form looks very ugly.
How can I change TextView width? If I have set android:layout_span="2" I can't change its width.
With android:stretchColumns, you can decide which columns are allowed to be streched (put it in the TableLayout tag).
android:stretchColumns="0"
android:stretchColumns="0,1,2"
android:stretchColumns="*"

tableLayout strechcolumns doesn't work

I have a TablLayout, it has 1 row and 2 columns.
The first column is a text, and I want it as big as possible, the second one is another table with 2 rows and 1 column:
<?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">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
>
<TableRow>
<TextView
android:id="#+id/parkingDataText"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
/>
<TableLayout>
<TableRow>
<ImageButton
android:id="#+id/buttonStar"
android:layout_marginLeft="10dip"
android:background="#null"
/>
</TableRow>
<TableRow>
<ImageButton
android:id="#+id/buttonRent"
android:layout_marginLeft="10dip"
android:background="#null"
/>
</TableRow>
</TableLayout>
</TableRow>
I thought I had to use strechColumns="1" and the first column occupied the space that the second one doesnt use, but it doesn't work.
Any idea?
Thank you in advance
Try below layout for your requirement :
<?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"
android:stretchColumns="2">
<TableRow>
<TextView
android:layout_column="1"
android:text="Save As..."
android:padding="3dip"
android:layout_width="200dp" />
<TableLayout
android:layout_column="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="2">
<TableRow>
<TextView
android:layout_column="1"
android:text="Textview column1"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:layout_column="1"
android:text="Textview column2"
android:padding="3dip" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>

Align textview columns vertically when using custom adaptor / layoutinflator

I have three columns that I want to align vertically. The data for the columns are retrieved using an object and then displayed using a LayoutInflater.
At present the columns are all over the place. I am hoping to have them perfectly aligned. I'm using TextView but I am starting to wonder if I shouldn't be using some kind of TableLayout.
Here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/table_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TableRow android:id="#+id/row_multi_row">
<TextView
android:id="#+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="3dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="3dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/item3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="3dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
Try this :
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row_multi_row"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:id="#+id/item1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.34"
android:padding="3dip"
android:text="asdasd"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/item2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.34"
android:padding="3dip"
android:gravity="center"
android:text="adasd"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/item3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.32"
android:padding="3dip"
android:gravity="center"
android:text="asdasdad"
android:textAppearance="?android:attr/textAppearanceLarge" />
set all textview width and tab row width to fill parent and use layout_weight="1". It will work

Can't Center middle textview when makeing app for my HTC

For about 3 week ago I started looking for the first time in to this program Eclipse and the possibility to make my own app for my HTC. I'm playing Modern Warefare 3 on PC and I like to keep track on my statistics when playing. I made an application in VisualBasic that extracts my stats and uploads it to my site. From there I download the stats to my HTC. Everything works perfect, but I can't get the yellow XP to center in the textvew.
I have google for a solution, but i have not find any with my specific problem.
Here is the code from main.xml:
<?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"
android:stretchColumns="0,2"
android:shrinkColumns="1">
<TableRow
android:background="#363B38" android:layout_width="wrap_content">
<TextView
android:layout_span="3"
android:layout_gravity="center"
android:id="#+id/gamename"
android:textColor ="#FCCA65" />
</TableRow>
<TableRow
android:background="#FF2E3230" android:layout_width="wrap_content">
<TextView
android:layout_span="3"
android:layout_gravity="center"
android:id="#+id/gamerank"/>
</TableRow>
<TableRow
android:layout_gravity="center"
android:background="#FF2E3230">
<TextView
android:text="00"
android:id="#+id/gamelevelc"
android:layout_gravity="left"/>
<TextView
android:text="00 / 00 XP"
android:id="#+id/gamexp"
android:textColor="#FEFC41"
android:layout_gravity="center"/>
<TextView
android:text="00"
android:id="#+id/gameleveln"
android:layout_gravity="right"/>
</TableRow>
<ProgressBar
android:id="#+id/myProgress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100" />
<TableRow android:background="#FF2E3230">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/score" />
<TextView
android:layout_gravity="right"
android:id="#+id/gamescore"
android:textColor ="#D2AD53" />
</TableRow>
<TableRow android:background="#FF363B38">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/wins" />
<TextView
android:id="#+id/gamewins"
android:layout_gravity="right" />
</TableRow>
<TableRow android:background="#FF2E3230">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/losses" />
<TextView
android:id="#+id/gamelosses"
android:layout_gravity="right" />
</TableRow>
<TableRow android:background="#FF363B38">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/ties" />
<TextView
android:id="#+id/gameties"
android:layout_gravity="right"/>
</TableRow>
<TableRow android:background="#FF2E3230">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/win_streak" />
<TextView
android:id="#+id/gamewinstreak"
android:layout_gravity="right" />
</TableRow>
<TableRow android:background="#FF363B38">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/kills" />
<TextView
android:id="#+id/gamekills"
android:layout_gravity="right"
android:textColor ="#D2AD53" />
</TableRow>
<TableRow android:background="#FF2E3230">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/deaths" />
<TextView
android:id="#+id/gamedeaths"
android:layout_gravity="right"/>
</TableRow>
<TableRow android:background="#FF363B38">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/kd_ratio" />
<TextView
android:id="#+id/gameratio"
android:layout_gravity="right" />
</TableRow>
<TableRow android:background="#FF2E3230">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/headshots" />
<TextView
android:id="#+id/gameheadshots"
android:layout_gravity="right"/>
</TableRow>
<TableRow android:background="#FF363B38">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/hits" />
<TextView
android:id="#+id/gamehits"
android:layout_gravity="right" />
</TableRow>
<TableRow android:background="#FF2E3230">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/misses" />
<TextView
android:id="#+id/gamemisses"
android:layout_gravity="right" />
</TableRow>
<TableRow android:background="#FF363B38">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/accuracy" />
<TextView
android:id="#+id/gamepercent"
android:layout_gravity="right" />
</TableRow>
<TableRow android:background="#FF2E3230">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/assists" />
<TextView
android:id="#+id/gameassists"
android:layout_gravity="right" />
</TableRow>
<TableRow android:background="#FF363B38">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/kill_streak" />
<TextView
android:id="#+id/gamekillstreak"
android:layout_gravity="right" />
</TableRow>
<TableRow android:background="#FF2E3230">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/time_played" />
<TextView
android:id="#+id/gametimeplayed"
android:layout_gravity="right"/>
</TableRow>
<View
android:layout_height="3dip"
android:background="#FF000000" />
<TableRow android:background="#FF363B38">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/games_played" />
<TextView
android:id="#+id/gameplayed"
android:layout_gravity="right" />
</TableRow>
<TableRow android:background="#FF2E3230">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/latest_game" />
<TextView
android:id="#+id/gamelastplayed"
android:layout_gravity="right" />
</TableRow>
<TableRow android:background="#54604A">
<TextView
android:layout_span="2"
android:layout_gravity="left"
android:text="#string/updated_at" />
<TextView
android:id="#+id/gamelastupdated"
android:layout_gravity="right" />
</TableRow>
</TableLayout>
Any suggestions? I'm new at Eclipse and makeing apps for android, so please no advanced programing suggestions :-)
Regards
George
You can use the android:layout_weight attribute for your TextViews. Specify android:layout_width="0dip" for every of them and set android:layout_weight="1". This will make every of your three TextViews occupy 1/3 of the screens width. Now you can change android:gravity attribute for every TextView to change the text positioning in its 1/3 of the screen. For example, use center to center the text, or left to bind it to the left side of its bounding rectangle. Hope this helps.
change to this and give a try:
<TextView
android:text="00 / 00 XP"
android:id="#+id/gamexp"
android:textColor="#FEFC41"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:gravity="center" />

issue on Screen design with table layout under scroll layout.

I have seen an issue while designing dashboard screen,
I had one layout : say Linearlayout1 and having scroll layout: say scrollLayout1 as child of Linearlayout1, and havinf Table layout under this scroll layout,
i added table rows which contains buttons with drawables.
now the question is> I can see, table layout height is bit more than the scroll layout which is child of scroll layout. how child layout can have more height then parent layout. (I set table layout height as matchparent or fill parent).
If you are building a dashboard, then use the DashboardLayout from Google IO open source app.
That is the best way to do it. If you are planning to do something manually, it may not be possible to test on all the screen sizes, densities.
I used it in 3 of my projects, which just works.
Check this answer on stackoverflow.
Try This it may helps you
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:id="#+id/toplayout"
android:layout_width="fill_parent"
android:layout_height="50dip">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TopBar"/>
</RelativeLayout>
<ScrollView
android:id="#+id/scrolllayout"
android:layout_width="fill_parent"
android:layout_height="150dip"
android:padding="5dip"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:padding="5dip"
android:background="#android:color/darker_gray">
<TableRow>
<TextView
android:layout_column="1"
android:text="Open..."
android:textColor="#FFFFFF"
android:padding="3dip" />
<TextView
android:text="Ctrl-O"
android:gravity="right"
android:textColor="#FFFFFF"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:layout_column="1"
android:text="Save..."
android:textColor="#FFFFFF"
android:padding="3dip" />
<TextView
android:text="Ctrl-S"
android:gravity="right"
android:textColor="#FFFFFF"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:layout_column="1"
android:text="Save As..."
android:textColor="#FFFFFF"
android:padding="3dip" />
<TextView
android:text="Ctrl-Shift-S"
android:gravity="right"
android:textColor="#FFFFFF"
android:padding="3dip" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow>
<TextView
android:text="X"
android:textColor="#FFFFFF"
android:padding="3dip" />
<TextView
android:text="Import..."
android:textColor="#FFFFFF"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="X"
android:textColor="#FFFFFF"
android:padding="3dip" />
<TextView
android:text="Export..."
android:textColor="#FFFFFF"
android:padding="3dip" />
<TextView
android:text="Ctrl-E"
android:gravity="right"
android:textColor="#FFFFFF"
android:padding="3dip" />
</TableRow>
<View
android:layout_height="2dip"
android:textColor="#FFFFFF"
android:background="#FF909090" />
<TableRow>
<TextView
android:textColor="#FFFFFF"
android:layout_column="1"
android:text="Quit"
android:padding="3dip" />
</TableRow>
</TableLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>

Categories

Resources