Android: Table Layout alignment - android

I want 'Duration' and 'Cost' Right aligned. I tried this code but it's not working.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.markupartist.android.widget.ActionBar
android:id="#+id/actionBar"
style="#style/ActionBar" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:text="Date" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:text="Couse Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:layout_gravity="right"
android:text="Duration" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:layout_gravity="right"
android:text="Cost" />
</TableRow>
</TableLayout>
</LinearLayout>
layout_gravity="right" not working.
I trird gravity="ritght" but that's also not working.
I also want each column to center aligned.

give weight for Table row and all the text views...
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:text="Date" android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:text="Couse Name" android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingLeft="5dip"
android:text="Duration" android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:text="Cost" android:layout_weight="1"/>
</TableRow>
</TableLayout>

Linear Layout is fine... Check this..
`
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:layout_weight="1"
android:gravity="center"
android:text="Date" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:layout_weight="1"
android:gravity="center"
android:text="Couse Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingLeft="5dip"
android:layout_weight="1"
android:gravity="center"
android:text="Duration" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:layout_weight="1"
android:gravity="center"
android:text="Cost" />
</TableRow>
</TableLayout>
`
Change each layout_weight as you wish.. Hope this helps.. Good luck...

Hey you can use Relative layout so you can place your text views any where you want...
below is a sample code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<TextView
android:id="#+id/usage"
android:layout_marginTop="220dip"
android:layout_marginLeft="45dip"
android:layout_marginRight="15dip"
android:typeface="serif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textColor="#030900"/>
</RelativeLayout>

For the same kind of layout, its better to use RelativeLayout.
Why to use RelativeLayout?
Just because we can set views by using below attributes:
android:layout_toRightOf
android:layout_toLeftOf
android:layout_alignParentRight
android:layout_alignParentLeft
android:layout_below
and many more attributes RelativeLayout provides to set views.
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:text="Date"
android:id="#+id/txtDate"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:text="Couse Name"
android:layout_toRightOf="#+id/txtDate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:layout_gravity="right"
android:text="Duration"
android:layout_toLeftOf="#+id/txtCost" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:layout_gravity="right"
android:text="Cost"
android:layout_alignParentRight="true"
android:id="#+id/txtCost" />
</RelativeLayout>

Related

Unable to align checkbox(horizontally/vertically) in Android app

