A Timetable that show ongoing Activities( eg Classes, Testes ) within a Week - android

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>

Related

Aligning Two LinearLayout Side By Side Inside a CardView

I am trying to align two TextViews OR LinearLayouts side by side in order to reach this cardView:
I have used layout_gravity and layout_weight attributes inorder to solve this situation, but with no luck.
I am open to any other suggestions that may accomplish my goal as log as I can generate cardview dynamicaly using BaseAdapter with ListView in the Activity Code.
I am looking for XML solution.
Here is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:padding="5dp">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="5sp"
card_view:cardElevation="5sp"
card_view:contentPadding="16dp"
android:orientation="horizontal"
android:layout_weight="2">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="2">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="left"
android:gravity="left"
android:layout_weight="1">
<TextView
style="#style/Base.TextAppearance.AppCompat.Headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16dp"
android:textColor="#ec1c24"
android:text="Model Name" />
<TextView
style="#style/Base.TextAppearance.AppCompat.Body1"
android:id="#+id/plc_ModelName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="36dp"
android:textColor="#666666"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="right"
android:gravity="right"
android:layout_weight="1">
<TextView
android:id="#+id/plc_localIp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp" />
<TextView
android:id="#+id/plc_machineType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16dp" />
<TextView
android:id="#+id/plc_ModelId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16dp" />
<TextView
android:id="#+id/plc_Version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Many thanks.
For the part on the right you can use a TableLayout.
Try to play with nesting layouts or switch to ConstraintLayout for better performance. Too many nested layout weights will quickly add up and create performance issues.
You can start fine tuning from the XML provided below. I removed the layout_weight from the CardView, if needed, add it back.
In preview it looks like this:
Layout XML:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal"
card_view:cardCornerRadius="5sp"
card_view:cardElevation="5sp"
card_view:contentPadding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
style="#style/Base.TextAppearance.AppCompat.Headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Model Name"
android:textColor="#ec1c24"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="#+id/plc_ModelName"
style="#style/Base.TextAppearance.AppCompat.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#666666"
android:textSize="36dp"
android:textStyle="bold"
tools:text="T-Max" />
</LinearLayout>
<TableLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="Model ID:"
android:textColor="#ec1c24"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="#+id/plc_ModelId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:textSize="14dp"
tools:text="12345678" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="IP:"
android:textColor="#ec1c24"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="#+id/plc_localIp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:textSize="14dp"
tools:text="192.168.0.1" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="BS Version:"
android:textColor="#ec1c24"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="#+id/plc_Version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:textSize="14dp"
tools:text="3.0.2.111" />
</TableRow>
</TableLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>

Prevent scrolling when in portrait mode android

