I am trying to format my tablelayout but I dont seem to get things working quite well.
I have 4 columns that I need them spaced as nice as possible. I was thinking really having 3 columns with same width and the last column taking the rest of the space. The last column is ok to wrap around in 2 or more lines but the first 3 columns has to be single line and NOT truncated. Here is my code
<TableLayout android:id="#+id/tlMylayout" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:stretchColumns="*" android:shrinkColumns="*" >
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" style="#style/showtext_style" android:text="col1" />
<TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" style="#style/showtext_style" android:text="col2" />
<TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" style="#style/showtext_style" android:text="col3" />
<TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" style="#style/showtext_style" android:text="lastCol" />
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="2011-01-01 14:59" />
<TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:text="50" />
<TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:text="0.57" />
<TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is the last one and can be very long string if I want it to be" />
</TableRow>
</TableLayout>
Try
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tlMylayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:text="col1" />
<TextView
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:text="col2" />
<TextView
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:text="col3" />
<TextView
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:text="lastCol" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:text="2011-01-01 14:59" />
<TextView
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:singleLine="true"
android:text="50" />
<TextView
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:singleLine="true"
android:text="0.57" />
<TextView
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:text="this is the last one and can be very long string if I want it to be" />
</TableRow>
I removed layout_gravity and added layout_weight to TextView
Related
How to display schedule like (Classes, Testes and so on) for college App Using a Timetable, for eg like this
I was thinking of extending FrameLayout and customize it in some way, Any help?
Thanks.
You can use TableLayout to design the desired view. According to documentation TableLayout is A layout that arranges its children into rows and columns. This I think is the best suit for you` as you have given number of rows and columns.
Here is an example of TableLayout you can extend to fit your needs:
<?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"
android:layout_marginTop="100dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<TableRow android:background="#0079D6" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="title 1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="title 2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="title 3" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Suresh Dasari" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hyderabad" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rohini Alavala" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Guntur" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Trishika Dasari" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Guntur" />
</TableRow>
Below I would like to strech the pool_team column and have the others shrink to the column width. I can't seem to get it right though. The other columns will contain an integer of small size and the pool_team will contain a long name that may wrap.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pool_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:shrinkColumns="0">
<TableRow
android:id="#+id/pool_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="#+id/pool_number"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textAlignment="center" />
<TextView
android:id="#+id/pool_team"
android:layout_width="0dip"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/pool_wins"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textAlignment="center" />
<TextView
android:id="#+id/pool_loses"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textAlignment="center" />
<TextView
android:id="#+id/pool_point_difference"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textAlignment="center" />
<TextView
android:id="#+id/pool_place"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textAlignment="center" />
</TableRow>
</TableLayout>
Something like this?
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pool_table"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/pool_row"
android:weightSum="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="#+id/pool_number"
android:layout_width="0dp"
android:layout_weight=".1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="123" />
<TextView
android:id="#+id/pool_team"
android:layout_width="0dp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Some longer text that may or may not wrap around to the next line...." />
<TextView
android:id="#+id/pool_wins"
android:layout_weight=".1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="123" />
<TextView
android:id="#+id/pool_loses"
android:layout_weight=".1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="123" />
<TextView
android:id="#+id/pool_point_difference"
android:layout_width="0dp"
android:layout_weight=".1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="123" />
<TextView
android:id="#+id/pool_place"
android:layout_weight=".1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="123" />
</TableRow>
I'm facing a problem: I'd like to create a grid of icons for my main screen and so I'm using 4 TableRows and 3 TextViews in each.
The TextViews are made of an icon and a text, the text is of different length.
I don't know what to write in the xml file to make all those TextViews the same width in a row.
I displayed their size in logcat so I'm sure they're not of the same width ;)
I've tried m1shk4's solution, but that doesn't work.
<?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"
android:clickable="true" >
<TableRow
android:id="#+id/tableRow1"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="#+id/textView1"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="match_parent"
android:clickable="true"
android:drawableTop="#drawable/nearby"
android:gravity="center"
android:onClick="goToArticles"
android:text="All Nearby" />
<TextView
android:id="#+id/textView2"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:layout_width="match_parent"
android:drawableTop="#drawable/supermarket"
android:gravity="center"
android:onClick="goToMap"
android:text="Supermarkets" />
<TextView
android:id="#+id/textView3"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="match_parent"
android:drawableTop="#drawable/pharmacy"
android:gravity="center"
android:text="Pharmacy" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="#+id/textView4"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="match_parent"
android:drawableTop="#drawable/fastfood"
android:gravity="center"
android:text="Fastfood" />
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/departmentstores"
android:gravity="center"
android:onClick="displaySizes"
android:text="Department Stores" />
<TextView
android:id="#+id/textView6"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="match_parent"
android:drawableTop="#drawable/petrol"
android:gravity="center"
android:text="Petrol Stations" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="#+id/textView7"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="#drawable/electronics"
android:gravity="center"
android:layout_width="match_parent"
android:text="Electronics" />
<TextView
android:id="#+id/textView8"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="match_parent"
android:drawableTop="#drawable/diy"
android:gravity="center"
android:text="DIY Stores" />
<TextView
android:id="#+id/textView9"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="match_parent"
android:drawableTop="#drawable/banking"
android:gravity="center"
android:text="Banks" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="#+id/textView10"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="match_parent"
android:drawableTop="#drawable/fashion"
android:gravity="center"
android:text="Fashion & Clothing" />
<TextView
android:id="#+id/textView11"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="match_parent"
android:drawableTop="#drawable/autoparts"
android:gravity="center"
android:text="Auto Service" />
<TextView
android:id="#+id/textView12"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="match_parent"
android:drawableTop="#drawable/more"
android:gravity="center"
android:text="All Categories" />
</TableRow>
</TableLayout>
Any idea?
Thanks :)
Since TableRow represents a horizontally oriented LinearLayout, the weight attribute should be used with layout_width="0dp" for the child views. So in order to make each TextView in the TableRow be same width replace
android:layout_width="match_parent"
with
android:layout_width="0dp"
for every TextView in a row.
I am a beginner and I have designed a layout that displays the rows from the XML upon the click of a "show" Button.
But when I don't click the show button, I want the table row with id-blank to increase its height to fit the screen.
Please suggest a way to do that.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Row 1 with single column -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
android:gravity="center_horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="Sale Report"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<!-- Row 2 with 3 columns -->
<LinearLayout
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#drawable/borders"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<EditText
android:id="#+id/starttime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:inputType="none"
android:textSize="#dimen/textSize" />
<EditText
android:id="#+id/stoptime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:inputType="none"
android:textSize="#dimen/textSize" />
<Button
android:id="#+id/show"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight=".75"
android:text="show" />
<Button
android:id="#+id/back"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight=".75"
android:text="back" />
</LinearLayout>
<!-- Row 3 with 2 columns -->
<TableLayout
android:id="#+id/sale_table_data"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:background="#drawable/borders"
android:gravity="center" >
<TableRow
android:id="#+id/sale_report_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/Yellow" >
<TextView
android:id="#+id/particulars"
android:layout_weight="2"
android:gravity="center_horizontal"
android:text="Particulars"
android:textStyle="bold" />
<TextView
android:id="#+id/voucherNo"
android:layout_weight="1"
android:gravity="fill_horizontal"
android:text="VCH No"
android:textStyle="bold" />
<TextView
android:id="#+id/quantity"
android:layout_weight="1"
android:gravity="fill_horizontal"
android:text="Quantity"
android:textStyle="bold" />
<TextView
android:id="#+id/amount"
android:layout_weight="2"
android:gravity="center_horizontal"
android:text="Amount"
android:textStyle="bold" />
<TextView
android:id="#+id/date"
android:layout_weight="1.3"
android:gravity="center_horizontal"
android:text="Date"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/blank"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3" >
<TextView
android:id="#+id/blank9"
android:layout_weight="2"
android:gravity="center_horizontal"
android:text="Particulars"
android:textStyle="bold" />
<TextView
android:id="#+id/blank10"
android:layout_weight="1"
android:gravity="fill_horizontal"
android:text="VCH No"
android:textStyle="bold" />
<TextView
android:id="#+id/blank11"
android:layout_weight="1"
android:gravity="fill_horizontal"
android:text="Quantity"
android:textStyle="bold" />
<TextView
android:id="#+id/blank12"
android:layout_weight="2"
android:gravity="center_horizontal"
android:text="Amount"
android:textStyle="bold" />
<TextView
android:id="#+id/blank13"
android:layout_weight="1.3"
android:gravity="center_horizontal"
android:text="Date"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/no_borders" >
<TextView
android:id="#+id/blank1"
android:layout_weight="1"
android:gravity="center_horizontal"
android:textStyle="bold" />
<TextView
android:id="#+id/blank2"
android:layout_weight="1"
android:gravity="center_horizontal"
android:textStyle="bold" />
<TextView
android:id="#+id/Total"
android:layout_weight="1"
android:gravity="center"
android:text="Total"
android:textStyle="bold" />
<TextView
android:id="#+id/total_sales"
android:layout_weight="2"
android:gravity="center_horizontal"
android:text="Amount_value"
android:textStyle="bold" />
<TextView
android:id="#+id/blank3"
android:layout_weight="1"
android:gravity="center_horizontal"
android:textStyle="bold" />
</TableRow>
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
I am using Table Layout.
Here i have three EditText's with same width ="200dp".
But i want to increase the size of 3rd EditText.
I made its width="wrap_content" but all other EditText also getting "wrap_content"
How can i increase the width of 3rd EditText with out affecting other EditText's.
This code for 3rd EditText:-
<TableRow
android:gravity="center_horizontal"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:id="#+id/addl"
android:text="Address"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:id="#+id/colon"
android:text=":" />
<EditText
android:layout_width="250dp"
android:layout_marginLeft="20dp"
android:id="#+id/et"
android:singleLine="true"/>
</TableRow>
Now i want to increase width of Address EditText only
i think you can achieve this by layout_weight parameter of EditTexts like below
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout12"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#909090"
android:padding="10dp" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student ID"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text=":"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="6" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text=":"
android:textColor="#000000" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text=":"
android:textColor="#000000" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text=":"
android:textColor="#000000" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</TableRow>
</TableLayout>
EDIT
This is the outcome
put the property android:stretchColumns="2" in TableLayout
<TableLayout android:stretchColumns="2" ... > .... </TableLayout>