How to set this tableLayout in android? - 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!

Related

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

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>

Layout elements weight problems

I'm trying to create xml for my ListView element.
Here is my *.xml file code:
<?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="horizontal">
<TextView
android:id="#+id/contactId"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:visibility="gone" />
<TextView
android:id="#+id/contactName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/contactLastName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textSize="15sp"
android:textStyle="bold" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="#+id/contactPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="3dp"
android:textSize="15sp"
android:textStyle="bold" />
</TableRow>
<TableRow>
<TextView
android:id="#+id/contactEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="3dp"
android:textSize="15sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
Every cell will have different value, and below is the picture of what I get, and what I'm trying to do:
layout.xml
Almost sure that solution is to use android:weight, but I have already try almost everything... I'll appreciate any help or hint
Maybe this is what you want to achieve
XML-
<?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="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="#+id/contactId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_blue_bright"
android:gravity="center"
android:text="Hello"
android:visibility="visible" />
<TextView
android:id="#+id/contactName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_green_dark"
android:gravity="center"
android:text="Hello2"
android:textSize="15sp"
android:textStyle="bold" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TableRow>
<TextView
android:id="#+id/contactPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/holo_red_dark"
android:padding="3dp"
android:text="Sample text 1"
android:textSize="15sp"
android:textStyle="bold" />
</TableRow>
<TableRow>
<TextView
android:id="#+id/contactEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/holo_orange_dark"
android:padding="3dp"
android:text="Sample text 2"
android:textSize="15sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
</LinearLayout>
Your linear layout needs to have the weightSum attribute of 100. Then the other elements for example 30 30 40 to achieve what youre looking for.
Try this,
<?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="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/contactName"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:gravity="center"
android:text="TEST NAME"
android:textSize="15sp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/contactLastName"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:gravity="center"
android:text="TEST LAST NAME"
android:textSize="15sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2">
<TextView
android:id="#+id/contactPhone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="0000000000"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/contactEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/contactPhone"
android:padding="3dp"
android:text="test#gmail.com"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>

TextView in the middle of ImageButton inside TableRow

