In Android I'm trying to use this xml to get a TableLayout next to a LinearLayout. But I can't get it working. The TableLayout always takes up the full width of the screen. How can I get it to share the screen width with the LinearLayout?
<LinearLayout android:layout_width="fill_parent" android:weightSum="1" android:layout_height="wrap_content" android:orientation="horizontal">
<TableLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.5">
<TableRow>
<TextView android:text="Name1:" />
<TextView android:text="Value1" />
</TableRow>
<TableRow>
<TextView android:text="Name2:" />
<TextView android:text="Value2" />
</TableRow>
</TableLayout>
<LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.5"></LinearLayout>
</LinearLayout>
Like stated above, don't put the table width as 0dp. I would make the root element a Relative layout then state that the linear layout should be to the right of the table layout.
this says that your layout width of the linear layout is 0dp. make it wrap content and have the table layout with weight: 1 > linear: 0 and table layout set to fill parent.
Related
It is possibile in Android to auto adapt bottons's width that are on the same line without write the specify width in the layout file?
I ask this question because with AppInvetor it is possibile to auto adapat bottons's width and this is the reason why i ask you this question.
You can use TableLayout to do this. Just add the buttons to TableRows and give them match_parent width
One way is a table:
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<Button />
<Button />
</TableRow>
<\TableLayout>
Alternatively, you can do a horizontal linear layout and assign a weight to each button with a width of 0.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="0dp"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_weight="1" />
<\LinearLayout>
I have this XML layout in my App (example of one button):
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollViewMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff0e8" >
///some views
<Button
android:id="#+id/btnVysledkySportka"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/chckSprotka"
android:layout_alignRight="#+id/chckSprotka"
android:layout_alignTop="#+id/chckSprotka"
android:layout_margin="3dp"
android:layout_toRightOf="#+id/textView3"
android:background="#ffe118"
android:gravity="center"
android:onClick="vysledkySportka"
android:text="Archiv výsledků"
android:textStyle="bold" />
///some views
</RelativeLayout>
</ScrollView>
But when I start my app at Android 4.3, the text of buttons isn´t in center of button. Look at the screenshot (in red rectangle):
Where can be problem?
EDIT:
Whole layout
When you specify:
android:layout_alignBottom="#+id/chckSprotka"
android:layout_alignRight="#+id/chckSprotka"
android:layout_alignTop="#+id/chckSprotka"
It does make the bottom/right/top edge of your button match the bottom edge of the given anchor view ID & accommodates bottom/right/top margin, but while doing that, the android_gravity does not take the resultant height/width into consideration.
So the gravity of the text is center according to wrap_content for layout_height and layout_width.
You can verify that by setting values for layout_height and layout_width (Eg. 200dp and 100dp to try with) and you will get the text with gravity center but for that height and width.
To confirm the same, what you can do is use a container LinearLayout for your Button like:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/chckStastnych"
android:layout_alignRight="#+id/chckStastnych"
android:layout_alignTop="#+id/chckStastnych"
android:layout_margin="3dp"
android:layout_toRightOf="#+id/textView3"
android:background="#ffe118" >
<Button
android:id="#+id/btnVysledkyStastnych"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffe118"
android:gravity="center"
android:onClick="vysledkyStastnych"
android:text="Archiv výsledků"
android:textStyle="bold" />
</LinearLayout>
Set the gravity of LinearLayout as center and then center the Button within or as shown above, use layout_gravity for the button to center it in parent LinearLayout.
This will work as a solution when you do that for all 4 buttons, however there might be better options if you restructure your xml and avoid this kind of nesting.
Try This:
<Button
android:id="#+id/btnVysledkySportka"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/chckSprotka"
android:layout_alignRight="#+id/chckSprotka"
android:layout_alignTop="#+id/chckSprotka"
android:layout_margin="3dp"
android:layout_toRightOf="#+id/textView3"
android:background="#ffe118"
android:gravity="center_vertical"
android:onClick="vysledkySportka"
android:text="Archiv výsledků"
android:textStyle="bold" />
I have two cases with Linear (also tried Relative) layout in android. The one happens for horizontal and the other for vertical. Lets start with the horizontal:
it is something like:
<LinearLayout ... >
<Button ... layout:gravity = "left" layout:width = "wrap_content"/>
<TextView ... layout:width = ??????? />
<Image .... layout:gravity = "right" layout:width = "wrap_content"/>
</LinearLayout>
Well , I want the button to stay at the left, the image to stay at the right(stick to the end , not just right of the text view) and the textview to (probably with an autowidth or whatever) to stay in the middle. If I put in textview width = "fill/match_parent it sends the image out of the screen. If I put wrap_content, then the image does not stay at the right of the screen. I have also tried relative layout without success.
Same case in vertical , where I have something like:
<LinearLayout ...>
<LinearLayout .... layout:height = "wrap_content" layout:gravity= "top" />
<ListView layout:height = ???????>
<LinearLayout ... layout:height = "wrap_content" layout:gravity = "bottom" />
</LinearLayout>
Same requirement here. I want the first L.layout to stay on top, List view auto size between them and the 2nd Linear layout to stay at the bottom. (Imagine I'm trying to create a view that looks like a UITableView in iPhone that has a NavigationBar, the list of items and a ToolBar at the bottom. Fist LinearLayout is the NavigationBar, the LIst view are the cells and the second LinearLayout is the ToolBar).
Any suggestions? Would prefer the xml solutions.
It can simply done by using RelativeLayout here we go.
Horizontal alignment
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="something"
/>
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/image"
android:layout_toRightOf="#+id/button" />
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/icon" />
</RelativeLayout>
Vertical alignment
<LinearLayout
android:id="#+id/linear_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linear_top"
android:layout_above="#+id/linear_bottom" />
<LinearLayout
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
For your second requirement use layout_weight for list view
<RelativeLayout ...>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentTop="true" />
<ListView
android:layout_height ="0dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_below="#+id/top_layout"
android:layout_above="#+id/bottm_layout" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true" />
</RelativeLayout>
You may try TableLayout instead LinearLayout with Stretch Column is 1 (Columns in Table LAyout are indexed base 0).
First column is your button.
Second column is your textview.
Third column is your image.
You can google about example or see TableLayout.
Updated:
You can see this for clearer:
<RelativeLayout>
<!-- This is Header-->
<LinearLayout
.....
android:layout_alignParentTop="true" >
</LinearLayout>
<!-- This is your table-->
<TableLayout
.....
android:stretchColumns = 1>
<TableRow>
<Button>
.....
</Button>
<TextView>
.....
</TextView>
<ImageView>
.....
</ImageView>
</TableRow>
</TableLayout>
<!-- This is Footer-->
<LinearLayout
.....
android:layout_alignParentBottom="true" >
</LinearLayout>
</RelativeLayout>
Here is the issue.
I'm creating a TableLayout containing a some ImageButtons.
Each row has 3 ImageButtons but their width does not equal the width of the table.
Also the same applies for their height.
All I want to do is center the ImageButtons within the cell they are in.
The
auto:StretchColumns="*"
stretches the ImageButtons (cell data), even I have specified their size in the XML file.
Also, are there any options to do the same for the rows?
Something like calculating the margins of the cells automatically.
Have you tried setting the android:layout_gravity="center" for the ImageButtons?
Alternatively, you could wrap each ImageButton in a LinearLayout like this:
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageButton android:layout_gravity="center" ...other attributes... />
</LinearLayout>
Children of the Table Layout do not have to be only TableRow. The Table Layout supports children of all types including LinearLayout, TextView, ImageButton, etc.
Do not wrap your image button in a TableRow. Insert it as a row.
<TableRow>
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example Table Row" />
</TableRow>
<ImageButton
android:id="#+id/ib1"
android:layout_width="fill_parent" <---! The Parent Is The Table Layout !--->
android:layout_height="wrap_content"
android:background="#drawable/coolBGImage" />
<TableRow>
BLAH BLAH BLAH
</TableRow>
To put 3 ImageButtons in one row use a LinearLayout with android:orientation="horizontal"
</TableRow>
<LinearLayout
android:id="#+id/LL1"
android:layout_width="fill_parent" <---! The Parent is the TableLayout !--->
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/IB1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/coolBGImage1"
android:weight="1" />
Add the next two image buttons with the same weight to make the buttons divide the row evenly.
</LinearLayout>
as the title means, I want my tablelayout to evenly assign each column's width and the child of each column be centered inside the column. Setting android:stretchColumns="*" also, however, streches the child inside each column.
any ideas for hacking this issue?
using horizontal Linearlayout to imitate a tablerow is acceptable, but I also have no idea how to get a Linearlayout to arrange its children evenly :(
pls help,
thanks in advance.
No hack needed. :-)
Simply set the layout_width of the children to wrap_content instead of fill_parent, or to a fixed value. Set layout_gravity to center or center_horizontal.
EDIT: Code added.
Try this:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:shrinkColumns="*" android:stretchColumns="*">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:gravity="center"
/>
</TableRow>
</TableLayout>