How can align the bottom in TableRow? - android

At present, textView1 and imageViewSamll are the top aligned, how can I make textView1 align the bottom of imageViewSamll?
Thanks!
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/Thumbnail" />
<ImageView
android:id="#+id/imageViewSamll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>

Try this:
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/Thumbnail" />
<ImageView
android:id="#+id/imageViewSamll"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</TableRow>

Place both in a RelativeLayout.
RelativeLayout has many alignment properties available.
You can add android:layout_alignBottom="#id/imageViewSamll" to TextViews's attributes.

Related

Image left to TextView, Textview should be in center

I need to take a textview within Tablelayout with a image icon left to TextView. The TableLayout have width has fill_parent, so that whole area is clickable.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="top"
android:drawablePadding="5dp"
android:drawableStart="#drawable/down_arrow"
android:gravity="start|center"
android:text="#string/hello_world" />
</TableLayout>
I tried to bring the Textview with image to center, but not able to do that.
Kindly help me to achieve the same.
Thanks
Vikash
try this :
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:drawablePadding="5dp"
android:drawableStart="#drawable/ic_launcher_0"
android:gravity="start|center"
android:text="#string/hello_world" />
</TableRow>
</TableLayout>
Use this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:src="#drawable/app_icon" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_marginBottom="24dp"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/imageView1"
android:text="TextView" />
</RelativeLayout>

Android TableLayout With Different Column's Width In A Row

I am trying to do my first complex GUI, but now i can't to solve this problem.
The first column of the first two rows only needs to contain the label, while the second column of the first two rows must occupy the remaining space.
In this snapshot the problem that i noticed is that the first column is larger than i would like.
This is the piece of code that implements that piece of layout.
...
...
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/linear_layout_background"
android:stretchColumns="*"
android:shrinkColumns="*">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp" >
<TextView
android:id="#+id/textViewPlateValueLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/plateValue"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/productID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/back"
android:ems="5"
android:text="#string/blank"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp" >
<TextView
android:id="#+id/ztlLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ztl"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/ztlShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/back"
android:text="#string/blank"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center" >
<TextView
android:id="#+id/isAllowed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/blank"
android:background="#drawable/back"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
...
...
In addition, since they are really impractical, I would like to ask you some advice on the best approaches to use than this.
You can try using layout_weight like this:
<LinearLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainActivity" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#ff00ff"
android:stretchColumns="*"
android:shrinkColumns="*">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<TextView
android:id="#+id/textViewPlateValueLabel"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Label1:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/productID"
android:layout_weight="7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#00ff00"
android:ems="5"
android:text="Some Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<TextView
android:layout_weight="3"
android:id="#+id/ztlLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Label2:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_weight="7"
android:id="#+id/ztlShow"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#00ff00"
android:text="Some Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center" >
<TextView
android:id="#+id/isAllowed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Text"
android:background="#0088ff"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
</LinearLayout>
I wouldn't use TableLayout, if I were you. I'd probably use LinearLayout or RelativeLayout. You can get the same effect with some of the XML attributes. This is akin to using HTML's table attribute vs. using divs to simulate tables.
So, instead of a table, you can have LinearLayout inside of LinearLayout to simulate a table.
Although canDroid, could be sufficient to some of us, it didn't worked for me, I tried all kind of tricks to make the TableLayout to work. I found LinearLayout much easier to use, here is an example of TableLayout "like" layout using LinearLayout, few cells with a thumbnail, and few without.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="94dp"
android:layout_margin="5dp"
android:text="#string/nut1" />
</LinearLayout>
<View
android:layout_width="395dp"
android:layout_height="2dp"
android:background="#color/colorPrimary"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<ImageView
android:layout_width="94dp"
android:layout_height="94dp"
android:adjustViewBounds="false"
android:background="#drawable/one"
android:contentDescription="#string/nut3"
android:cropToPadding="false"
android:saveEnabled="false"
android:scaleType="center" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="94dp"
android:layout_margin="10dp"
android:text="#string/nut3" />
</LinearLayout>
</LinearLayout>

How to put a TEXTVIEW center on a IMAGEVIEW in Android Layout