I still didn`t a solution how to do that in XML. I want to center a textview over a imagebutton that are on a tablerow.
This is how should be:
My code:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:layout_below="#+id/button"
android:layout_marginTop="85dp">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="30dip" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:id="#+id/imageButton5"
android:src="#drawable/options_460"
android:layout_span="3"
android:layout_gravity="left"/>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:padding="0dip" >
<ImageButton
android:layout_height="wrap_content"
android:background="#null"
android:id="#+id/a"
android:src="#drawable/buttonsh"
android:layout_weight="1"
android:onClick="rasp"
android:tag="a"
android:layout_gravity="right"
/>
<ImageButton
android:layout_height="wrap_content"
android:background="#null"
android:id="#+id/b"
android:src="#drawable/buttonsh"
android:layout_weight="1"
android:onClick="rasp"
android:tag="b"
android:layout_gravity="left"/>
</TableRow>
............................................
</TableLayout>
Is there any way to do that? Any suggestion would be welcomed.
Thank you for reading.
First thing is that there is no need to take the table view, we have the easy alternative for it, you should try like below... here you have to just set a background 9.pach in your button and take appropriate id for the related component and you will bit your task. good luck
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:gravity="center"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:text="Middle center text" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="left upper" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="right upper" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="left bottom" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="right bottom" />
</LinearLayout>
</LinearLayout>

how to layout with right position

i have a screen and it is
i want the buttons to put under each other and the text view under each over , my code is
<LinearLayout
android:id="#+id/LL1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvTab1Ask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ask for new question" />
<Button
android:id="#+id/bMainTabsGetQuestion"
android:layout_width="116dp"
android:layout_height="33dp"
android:text="Get Question"
android:textSize="15px" />
</LinearLayout>
<LinearLayout
android:id="#+id/LL2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvTab1Join"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Join New Competition" />
<Button
android:id="#+id/bMainTabsJoin"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Join"
android:textSize="15px" />
</LinearLayout>
<LinearLayout
android:id="#+id/LL3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvTab1Signout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="or you can signout" />
<Button
android:id="#+id/bMainTabsSignout"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Sign out"
android:textSize="15px" />
</LinearLayout>
i mean each button have to start vertically at the same point
Try followin XML:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="#+id/row1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:weightSum="2" >
<TextView
android:id="#+id/tv1"
android:layout_column="0"
android:text="Ask for new Question"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="1" />
<Button
android:id="#+id/btn1"
android:text="Get Questiopn"
android:layout_column="1"
android:layout_weight="1" />
</TableRow>
<TableRow
android:id="#+id/row2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:weightSum="2" >
<TextView
android:id="#+id/tv2"
android:layout_column="0"
android:text="Join new Competition"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="1" />
<Button
android:id="#+id/btn2"
android:text="Join"
android:layout_column="1"
android:layout_weight="1" />
</TableRow>
<TableRow
android:id="#+id/row3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:weightSum="2" >
<TextView
android:id="#+id/tv3"
android:layout_column="0"
android:text="Or you can Signout"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="1" />
<Button
android:id="#+id/btn3"
android:text="Sign Out"
android:layout_column="1"
android:layout_weight="1" />
</TableRow>
</TableLayout>
It is perfectly working, see followin
I think that when you will make it with <TableLayout> it will works how you want.
Why don't you try to change orientation to vertical(something like android:orientation="vertical") of each linear layout if you want to set the buttons and texts vertically ?
Use RelativeLayout instead of LinearLayout
and use parameters like android:layout_alignLeft=""
android:layout_alignBottom="" to align it according to your designs.

Table Layout Alignment?

I have designed an XML layout as in this image:
I need to equally place the four buttons in the screen but I am having trouble getting it to work. I tried all possible changes that I can think of. Below is the layout's XML:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:id="#+id/buttonlayout"
android:layout_alignBottom="#id/framelayout">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow>
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/previous_icon" />
<Button
android:id="#+id/button_startprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/print_icon"
android:padding="10dp" />
<Button
android:id="#+id/button_cam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/cam_icon"
android:padding="10dp" />
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/next_icon"
android:hapticFeedbackEnabled="true"/>
</TableRow>
</TableLayout>
</RelativeLayout>
Can anyone help me out with this?
Hi jxgn use this code :
<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="#+id/button_startprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="#+id/button_cam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
</LinearLayout>
</TableRow>
</TableLayout>
use android:layout_weight=1, and Use android:layout_width="0dp" to make buttons of equal weights..
EDIT
for example define your button as
<Button
android:id="#+id/button_cam"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=1
android:background="#drawable/cam_icon"
android:padding="10dp" />
If you set the attribute android:weight="1" for all four buttons they should be spaced out properly as TableRow is a subclass of LinearLayout. For better performance it is suggested to use android:layout_width="0dp" as well.
Try to replace all your buttons width with layout_width="0dp" and add layout_weight="1"
Try to use 9-patch image to avoid the fact that your image get stretched
<TableRow>
<Button
android:id="#+id/previous"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/ic_menu_compass"
android:layout_weight="1" />
<Button
android:id="#+id/button_startprint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/ic_menu_mylocation"
android:padding="10dp"
android:layout_weight="1"/>
<Button
android:id="#+id/button_cam"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/ic_menu_mapmode"
android:padding="10dp"
android:layout_weight="1" />
<Button
android:id="#+id/next"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/ic_menu_about"
android:hapticFeedbackEnabled="true"
android:layout_weight="1"/>
</TableRow>
Try to set at each button:
android:layout_weight="0.25"
Hope this will already work for you
Try this it is usefull
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/buttonlayout" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow>
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
android:layout_weight="1"/>
<Button
android:id="#+id/button_startprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
android:layout_weight="1"
/>
<Button
android:id="#+id/button_cam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
android:layout_weight="1" />
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
android:layout_weight="1" />
</TableRow>
</TableLayout>
</RelativeLayout>
Applying android:layout_weight="1" in your xml will stretch your button images.
If you dont want to stretch button images use ImageButton and set android:src to your drawables.
Refer below code:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<ImageButton
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/previous_icon"
android:background="#android:color/transparent"
android:layout_weight="1" />
<ImageButton
android:id="#+id/button_startprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/print_icon"
android:background="#android:color/transparent"
android:padding="10dp"
android:layout_weight="1" />
<ImageButton
android:id="#+id/button_cam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/cam_icon"
android:background="#android:color/transparent"
android:padding="10dp"
android:layout_weight="1" />
<ImageButton
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/next_icon"
android:background="#android:color/transparent"
android:hapticFeedbackEnabled="true"
android:layout_weight="1" />
</TableRow>
</TableLayout>
`

Categories

Resources