I have an xml which uses linear layout and weights to equally distribute space on the screen to a few buttons and such. When I rotate screen to landscape mode, the buttons all get compressed. Could someone tell me how to make the buttons take equal space, non scrollable, when in portrait, and the whole screen scrollable when in landscape. Presently I am not using any scrollview and the layout is looking good, with the table and buttons taking the right amount of space, but when i rotate the screen to landscape, everything gets compressed. I tried enclosing the Linear Layout inside a scrollview but that caused the Linear Layout to move out of the screen, so when in portrait mode, i have to scroll to see whole content. Here is my xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/linear"
android:orientation="vertical"
android:background="#bf000000"
tools:context=".MainActivity"
android:layout_above="#+id/imageView">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtReceive1"
android:visibility="gone"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/table"
android:background="#fff3f3f2"
android:stretchColumns="*"
android:shrinkColumns="*"
>
<TableRow>
<TextView
android:text="CH"
android:gravity="center"
android:padding="8dip"
android:layout_width="wrap_content" />
<TextView
android:text="kWh"
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content" />
<TextView
android:text="Load"
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content" />
<TextView
android:text="Date"
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content" />
</TableRow>
<TableRow >
<TextView
android:padding="8dip" android:layout_width="wrap_content"
android:gravity="center"
android:id="#+id/t11"
android:text="1" />
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content"
android:id="#+id/t12" />
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content"
android:id="#+id/t13" />
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content"
android:id="#+id/t14" />
</TableRow>
<TableRow>
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content"
android:id="#+id/t21"
android:text="2" />
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content"
android:id="#+id/t22" />
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content"
android:id="#+id/t23" />
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content"
android:id="#+id/t24" />
</TableRow>
<TableRow>
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content"
android:id="#+id/t31"
android:text="3" />
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content"
android:id="#+id/t32" />
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content" />
<TextView
android:gravity="center"
android:padding="8dip"
android:layout_width="wrap_content"
android:id="#+id/t34" />
</TableRow> <TableRow>
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content"
android:id="#+id/t41"
android:text="4" />
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content"
android:id="#+id/t42" />
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content"
android:id="#+id/t43" />
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content"
android:id="#+id/t44" />
</TableRow>
<TableRow >
<TextView
android:padding="8dip" android:layout_width="wrap_content"
android:gravity="center"
android:id="#+id/t51"
android:text="5" />
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content"
android:id="#+id/t52" />
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content"
android:id="#+id/t53" />
<TextView
android:gravity="center"
android:padding="8dip" android:layout_width="wrap_content"
android:id="#+id/t54" />
</TableRow>
</TableLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff8c00"
android:padding="6dip"
android:id="#+id/txtReceive"
android:layout_below="#+id/table"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/lin2"
android:visibility="visible"
android:paddingTop="5dp"
android:weightSum="2">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:visibility="visible"
android:weightSum="4"
android:id="#+id/rel">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:layout_height="fill_parent"
android:id="#+id/imageButton1"
android:layout_width="fill_parent"
android:background="#drawable/btn_01_small"
android:textColor="#ffa5ff45"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Channel"
android:textSize="22sp"
android:id="#+id/textView3"
android:layout_alignTop="#+id/imageButton1"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#ffa5ff45"
android:id="#+id/textView9"
android:textSize="22sp"
android:layout_alignBottom="#+id/imageButton1"
android:layout_centerHorizontal="true" />
</RelativeLayout><RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:id="#+id/imageButton2"
android:background="#drawable/btn_02"
android:textColor="#ffa5ff45"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kWh"
android:textSize="22sp"
android:id="#+id/textView6"
android:layout_alignTop="#+id/imageButton2"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="000.0"
android:textColor="#ffa5ff45"
android:id="#+id/textView10"
android:textSize="22sp"
android:layout_alignBottom="#+id/imageButton2"
android:layout_centerHorizontal="true" />
</RelativeLayout><RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:id="#+id/imageButton3"
android:background="#drawable/btn_03"
android:textColor="#ffa5ff45"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load"
android:id="#+id/textView7"
android:textSize="22sp"
android:layout_alignTop="#+id/imageButton3"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="000"
android:textColor="#ffa5ff45"
android:id="#+id/textView11"
android:textSize="22sp"
android:layout_alignBottom="#+id/imageButton3"
android:layout_centerHorizontal="true" />
</RelativeLayout><RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:id="#+id/imageButton4"
android:background="#drawable/btn_04_small" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Days"
android:id="#+id/textView8"
android:textSize="22sp"
android:layout_alignTop="#+id/imageButton4"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="000"
android:textColor="#ffa5ff45"
android:id="#+id/textView12"
android:textSize="22sp"
android:layout_alignBottom="#+id/imageButton4"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_alignParentRight="true"
android:visibility="visible"
android:weightSum="4"
android:layout_below="#+id/txtReceive"
android:id="#+id/rel2">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:layout_height="fill_parent"
android:id="#+id/imageButton12"
android:layout_width="fill_parent"
android:background="#drawable/btn_01_small"
android:textColor="#ffa5ff45"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Auto ON"
android:id="#+id/textView32"
android:textSize="22sp"
android:layout_alignTop="#+id/imageButton12"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:textColor="#ffa5ff45"
android:id="#+id/textView92"
android:textSize="22sp"
android:layout_alignBottom="#+id/imageButton12"
android:layout_centerHorizontal="true" />
</RelativeLayout><RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:id="#+id/imageButton22"
android:background="#drawable/btn_04_small"
android:textColor="#ffa5ff45"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Auto OFF"
android:textSize="22sp"
android:id="#+id/textView62"
android:layout_alignTop="#+id/imageButton22"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:textColor="#ffa5ff45"
android:id="#+id/textView102"
android:textSize="22sp"
android:layout_alignBottom="#+id/imageButton22"
android:layout_centerHorizontal="true" />
</RelativeLayout><RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:id="#+id/imageButton31"
android:layout_marginTop="7dp"
android:layout_marginBottom="-14dp"
android:background="#drawable/send_btn_01"
android:textSize="22sp"
android:text="SET TIME" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scaleType="matrix"
android:layout_marginBottom="-7dp"
android:id="#+id/imageButton32"
android:padding="0dp"
android:background="#drawable/send_btn_01"
android:textSize="22sp"
android:text="RECHARGE" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/base_irya_left1"
android:background="#bf000000"
android:adjustViewBounds="true"
android:layout_alignParentBottom="true" />
<!-- </RelativeLayout> -->
Did you consider using Fragments in Android. Here is a tutorial.
How to support different screensizes with fragments(See here)
It is possible to define in the layout file of an activity that it contains fragments (static definition) or to modify the fragments of an activity at runtime (dynamic definition).
To display differnet fragments in your activities based on the actual available space you can:
Use one activity, which displays two fragments for tablets and on handset devices. In this case change at runtime the fragments displayed by the activity whenever necessary. In this scenario you typically define instances of the FrameLayout class as placeholder in your layout and add the fragments at runtime to them.
Use separate activities to host each fragment on a handset. For example, when the tablet UI uses two fragments in an activity, use the same activity for handsets, but supply an alternative layout that includes just one fragment. If the detailed fragment is there, the main activity tells the fragment that it should update itself. If the detail fragment is not available, the main activity starts the detailed activity.
Which option to select depends on the use case, typical the dynamic contribution is more flexible bit a bit harder to implement.
Creating two xmls, one for landscape and one for portrait, will fix the issue. Portrait goes into res/layout folder and landscape goes into res/layout-land. The two should have identical names for this to work.

Create TextViews with same width in 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.

Table layout column spacing

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

How to set this tableLayout in android?

I want to Implement this type of View in My Application. . .
data1 hello hi
data2 hello2 hi
data3 hi Hello
I want to know, which one is the best way to set it ??
TableLayout oranyother linearLayout.RelativeLayout ???
I also want the xml code to set this layout. .
Thanks.
Try this,
<?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">
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow android:weightSum="3">
<TextView android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="hello" />
<TextView android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="hello" />
<TextView android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="hello" />
</TableRow>
<TableRow android:weightSum="3">
<TextView android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="hello" />
<TextView android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="hello" />
<TextView android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="hello" />
</TableRow>
<TableRow android:weightSum="3">
<TextView android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="hello" />
<TextView android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="hello" />
<TextView android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="hello" />
</TableRow>
</TableLayout>
</LinearLayout>
TableLayout is preferrable is your case.
Example: http://www.tutorialforandroid.com/2008/12/simple-tablelayout-in-android.html
#note: just don't be so lazy to Google it!

Categories

Resources