I need to display checkbox. All the check-boxes should align themselves at the extreme right of the screen.However, if I change the orientation of my screen the check-box comes at the middle of the screen.Please help me to solve the issue.Thanks in advance
<?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"
android:background="#000000"
>
<LinearLayout android:id="#+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:orientation="vertical"
android:layout_margin="10dp">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sf_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:background="#drawable/test"
>
<View android:id="#+id/Divider_7"
android:layout_height="0.5dp"
android:layout_width="fill_parent"
android:background="#808080"
android:layout_marginBottom="5dp"
/>
<TableRow
android:id="#+id/tbr6"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_margin="2dip"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
>
<TextView
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:textColor="#3399CC"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pages For Website (existing pages plus new ones you need)-Contact Us"
></TextView>
</TableRow>
<View android:id="#+id/Divider_2"
android:layout_height="1.5dp"
android:layout_width="fill_parent"
android:background="#C0C0C0"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
/>
<TableRow
android:id="#+id/tbr9"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_margin="2dip"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
>
<TextView
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Feedback"
></TextView>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/feedback_id"
android:layout_gravity="right"
></CheckBox>
</TableRow>
<TableRow
android:id="#+id/tbr9"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_margin="2dip"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
>
<TextView
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Tech Support"
></TextView>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/techsupport_id"
android:layout_gravity="right"
></CheckBox>
</TableRow>
<TableRow
android:id="#+id/tbr9"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_margin="2dip"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
>
<TextView
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Quote Form"
></TextView>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/quoteform_id"
android:layout_gravity="right"
></CheckBox>
</TableRow>
</TableLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>
try the following
android:layout_gravity="right"
android:gravity="right"
android:layout_gravity is not relevant in a LinearLayout. It is meant for a FrameLayout. It means "what should be my gravity in relation to my (FrameLayout) father". android:gravity on the other hand means "what is the gravity inside me" and it applies to different views differently (like in a TextView it will align the text according to the gravity).
I think you could probably throw out half of the layouts you are using to display views (it's kind of confusing and hard to read) by simplifying your layout scheme and use just using a RelativeLayout for alignment issues (align to parent or to other views) and LinearLayout for grouping vertical and horizontal groups.
use tablelayout an then use tablerowlayout. and put your checkboxes.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#ffffffff"
android:text="ARE YOU CLASS 2B?" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="50dp"
android:layout_height="50dp" >
<RadioGroup
android:id="#+id/radioGroup" android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="#+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes" />
<RadioButton
android:id="#+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</RadioGroup>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/jimmy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jimmy" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/diana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Diana" />
</TableRow>
<TableRow
android:id="#+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/dina"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dina" />
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/jack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jack" />
</TableRow>
<TextView
android:id="#+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TableRow
android:id="#+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
</LinearLayout>
It's example hehe

Why my Android Listview is not scrolling?

I have made a xml display with some items . But my listView of that xml is not scrolling. I have made a scrollView and the listView is inside the scrollView , is it a problem ? If I design display with keeping the listView out side the scrollView, then the lisViw is not showing on my android device display. I need such a display where need a scrollView and scrolling listview. My code is given below.
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/head_logo" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/cardlistButton"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/head_menu04" />
<View
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#ffffff" />
<Button
android:id="#+id/cameraButton"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/head_menu02" />
<View
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#ffffff" />
<Button
android:id="#+id/homeButton"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/head_menu03" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/title_detail"
android:gravity="right|center_vertical" >
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:orientation="vertical"
android:paddingBottom="10dip"
android:paddingTop="15dip" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="7dip"
android:orientation="horizontal" >
<TextView
android:layout_width="80dip"
android:layout_height="wrap_content"
android:gravity="left"
android:text="STATUS :"
android:textColor="#666666"
android:textStyle="bold" />
<TextView
android:id="#+id/pdtvText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:text=""
android:textColor="#666666"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="7dip"
android:orientation="horizontal" >
<TextView
android:layout_width="80dip"
android:layout_height="wrap_content"
android:gravity="left"
android:text="注文日 :"
android:textColor="#666666"
android:textStyle="bold" />
<TextView
android:id="#+id/pdtvText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:text=""
android:textColor="#666666"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="7dip"
android:orientation="horizontal" >
<TextView
android:layout_width="80dip"
android:layout_height="wrap_content"
android:gravity="left"
android:text="納品日 :"
android:textColor="#666666"
android:textStyle="bold" />
<TextView
android:id="#+id/pdtvText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:text=""
android:textColor="#666666"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="7dip"
android:orientation="horizontal" >
<TextView
android:layout_width="80dip"
android:layout_height="wrap_content"
android:gravity="left"
android:text="依頼枚数 :"
android:textColor="#666666"
android:textStyle="bold" />
<TextView
android:id="#+id/pdtvText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:text=""
android:textColor="#666666"
android:textStyle="bold" />
<TextView
android:id="#+id/pdtvText41"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:text=""
android:textColor="#FF0000"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="7dip"
android:orientation="horizontal" >
<TextView
android:layout_width="80dip"
android:layout_height="wrap_content"
android:gravity="left"
android:text="納品枚数 :"
android:textColor="#666666"
android:textStyle="bold" />
<TextView
android:id="#+id/pdtvText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:text=""
android:textColor="#666666"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/errorLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/subtitle_error"
android:gravity="right|center_vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/bluebar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/subtitle_deli"
android:gravity="right|center_vertical" >
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:gravity="left"
android:paddingBottom="5dip"
android:paddingTop="5dip"
android:text=" "
android:textColor="#666666"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageviewproduct"
android:layout_width="300dip"
android:layout_height="180dip"
android:layout_marginBottom="10dip"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="11.89" >
<ListView
android:id="#+id/productlistall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000" >
</ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</merge>
Never add ListView,MapView,WebView(its already scrollable) in ScrollView
Remove from ScrollView.
Do not put a ListView inside a ScrollView.
ListView already handles
scrolling, you're only going to run into trouble.
Using a ListView to make it not scroll is extremely expensive and goes against the whole purpose of ListView. You should NOT do this. Just use a LinearLayout instead.
As samir and coader said, never use them in scrollview; but let this time be an exception, first check whether you've entered enough no. of items inside the list such tat there is enough no. of objects to scroll

how can i display text below listview in android?

I have an problem to displaying text below listview.when items are more in list the below text is not appear in my emulator. how can i solve this. i have use the below code.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg1"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TextView
android:id="#+id/t7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text=""
android:textColor="#000033"
android:typeface="serif" />
<TableRow
android:id="#+id/tableRow5"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/fromstation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#DC143C"
android:layout_gravity="left"
android:typeface="serif"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/to"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To"
android:textColor="#000000"
android:layout_gravity="center"
android:typeface="serif"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tostation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#DC143C"
android:layout_gravity="right"
android:typeface="serif"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tname"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:padding="3dip"
android:text="Train"
android:layout_gravity="left"
android:textColor="#9400D3"
android:typeface="serif"
>
</TextView>
<TextView
android:id="#+id/arr"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:text="Arrival"
android:layout_gravity="center"
android:textColor="#9400D3"
android:typeface="serif" >
</TextView>
<TextView
android:id="#+id/dep"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:text=" Departure"
android:textColor="#9400D3"
android:layout_gravity="right"
android:typeface="serif" >
</TextView>
</TableRow>
<ListView
android:id="#id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
>
</ListView>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/enq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*Marked services do not run on sunday."
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#800000"
android:typeface="serif" />
</LinearLayout>
</TableLayout>
can anyone tell me where i have done wrong.
thanks in advance.
Try this, android:layout_weight="1" and change layout_height="match_parent" to android:layout_height="match_parent" inside lsitview and the below layoutview
<ListView
android:id="#id/android:list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:layout_weight="1">
</ListView>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/enq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*Marked services do not run on sunday."
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#800000"
android:typeface="serif" />
</LinearLayout>
P.S. For make a space of listview lager than the below layout use Proportion of android:layout_weight to 1:3(Listview 1: the belowlayout 3) or any Propotion you want
Jusy give the listview height like
android:layout_height="100dp"

How to use ScrollView in Android?

I have an XML layout file, but the text is more than fits into the screen size. What do I need to do in order to make a ScrollView?
<?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"
android:stretchColumns="1" >
<TableRow>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginTop="10dip"
android:layout_marginRight="5dip"
android:tint="#55ff0000"
android:src="#drawable/icon" />
</TableRow>
<TableRow>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" Name " />
<TextView android:id="#+id/name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Veer" />
</TableRow>
<TableRow>
<TextView android:id="#+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" Age" />
<TextView android:id="#+id/age1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="23" />
</TableRow>
<TableRow>
<TextView android:id="#+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" Gender" />
<TextView android:id="#+id/gender1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Male" />
</TableRow>
<TableRow>
<TextView android:id="#+id/profession"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" Professsion" />
<TextView android:id="#+id/profession1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Mobile Developer" />
</TableRow>
<TableRow>
<TextView android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" Phone" />
<TextView android:id="#+id/phone1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="03333736767" />
</TableRow>
<TableRow>
<TextView android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" Email" />
<TextView android:id="#+id/email1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="veer.developer#gmail.com" />
</TableRow>
<TableRow>
<TextView android:id="#+id/hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" Hobby" />
<TextView android:id="#+id/hobby1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Play Games" />
</TableRow>
<TableRow>
<TextView android:id="#+id/ilike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" I like" />
<TextView android:id="#+id/ilike1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Java, Objective-c" />
</TableRow>
<TableRow>
<TextView android:id="#+id/idislike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" I dislike" />
<TextView android:id="#+id/idislike1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Microsoft" />
</TableRow>
<TableRow>
<TextView android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" Address" />
<TextView android:id="#+id/address1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Johar Mor" />
</TableRow>
</TableLayout>
Just make the top-level layout a ScrollView:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<!-- everything you already have -->
</TableLayout>
</ScrollView>
How to use ScrollView
Using ScrollView is not very difficult. You can just add one to your layout and put whatever you want to scroll inside. ScrollView only takes one child so if you want to put a few things inside then you should make the first thing be something like a LinearLayout.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- things to scroll -->
</LinearLayout>
</ScrollView>
If you want to scroll things horizontally, then use a HorizontalScrollView.
Making the content fill the screen
As is talked about in this post, sometimes you want the ScrollView content to fill the screen. For example, if you had some buttons at the end of a readme. You want the buttons to always be at the end of the text and at bottom of the screen, even if the text doesn't scroll.
If the content scrolls, everything is fine. However, if the content is smaller than the size of the screen, the buttons are not at the bottom.
This can be solved with a combination of using fillViewPort on the ScrollView and using a layout weight on the content. Using fillViewPort makes the ScrollView fill the parent area. Setting the layout_weight on one of the views in the LinearLayout makes that view expand to fill any extra space.
Here is the XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"> <--- fillViewport
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textview"
android:layout_height="0dp" <---
android:layout_weight="1" <--- set layout_weight
android:layout_width="match_parent"
android:padding="6dp"
android:text="hello"/>
<LinearLayout
android:layout_height="wrap_content" <--- wrap_content
android:layout_width="match_parent"
android:background="#android:drawable/bottom_bar"
android:gravity="center_vertical">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Accept" />
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Refuse" />
</LinearLayout>
</LinearLayout>
</ScrollView>
The idea for this answer came from a previous answer that is now deleted (link for 10K users). The content of this answer is an update and adaptation of this post.
There are two options. You can make your entire layout to be scrollable or only the TextView to be scrollable.
For the first case,
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1" >
<TableRow>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="10dip"
android:src="#drawable/icon"
android:tint="#55ff0000" >
</ImageView>
</TableRow>
<TableRow>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" Name " >
</TextView>
<TextView
android:id="#+id/name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Veer" >
</TextView>
</TableRow>
<TableRow>
<TextView
android:id="#+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" Age" >
</TextView>
<TextView
android:id="#+id/age1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="23" >
</TextView>
</TableRow>
<TableRow>
<TextView
android:id="#+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" Gender" >
</TextView>
<TextView
android:id="#+id/gender1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Male" >
</TextView>
</TableRow>
<TableRow>
<TextView
android:id="#+id/profession"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" Professsion" >
</TextView>
<TextView
android:id="#+id/profession1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Mobile Developer" >
</TextView>
</TableRow>
<TableRow>
<TextView
android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" Phone" >
</TextView>
<TextView
android:id="#+id/phone1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="03333736767" >
</TextView>
</TableRow>
<TableRow>
<TextView
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" Email" >
</TextView>
<TextView
android:id="#+id/email1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="veer.developer#gmail.com" >
</TextView>
</TableRow>
<TableRow>
<TextView
android:id="#+id/hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" Hobby" >
</TextView>
<TextView
android:id="#+id/hobby1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Play Games" >
</TextView>
</TableRow>
<TableRow>
<TextView
android:id="#+id/ilike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" I like" >
</TextView>
<TextView
android:id="#+id/ilike1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Java, Objective-c" >
</TextView>
</TableRow>
<TableRow>
<TextView
android:id="#+id/idislike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" I dislike" >
</TextView>
<TextView
android:id="#+id/idislike1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Microsoft" >
</TextView>
</TableRow>
<TableRow>
<TextView
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text=" Address" >
</TextView>
<TextView
android:id="#+id/address1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Johar Mor" >
</TextView>
</TableRow>
<Relativelayout>
</Relativelayout>
</TableLayout>
</RelativeLayout>
</ScrollView>
or, as I said you can use scrollView for TextView alone.
A ScrollView is a special type of FrameLayout in that it allows users to scroll through a list of views that occupy more space than the physical display.I just add some attributes .
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars = "vertical"
android:scrollbarStyle="insideInset"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<!-- Add here which you want -->
</TableLayout>
</ScrollView>
As said above you can put it inside a ScrollView... and if you want the Scroll View to be horizontal put it inside HorizontalScrollView... and if you want your component (or layout) to support both put inside both of them like this:
<HorizontalScrollView>
<ScrollView>
<!-- SOME THING -->
</ScrollView>
</HorizontalScrollView>
and with setting the layout_width and layout_height ofcourse.
Put your TableLayout inside a ScrollView Layout.That will solve your problem.
To scroll data in text view you can use this to your text view.
and add
and for anything other layout you can just add scroll view on layout as people are saying above.
/**
android:scrollable=true at textview in xml layout.
TextView txtScroll = (TextView) findViewById(R.id.txt1);
txtScroll.setMovementMethod(new ScrollingMovementMethod());
*//
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<RadioButton
android:id="#+id/butonSecim1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight=".50"
android:text="#string/buton1Text" />
<RadioButton
android:id="#+id/butonSecim2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight=".50"
android:text="#string/buton2Text" />
</RadioGroup>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/bilgiAlani"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible" >
<TableRow
android:id="#+id/BilgiAlanitableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/bilgiMesaji"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".100"
android:ems="10"
android:gravity="left|top"
android:inputType="textMultiLine" />
</TableRow>
</TableLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/metin4"
android:layout_height="match_parent"
android:layout_weight=".100"
android:text="deneme" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/metin5"
android:layout_height="match_parent"
android:layout_weight=".100"
android:text="deneme" />
</TableRow>
</TableLayout>
</ScrollView>

Android Emulator doesn't Scroll down

I am creating a layout as follows and when I emulate it in the AVD. It doesn't Scroll down to see the conten below the fold.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView android:text="#string/UserFormWelcome"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textSize="20px" android:gravity="center" />
<TextView android:text="#string/name" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textStyle="bold"
android:paddingTop="20px" android:paddingLeft="10px" />
<TableLayout android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TableRow android:layout_height="wrap_content"
android:layout_width="match_parent" android:paddingTop="20px">
<TextView android:text="#string/firstname"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:width="100px" android:paddingLeft="10px" />
<EditText android:id="#+id/LastName" android:width="200px"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</TableRow>
<TableRow android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView android:text="#string/lastname"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:paddingLeft="10px" />
<EditText android:id="#+id/LastName" android:width="200px"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
<TextView android:text="#string/dob" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textStyle="bold"
android:paddingTop="20px" android:paddingLeft="10px" />
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:stretchColumns="3"
android:paddingTop="20px" android:paddingLeft="10px">
<TableRow>
<TextView android:text="#string/date" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_column="0" />
<TextView android:text="#string/month" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_column="1" />
<TextView android:text="#string/year" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_column="2" />
</TableRow>
<TableRow>
<Spinner android:id="#+id/spinnerDate" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_column="0" />
<Spinner android:id="#+id/spinnerMonth" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_column="1" />
<Spinner android:id="#+id/spinnerYear" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_column="2" />
</TableRow>
</TableLayout>
<LinearLayout android:id="#+id/linearLayout1"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:paddingLeft="10px">
<TextView android:text="#string/sex" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textStyle="bold"
android:paddingTop="20px" />
<RadioGroup android:id="#+id/radioGroup1"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:text="Male" android:id="#+id/rdbMale"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingRight="20px" android:checked="true" />
<RadioButton android:text="Female" android:id="#+id/rdbFemale"
android:layout_height="wrap_content" android:layout_width="wrap_content" />
</RadioGroup>
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:paddingLeft="10px">
<TextView android:text="#string/city" android:id="#+id/textView3"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textStyle="bold" android:paddingTop="20px"
android:paddingBottom="10px" />
<Spinner android:id="#+id/citySpiner" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>
</LinearLayout>
You should wrap your layout / the part you want to scoll into a ScrollView.
e.g. you can rewrite your layout as:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_height="wrap_content" android:layout_width="wrap_content">
<TextView [...]
[...]
</LinearLayout>
</ScrollView>
so your root tag will be a ScrollView, and you just paste your current layout inside.
you just need to remove the namespace declaration from your LinearLayout, and declare it in the ScrollView.
The ScrollView API Docs might be helpful, and of course, Romain Guy's "ScrollView's Handy tricks".

Categories

Resources