Android Table row, cannot center text - android

i have following layout code:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout1"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- APP NAME -->
<TableRow
android:id="#+id/table_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#2d9adf"
android:gravity="center_vertical"
android:padding="5dip" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center_horizontal"
android:text="#string/app_name"
android:textColor="#fff"
android:textSize="16sp" />
</RelativeLayout>
</TableRow>
Problem is, that result is not centered (see image below):
How can I solve it please?

Very easy fix to this. You need to change your RelativeLayout to have the correct dimensions:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
That way it will stretch the full size of the TableRow at the top. Then you can add this to your TextView:
android:layout_centerInParent="true"
to center the TextView in the RelativeLayout, which is spanning the TableRow.

Related

Display a TextView 5dp from the bottom

Android Studio 0.3.7
Hello,
I have the following TextView that is on a Linearlayout (vertical).
I want to display the TextView just about 5dp from the bottom of the layout. However, I can't seem to do that.
I have played around with the different properties of the android:layout_marginBottom and some other properties as well.
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Sign Up with StarBucks"
android:id="#+id/tv_signup"
android:clickable="true"
android:textColor="#380aff"
android:enabled="true"
android:textStyle="bold|italic"
android:gravity="center_horizontal"
android:layout_marginBottom="5dp"/>
Can anyone tell me what is the correct property to set to get the Textview positioned from the bottom of the layout.
Many thanks for any suggestions,
<?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:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center" >
//other controls define here
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="TextView" />
</LinearLayout>
Use this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="TextView" />
</LinearLayout>
The textView have to be child of the RelativeLayout then You can use alignParentBottom="true" and set marignBottom
Set android:layout_gravity to bottom
try this...
<TextView
android:padding_bottom="5dp"
android:gravity="bottom|center_horizontal"
........ />
Try setting android:layout_gravity="bottom"
or else if you want your textview to be horizontally center then you may set
android:layout_gravity="bottom|center_horizontal"

Need xml layout for the below screen

I am working on creating a page with a layout with header "Account Information" (ref.. image). Followed by a table and i need to populate the table dynamically after fetching from server but not able to do so. Below is the .xml file and attached is the image:
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#layout/header_gradient" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_weight="0.20"
android:gravity="center_horizontal"
android:padding="15dp"
android:text="#string/my_account_header_text"
android:textColor="#ffffff"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff" >
<RelativeLayout
android:id="#+id/my_activity_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff" >
<TextView
android:id="#+id/my_acc_component"
android:layout_width="158dp"
android:layout_height="wrap_content"
android:text="#string/my_account_component"
android:textColor="#372c24"
android:textSize="#dimen/login_credentials_txt" />
<TextView
android:id="#+id/my_acc_details"
android:layout_width="158dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/my_acc_component"
android:text="#string/my_account_details"
android:textColor="#372c24"
android:textSize="#dimen/login_credentials_txt" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
This may not be your issue, since I don't know what the problem is yet, but there is an issue that I see. When using weight in a LinearLayout with a vertical orientation your height should be 0dp. Similarly, if it is a horizontal orientation then width should be 0dp. So for example you would change your layouts to
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffffff" >
and
<RelativeLayout
android:id="#+id/my_activity_layout"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff" >
Also, this is definitely not whatever your problem is but fill_parent is deprecated and match_parent should be used instead.
*Actually, there is a problem with the weight property... if you are working with weight then you better use weight_sum property in the LinearLayout and give your textView or other view an percentage depending on your giver weight_sum value in the parent layout...
However,I think what you are looking for is something like this:*
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center|top"
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/header"
android:background="#drawable/image" // if you want your title/header to be upon the image. Or just create another ImageView for that image
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:orientation="horizontal"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:margin_Top="50dp"
android:weight="50"
android:id="#+id/my_acc_component"
android:text="your text"
android:textSize="your Size"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:margin_Top="50dp"
android:weight="50"
android:id="#+id/my_acc_details"
android:text="your text"
android:textSize="your Size"
/>
</LinearLayout>
</RelativeLayout>

TableLayout alignment with icon

Here is my XML.
I want to align the icon to the left and the text should appear right next to the icon, with some space, like you see elsewhere. But the text is showing up in the center. (or right aligned to the 2nd column, in two column layout)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#android:color/white"
android:layout_margin="5dp"
android:stretchColumns="2"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/events_header"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:drawable/list_selector_background"
android:clickable="true"
android:padding="5dip">
<ImageView android:id="#+id/ImageView02"
android:src="#drawable/event_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|left"
android:layout_margin="5dp">
</ImageView>
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="View Events"
android:layout_gravity="left"
android:textSize="21dp"
android:textStyle="bold"
android:background="#android:color/black"
android:textColor="#android:color/white" />
</TableRow>
</TableLayout>
</ScrollView>
This is how it displays:
[ * View Events ]
is the icon. I want to display like this.
[ * View Events ]
Try changing android:stretchColumns="2" to android:stretchColumns="1" because column index starts with 0. So if you only have two columns then the second column stands at index 1.

android width of linearlayout the same as image

I have my layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<ImageView
android:id="#+id/id_new_big_list_item_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="#drawable/round_corners_new_bottom" >
<TextView
android:id="#+id/id_new_big_list_item_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#color/new_restaurant_name"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="0dp" />
</LinearLayout>
</LinearLayout>
I need to show image at the top (center horizontal) and below linearlayout with textview. Image can have different width, but i want to set the width of the linearlayout the same as image has. But if text is too long, the width becomes more. How can i do that? if text's width more than image width is, make 2 lines of text.
Is it possible??
here snap of my layout :
set ImageView's layout_width="wrap_content" and remove the 2nd LinearLayout enclosing the TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal" >
<ImageView
android:id="#+id/id_new_big_list_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/id_new_big_list_item_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#color/new_restaurant_name"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="0dp" />
</LinearLayout>
Your Image should be wrap_contant and depends on it size. TextView will place in all parent.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal" >
<ImageView
android:id="#+id/id_new_big_list_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<TextView
android:background="#drawable/round_corners_new_bottom"
android:id="#+id/id_new_big_list_item_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#color/new_restaurant_name" />
</LinearLayout>
Good luck!
Here instead of taking a LinearLayout use a RelativeLayout.
And set the Bounds of your EditText to be Left & right aligned to the ImageView Width.So that when the length of the text is exceeding the height of the EditText is incremented in Vertical than that of Horizontal.

Layout views with dynamic width

I have a layout with two children one after another, like this:
|<view1><text1> |
<view1> may change it's width. So, I want it increases while both views stay on the screen:
|<really_wide_view1><text1> |
But when there is no more space at right, it stops:
|<reeeeeeeeeeeally_wide_vi...><text1>|
Is there easy way to do this?
Now I try to use this layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF00aa00" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loooooooooooooooooooooooooooooooooooong text" />
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FFaa0000"
android:minWidth="30dp"
android:singleLine="true"
android:text="test" />
</LinearLayout>
But result is the same:
I am able to solve your problem using the following code (a modified version of yours):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF00aa00" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0"
android:background="#FFaa0000"
android:text="testtest" />
</LinearLayout>
Here is a screenshot of the same:
Let me know if the above piece of code solves the problem.
Try to give both views inside the RelativeLayout and make the views layout width and as a wrap_content and give orientation as horizontal. I think it will solve your problem.
Here is solution with TableLayout, but it looks dirty.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:shrinkColumns="0" >
<TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
It works for any length of both text views.

Categories

Resources