I have to design UI to contain two columns.
First column should wrap contained label and second in the same row should fill whole space. Below some example:
http://temp.chrzanowski.info/so_layout.png
How can I do this ?
Use TableLayout with strechColumns = "1"
It stretches the 2nd column occupying the remaining space.
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
</TableLayout>
Following TableLayout would give you the desired output:
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bla bla bla"
android:singleLine="true" /> <!-- first column's view -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> <!-- second column's view -->
</TableRow>
...
</TableLayout>
As an alternative, you can do a LinearLayout like so:
<?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="100"
android:gravity="center_vertical">
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="40">
<TextView
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="A very very very long text"
android:singleLine="true"
android:scrollHorizontally="true"
android:ellipsize="end">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="60">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="TextBox">
</EditText>
</LinearLayout>
</LinearLayout>
To adjust width of columns, modify the layout_weight of each LinearLayout containers. Lesser value means smaller width, and both weights must equal 100.
Try this:
<?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="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label 1"
android:layout_marginLeft="7dp"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label Loooong2"
android:layout_marginLeft="7dp"
android:layout_marginTop="30dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label 3"
android:layout_marginLeft="7dp"
android:layout_marginTop="30dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="TextBox"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="TextBox"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="TextBox"/>
</LinearLayout>
</LinearLayout>
Related
I've got a problem with TableLayout. I'm trying to place my objects in columns but every next row affects previous ones and destroys them. Here's some code
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.dom.zadanie2.aktywnosc2"
android:orientation="vertical">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView2"
android:text="Imie"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/editText2"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView3"
android:text="Płeć"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kobieta"
android:id="#+id/radioButton"
android:layout_column="1"
android:paddingRight="40dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "Mężczyzna"
android:id="#+id/radioButton2"/>
</RadioGroup>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ratingBar"/>
</TableRow>
</TableLayout>
When I have placed the RatingBar, previous rows moved out of screen. Please give me some tips, I've tried various setting but nothing works properly. I don't know what do to so rows would appear independent. Thanks in advance.
Instead of using TableLayout and TableRow , Simply use LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- your Views -->
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- your Views -->
</LinearLayout>
<!-- goes on like this -->
</LinearLayout>
Suggest you to change to Linearlayout,
As in below 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="vertical"
android:weightSum="10">
<LinearLayout
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:gravity="center"
android:text="Imie"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
</LinearLayout>
<LinearLayout
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:gravity="center"
android:text="Plec"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kobieta" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mezczyzna" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<RatingBar
android:id="#+id/ratingBar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:isIndicator="true"
android:numStars="5"
android:stepSize="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/tableRow5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="7"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
Add
android:layout_weight="3"
to TableLayout, and in each TableRow, change
android:layout_height="wrap_content"
to
android:layout_height="0dp"
android:weightSum="1"
like the following
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:weightSum="6"
>
<TextView
android:id="#+id/textView2"
android:text="Imie"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/editText2"
android:layout_weight="5"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:weightSum="5"
>
<TextView
android:id="#+id/textView3"
android:text="Płeć"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="2"
android:width="0dp"
/>
<RadioGroup
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kobieta"
android:id="#+id/radioButton"
android:layout_column="1"
android:paddingRight="40dp"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "Mężczyzna"
android:id="#+id/radioButton2"
/>
</RadioGroup>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:weightSum="1"
>
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="#+id/ratingBar"
/>
</RelativeLayout>
</TableRow>
TableRow has by default layout_width as match_parent and layout_hieght as wrap_content, so do not specify layout_width and layout_height in TableRow tag.
Now, The main answer of your question:- In Table layout, width of each cell(View) in a column is related to other cells(views) in the same column. The width of each cell will be equal to the highest width of the cell in the same column.
In your case the rating bar is in column zero and width of the rating bar is more then other cells of same column. To overcome from this problem use layout_span="total number of columns in table layout" attribute in ranting bar view. By using this attribute your rating bar will span in all the columns.
solution of your problem is this:-
<TableRow
android:id="#+id/tableRow3">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout_span="2"
android:id="#+id/ratingBar"/>
</TableRow>
I have a layout with a view text views, a table and then some buttons at the bottom.
I wanted the buttons to always be at the bottom regardless of the height of the table so I changed the layout to relative so I could do this.
However this mashed all the textviews and the table into one line. So I put these in a LinearLayout within the RelativeLayout which I thought would fix the problem. Problem now is that only the first TextView and the buttons at the bottom show up. Everything else is missing.
Here is the xml code I am using to define this layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="4dip"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/view_meal_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Spaghetti Bolognaise"
android:textSize="25sp" />
<TextView
android:id="#+id/view_meal_type"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dinner"
android:textSize="16sp" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/view_day_calories"
android:text="#string/calories_label"
android:textSize="18sp" />
<TextView
android:id="#+id/view_day_calories"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/calories_today_value"
android:textSize="18sp" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_marginBottom="10dip"
android:background="#color/hr" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:stretchColumns="0,1,2" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/meal_table_ingred_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ingredient"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/meal_table_quantity_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/quantity"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/meal_table_calories_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calories"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="1.0" >
<Button
android:id="#+id/edit_meal_button"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#string/edit_meal_label" />
<Button
android:id="#+id/favourite_meal_button"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#string/favourite_meal_label" />
</LinearLayout>
</RelativeLayout>
Thanks for your help!
Change
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
to
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical">
<?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">
<RelativeLayout
android:orientation="horizontal"
android:id="#+id/headerLayout"
style="#style/TitleBar">
<TextView android:textAppearance="?android:textAppearanceMedium"
android:textColor="#ffffff"
android:id="#+id/createpatientheader"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Patient Registration"
android:maxLines="1"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"/>
</RelativeLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#81BEF7"
android:scrollbarAlwaysDrawVerticalTrack="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scrollbarFadeDuration="0"
android:scrollbarAlwaysDrawHorizontalTrack="true"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/patient_fields"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#81BEF7"
android:gravity="center"
android:stretchColumns="*">
<TableRow>
<TextView
style="#style/TextViewStandard"
android:gravity="left"
android:text="Name *"
android:textColor="#ffffff"
android:layout_weight="1"/>
<EditText
style="#style/EditTextStandard"
android:id="#+id/patientname"
android:hint="At least 3 Alphabets"
android:layout_span="2"
android:singleLine="true"
android:maxLength="30"
android:inputType="textPersonName"
android:layout_weight="1"
/>
</TableRow>
<TableRow >
<TextView
style="#style/TextViewStandard"
android:gravity="left"
android:text="Date of Birth"
android:textColor="#ffffff"
android:layout_weight="1" />
<EditText
style="#style/EditTextStandard"
android:id="#+id/dob"
android:layout_span="2"
android:singleLine="true"
android:layout_weight="1"
/>
<DatePicker android:id="#+id/dateofbirth"
android:visibility="gone"/>"
</TableRow>
<TableRow>
<TextView
style="#style/TextViewStandard"
android:gravity="left"
android:padding="3dip"
android:text="Mobile"
android:textColor="#ffffff"
android:layout_weight="1"/>
<EditText
style="#style/EditTextStandard"
android:id="#+id/mobile"
android:layout_span="2"
android:singleLine="true"
android:numeric="integer"
android:maxLength="10"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
</LinearLayout>
I want both scroll bar in my application.I have tried with this code. If I place all controls inside the scroll view, then all the controls are streched and it filled the screen. But if i put it inside the horizontal scroll, it is not streched and not filled in the screen. I am missing something in that.. help me..
I am trying to align in the center 2 buttons in a table row, no matter the screen resolution, so i am trying to avoid using specified margins.
This is my code, it works only with declared margins, and i am using gravity and layout gravity...nothing worked for me. They did not align in the center.
Does anyone have an idea? Thanks anyway.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableLayout
android:id="#+id/TableLayout01"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100px"
android:stretchColumns="1" >
<TableRow
android:id="#+id/TableRow01"
android:layout_gravity="center"
android:paddingLeft="50dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/image1Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:drawablePadding="-5sp"
android:drawableTop="#drawable/image1"
android:gravity="center"
android:src="#drawable/image1"
android:text="#string/image1text"
android:textColor="#color/darkgrey" />
<Button
android:id="#+id/image2Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:drawablePadding="-5sp"
android:drawableTop="#drawable/image2"
android:gravity="center"
android:src="#drawable/image2"
android:text="#string/image2text"
android:textColor="#color/darkgrey" />
</TableRow>
<TableRow
android:id="#+id/TableRow02"
android:paddingLeft="50dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/image3Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:drawablePadding="-5sp"
android:drawableTop="#drawable/image3"
android:gravity="center"
android:src="#drawable/image3"
android:text="#string/image3text"
android:textColor="#color/darkgrey" />
<Button
android:id="#+id/image4Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:drawablePadding="-5sp"
android:drawableTop="#drawable/image4"
android:gravity="center"
android:src="#drawable/image4"
android:text="#string/image4text"
android:textColor="#color/darkgrey" />
</TableRow>
</TableLayout>
</RelativeLayout>
</ScrollView>
Put your buttons in a couple of LinearLayouts instead of a TableRow.
<TableLayout
...
>
<LinearLayout
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
>
<Button
android:id="#+id/image1Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:drawablePadding="-5sp"
android:drawableTop="#drawable/image1"
android:src="#drawable/image1"
android:text="#string/image1text"
android:textColor="#color/darkgrey" />
<Button
android:id="#+id/image2Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:drawablePadding="-5sp"
android:drawableTop="#drawable/image2"
android:src="#drawable/image2"
android:text="#string/image2text"
android:textColor="#color/darkgrey" />
</LinearLayout>
</LinearLayout>
...
</TableLayout>
The outer LinearLayout will be stretched to the width of the TableLayout and have its height set to wrap_content by TableLayout. The inner LinearLayout will be centered horizontally in the outer LinearLayout because the latter is vertically oriented.
<LinearLayout android:id="#+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:background="#drawable/wood">
<ScrollView android:id="#+id/ScrollView01" android:layout_width="fill_parent" android:layout_height="fill_parent">
<AbsoluteLayout android:id="#+id/absoluteLayout1" android:layout_width="match_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:paddingLeft="10dp" android:paddingRight="10dp">
<TableLayout android:id="#+id/tableLayout0" android:background="#drawable/my_border" android:layout_width="fill_parent" android:layout_y="150dp" android:layout_height="match_parent">
<TableRow android:layout_height="wrap_content" android:id="#+id/tableRow1" android:layout_width="wrap_content">
<TextView android:id="#+id/EmailID" android:text="EmailID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#drawable/my_border2" android:textStyle="bold" android:layout_weight="1"></TextView>
<EditText android:id="#+id/txtEmailID" android:layout_width="150dp" android:background="#drawable/my_border1" android:layout_height="wrap_content" android:layout_weight="1">
<requestFocus></requestFocus>
</EditText>
</TableRow>
<TableRow android:layout_height="wrap_content" android:id="#+id/tableRow6" android:layout_width="wrap_content" android:paddingTop="20dp">
<Button android:id="#+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Cancel"></Button>
<Button android:id="#+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Login"></Button>
</TableRow>
</TableLayout>
</AbsoluteLayout>
</ScrollView>
</LinearLayout>
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>