I would like to know, how I can a put a text center on a image, here is the sample image,
I tried the below code, but its not working fine,
<LinearLayout
android:layout_width="100dip"
android:layout_height="fill_parent"
android:layout_marginRight="5dip"
android:gravity="center"
android:orientation="vertical" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/points"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="+2"
android:textColor="#fff"
android:textSize="16sp" />
<ImageView
android:id="#+id/btbt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/btbt" />
</TableRow>
</TableLayout>
</LinearLayout>
Please let me know a good solution.
you can use an imagebutton for this purpose
i will prefer using imagebutton
.. or
put this imageview in this order
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Relativelayout android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/points"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:text="+2"
android:textColor="#fff"
android:textSize="16sp" />
<ImageView
android:id="#+id/btbt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/btbt" />
</Relativelayout >
</TableRow>
You could try creating a textview with a background image. Something like:
<TextView
android:id="#+id/textview1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:textColor="#000000"
android:textStyle="bold"
android:text="Random Text"
android:background="#drawable/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

ScrollView cutting off part of the layout

I have a weird issue with a ScrollView in my layout, which is that it cuts off the bottom of the TableLayout within.
Here's what it looks like when scrolling to the top.
The green background with the rounded corners should be the same for the bottom, i.e. it's really just a plain rectangle drawable with rounded corners. But when I try to scroll to the bottom, here's what I see:
As you see, the bottom corners are invisible, I can't scroll any further. Now, I only see all lines of the Comments section because I manually added some android:paddingBottom to the TableLayout (see below), otherwise more would be cut off. And I just cannot figure out, what the problem is.
Can somebody tell me what I'm missing? Here is the XML layout. It is populated by a database query at runtime.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="30dp"
android:background="#drawable/some_shape"
android:paddingBottom="40dp" >
<TableRow android:layout_margin="20dp" >
<TextView
android:id="#+id/code_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="Code:" />
<TextView
android:id="#+id/code_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test" />
</TableRow>
<TableRow android:layout_margin="20dp" >
<TextView
android:id="#+id/name_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="Name:" />
<TextView
android:id="#+id/name_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test" />
</TableRow>
<TableRow android:layout_margin="20dp" >
<TextView
android:id="#+id/pra_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="Type:" />
<TextView
android:id="#+id/pra_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test" />
</TableRow>
<TableRow android:layout_margin="20dp" >
<TextView
android:id="#+id/day_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="Day:" />
<TextView
android:id="#+id/day_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test" />
</TableRow>
<TableRow android:layout_margin="20dp" >
<TextView
android:id="#+id/room_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="Room:" />
<TextView
android:id="#+id/room_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test" />
</TableRow>
<TableRow android:layout_margin="20dp" >
<TextView
android:id="#+id/time_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="Time:" />
<LinearLayout android:orientation="horizontal" >
<TextView
android:id="#+id/time_field1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="#string/to" />
<TextView
android:id="#+id/time_field2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test2" />
</LinearLayout>
</TableRow>
<TableRow android:layout_margin="20dp" >
<TextView
android:id="#+id/comment_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="Comments:" />
<TextView
android:id="#+id/comment_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test" />
</TableRow>
</TableLayout>
</ScrollView>
I know it's too late, but the answer is remove android:layout_margin="30dp" from TableLayout

How to leave spaces between views in android

Below there are two row views .......... How to leave a space between them i mean blank space between Views
What code do i need to add between the rows
I am not speaking abot padding between the rows, ......... I am
trying to achieve Blank space between
I am trying to achieve this in relative layout not grid layout
Any Ideas
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="2sp" >
<TextView
android:id="#+id/TimingsID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timings"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/BuyReservedButtonRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="2sp" >
<Button
android:id="#+id/BuyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
Just make your table-row like this, you can leave any space you need:
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText2"
android:gravity="center"
android:padding="2sp" >
<!-- HERE IS THE THING YOU NEED -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/TimingsID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timings"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp" >
</LinearLayout>
</LinearLayout>
<!-- MAKE SPACE AS MUCH AS YOU WANT, 20DP FOR EXAMPLE -->
</TableRow>
Hope this helps.
Use Space view between them like this
<Space
android:id="#+id/space1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/mainListView"
android:layout_alignParentBottom="true"
android:layout_marginBottom="150dp" />
You can use LinearLayout as well:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".SecondActivity" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="2sp" >
<TextView
android:id="#+id/TimingsID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timings"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/BuyReservedButtonRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="2sp" >
<Button
android:id="#+id/BuyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
</LinearLayout>
I think what you need are margins like android:layout_marginTop="10dp". Because in your code you already use padding, so I assume you know what happens if you use it. A margin will add some room around your View, depends on which margin you will use.
Try this:
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="2sp"
android:marginBottom="10dp"
>
<TextView
android:id="#+id/TimingsID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timings"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/BuyReservedButtonRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="2sp" >
<Button
android:id="#+id/BuyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>

Categories

Resources