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

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" />

Related

Android multiple checkboxes on a single table row

Not sure what I am doing wrong, but I am trying to put 5 check boxes in a single table row. The row seems to end after the first check box, and the remaining 4 check boxes are appearing outside the row. Here is the code:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/txtView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:typeface="serif"
android:textSize="18sp"
android:text="#string/start_head"
android:gravity="center"
android:layout_span="5"
android:layout_marginTop="10dp" >
</TextView>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" >
<CheckBox
android:id="#+id/chkAtm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/atm" />
<CheckBox
android:id="#+id/chkGift"
android:layout_width="wrap_content"
android:text="#string/giftcard" />
<CheckBox
android:id="#+id/chkRing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ring" />
<CheckBox
android:id="#+id/chkMerchant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/merchant" />
<CheckBox
android:id="#+id/chkCod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cod" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<Button
android:id="#+id/btnRun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/run"
android:layout_span="5" />
</TableRow>
</TableLayout>
Your second CheckBox hasn't got android:layout_height, check it ;)

Table layout in android (span/shrink)?

I want this special table layout:
In my current layout the cells that are shorter are at the same size like the cells with the long text:
How do have to do it?
It should look like the first picture.
There may be better ways of doing this, I actually haven't used TableLayout before, but the XML below gives the basic layout that you asked for:
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="4"
android:layout_weight="1"
android:text="Text"
android:gravity="center" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:layout_weight="0.75"
android:text="Text that is longer than other cells" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Text" />
</TableRow>
</TableLayout>
Yojimbo, you are right, however if you use
android:layout_weight
you must remember about replacing (in this special case) android:layout_width="wrap_content" with
android:layout_width="0dp" .
Here you have a nice reference.

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="*"

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>

Views/Widgets are not wrapping

I will just cut to the chase and paste all of my xml for a particular Activity below. The problem I'm having is that the widgets are not wrapping, and so much of the text is bleeding off the right edge of the screen.
As I'm using "wrap_content" for the width, I don't understand why this is happening.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textViewSendLocWhenItChanges"
android:text="Send My Pterodactyl When It Changes"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<CheckBox
android:id="#+id/checkBoxSendToSelectedDuckbilledPlatypiWhenPterodactylChanges"
android:text="Send My Pterodactyl to Selected Duckbilled Platypi When It Changes" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<Button
android:id="#+id/buttonSendOnPterodactylChangedToSelected"
android:layout_weight="1"
android:text="Select Recipients From Contacts" />
<TextView android:layout_weight="1" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<TextView
android:layout_span="1"
android:layout_weight="1"
android:text="Send Pterodactyl to selected Duckbilled Platypi from Contact List when my Pterodactyl changes by more than the following number of miles:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editTextMilesBeforeNotification"
android:layout_width="200px"
android:layout_span="1"
android:layout_weight="1" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<TextView android:layout_height="10px" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<TextView android:layout_height="20px" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textViewBuildStringOfWhoReceivesChangeOfPterodactylUpdates"
android:layout_width="wrap_content"
android:text="Your Pterodactyl will be immediately sent to "
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<TextView android:layout_height="40px" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<Button
android:id="#+id/buttonNext"
android:layout_weight="1"
android:text="Save and Exit Keep In Touch Configuration" />
<TextView
android:layout_weight="1" />
</TableRow>
</TableLayout>
layout_width does not apply to TableRows. Use layout_span when you have a single column spanning two columns. Try using android:shrinkColumns. Hope it helps.

Categories

Resources