Percentage Layout Android - android

Hello I am new in Android, and my English is bad. I try it.
I want to divide my layout in 3 diferent sizes.
<LinearLayout 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=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TextView
android:id="#+id/txt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Esto es una mierda de Relative Layout" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2">
<Button
android:id="#+id/btn1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"
android:text="Button" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:id="#+id/btn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:text="Button" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
My code should display three columns, the center column being twice the size of the side columns. If I set all the layouts with the same layout weight, they all display correctly, but with the same size.
However, when I set the center column with a different size value (in the layout weight) it dissapears from the screen.
What can I do to display my layouts in three columns with different sizes?
If anybody can help me. Thanks for reading.

use this structure:
<!-- parent layout-->
<LinearLayout
android:weightSum="1">
<!-- children layouts -->
<RelativeLayout
android:weight="0.25"
></RelativeLayout>
<RelativeLayout
android:weight="0.5"
></RelativeLayout>
<RelativeLayout
android:weight="0.25"
></RelativeLayout>
</LinearLayout>

<LinearLayout
android:id="#+id/stupid_android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<View
android:layout_width="0dip"
android:layout_height="match_parent"
android:background="#FF0000"
android:layout_weight="1" />
<View
android:layout_width="0dip"
android:layout_height="match_parent"
android:background="#0000FF"
android:layout_weight="2" />
<View
android:layout_width="0dip"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>

This is your modified code:
<LinearLayout 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=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Esto es una mierda de Relative Layout" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" >
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:text="Button" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:text="Button" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Use match_parent instead fill_parent. Althought you write program for API 8-.

Do it like this :
It's important to set the corresponding width/height to 0dp.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="4" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="column1" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="column2" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="column3" />
</LinearLayout>

Related

change alignment and position of android layout

I have main layout activity_user_profile which include these three layout: First_layout,
Second_layout,
list_item.
activity_user_profile.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include layout="#layout/first_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<include layout="#layout/second_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<include layout="#layout/list_item"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</ScrollView>
list_item.xml
<?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="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/listView" />
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textStyle="bold"/>
<TextView
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
As seen in the image, there is a white gap between bottom and list_item.xml.
How can shift this layout to right and extend it to bottom of the page?
Update
first_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="UserProfile"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView3"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="7.03"
android:textSize="15sp"/>
</LinearLayout>
second_layout.xml
<LinearLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".Main3Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:id="#+id/textView"
android:visibility="gone" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editTextName"
android:visibility="gone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:id="#+id/textView2"
android:visibility="gone" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editTextAddress"
android:visibility="gone" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Post"
android:onClick="insert"
android:id="#+id/button"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewResult" />
</LinearLayout>
Have searched this for a long time and concluded this :
Remove,
android:orientation="vertical"
android:layout_weight="1"
Check that :
Scroll view must have a single child (i.e all views must be included
within a single root layout inside scroll view)
ScrollView works when your page size is greater the default height
of the screen.
try this
Replace your activity_user_profile.xml by this
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include layout="#layout/first_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_weight="1"/>
<include layout="#layout/second_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_weight="1"
/>
<include layout="#layout/list_item"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_weight="1"/>
</LinearLayout>
</ScrollView>
I have made following changes in your layout.....
See code below
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="3"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<include layout="#layout/first_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<include layout="#layout/second_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<include layout="#layout/list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
The child of the scroll view should have it's height set to "wrap_content" to allow scrolling.
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
Change this to:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

Dividing screen into two equal parts , can not set layout_weight

I guess question is silly, but I am missing something in this and want to understand.
I am dividing the screen into two equal parts. I know this can be done by setting weight. But I do not know why I can not set weight in Linear layout. Instead I am setting weightSum, but what I understood about weightSum is, it is space on screen left after setting width and height I guess.
here is what I am doing
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:id="#+id/main_panel">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/top"
android:weightSum="100">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/list"
android:divider="#null"
android:dividerHeight="0dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/top"
android:weightSum="50"
android:layout_alignParentBottom="true"
android:id="#+id/ bottom">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/data"
android:orientation="vertical"
android:background="#D3D3D3"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<View
android:id="#+id/drag_line"
android:layout_width="50dp"
android:visibility="visible"
android:background="#drawable/bar"
android:layout_gravity="center_horizontal|top"
android:layout_height="170dp"
android:src="#drawable/line" />
</FrameLayout>
</LinearLayout>
</RelativeLayout>
PS: I cannot set layout_weight property using android lollipop
Try with below code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:id="#+id/main_panel">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/top"
>
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/list"
android:divider="#null"
android:dividerHeight="0dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_below="#id/top"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:id="#+id/ bottom">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/data"
android:orientation="vertical"
android:background="#D3D3D3"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<View
android:id="#+id/drag_line"
android:layout_width="50dp"
android:visibility="visible"
android:background="#drawable/bar"
android:layout_gravity="center_horizontal|top"
android:layout_height="170dp"
android:src="#drawable/line" />
</FrameLayout>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/color_in_palette"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCFFFF"
android:text="Button"
android:layout_weight="50"
android:textSize="0sp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50">
<TextView
android:id="#+id/color_id"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="(204,255,255)"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:numColumns="13"
android:layout_weight="48" >
</GridView>
</LinearLayout>
</LinearLayout>
In this button will occupy the half of the screen , and the other half is occupied by another Linear Layout , which contains a TextView & GridView
Problem:
You cant add a weightSum within a Relative layout that it is only bounded to linearlayout layout params itself.
solution:
add another linearlayout inside your relatie layout as a parent to both of the inner linear layout.
sample:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:id="#+id/main_panel">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/parent"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/top"
android:weightSum="0.5">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/list"
android:divider="#null"
android:dividerHeight="0dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/top"
android:weightSum="0.5"
android:layout_alignParentBottom="true"
android:id="#+id/ bottom">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/data"
android:orientation="vertical"
android:background="#D3D3D3"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<View
android:id="#+id/drag_line"
android:layout_width="50dp"
android:visibility="visible"
android:background="#drawable/bar"
android:layout_gravity="center_horizontal|top"
android:layout_height="170dp"
android:src="#drawable/line" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Weightsum is property used by parent to find the total weight sum by its children.This property is only applicable to LinearLayout. Its childrens should specify its weight with width or height as 0dp. You can find the tutorial here. So your layout.xml will be
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_panel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:weightSum="100" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="50"
android:divider="#null"
android:dividerHeight="0dp" />
<FrameLayout
android:id="#+id/data"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:background="#D3D3D3"
android:orientation="vertical" >
<View
android:id="#+id/drag_line"
android:layout_width="50dp"
android:layout_height="170dp"
android:layout_gravity="center_horizontal|top"
android:visibility="visible" />
</FrameLayout>
</LinearLayout>
Get Root attribute always be <Relative> and with in that write your remaining XML code.
e.g.
<?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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/black">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/white">
</LinearLayout>
</LinearLayout>
//Replace above XML code by...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/black">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/white">
</LinearLayout>
</LinearLayout>
</RelativeLayout>

SeekBar not showing up on view xml file

I am trying to get the SeekBar to display at the bottom of the screen but actually under the LinerLayout I created, but I can't seem to get it to display. I have split the screen into 5 sections and want the seek bar at the bottom of the screen, where am I going wrong?
<LinearLayout 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:background="#color/gray"
android:orientation="horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:layout_weight="1.0"
android:background="#color/white" />
<FrameLayout
android:id="#+id/green"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:layout_weight="1.0"
android:background="#color/green" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/blue"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:layout_weight="1.0"
android:background="#color/blue" />
<FrameLayout
android:id="#+id/red"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:layout_weight="1.0"
android:background="#color/red" />
<FrameLayout
android:id="#+id/yellow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:layout_weight="1.0"
android:background="#color/yellow" />
</LinearLayout>
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
Try to change your SeekBar layout_width to "match_parent":
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Hope this helps.
The encompassing linearlayout has orientation="horizontal", try setting it to "vertical".

Android Relative Layout with sticky footer

I'm relatively new to Android development and I'm having a hard time putting together a certain interface. I've looked through many similar questions but none of them give me the answer i'm looking for.
I want to put together an XML interface like the following:
I want a LinearLayout that acts like a sticky footer that will contain two Buttons. This part will always align to the bottom and and will always be 60dp high. The RelativeLayout would then be on top of the footer, but it's height should scale depending on the screen size.
I've tried the following:
<LinearLayout 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:orientation="vertical"
tools:context=".DartboardActivity">
<RelativeLayout
android:id="#+id/game_layout"
android:layout_width="match_parent"
android:layout_height="506dp"
android:background="#drawable/background"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<ImageView
android:id="#+id/Dartboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/desc_dartboard"
android:src="#drawable/dartboard" />
<hhs.week3.dartboard.VerticalSeekBar
android:id="#+id/seekBarVertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:thumb="#drawable/thumbhorizontal"
android:layout_marginBottom="40dp" />
<SeekBar
android:id="#+id/seekBarHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:thumb="#drawable/thumbhorizontal" />
</RelativeLayout>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#333">
<Button
android:id="#+id/fire"
style="#style/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="#string/fire" />
<Button
android:id="#+id/settings"
style="#style/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="#string/settings" />
</LinearLayout>
</LinearLayout>
I got it working half decently, but it requires me to give the RelativeLayout bit a fixed height, making it look right on my phone, but not on others.
How do I best approach this?
Use the LinearLayout layout_weight mechanism to assign all remaining space to your RelativeLayout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
... />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
... />
</LinearLayout>
Try following code
<RelativeLayout 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:orientation="vertical"
tools:context=".DartboardActivity" >
<RelativeLayout
android:id="#+id/game_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/buttons"
android:layout_alignParentTop="true"
android:background="#drawable/ic_launcher"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
</RelativeLayout>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:background="#333"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
I removed LinearLayout and replaced with RelativeLayout. This will allow you to set your LinearLayout stick to bottom.
I was able to achieve the same result by using below code. Basically, the parent layout is one Linear layout and the first child element is a scrollview so it can squeeze its content when the keyboard is shown. Also note the use of the android:layout_height="0dp" and android:layout_weight="1" used with the scrollview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wrap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/wrap2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
</ScrollView>
<FrameLayout
android:id="#+id/buttonsPanel"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#333" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="action buttons" />
</FrameLayout>
</LinearLayout>
Try this
<LinearLayout 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:orientation="vertical"
tools:context=".DartboardActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1"
android:background="#drawable/background"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<ImageView
android:id="#+id/Dartboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/desc_dartboard"
android:src="#drawable/dartboard"
android:adjustViewBounds="true"/>
<hhs.week3.dartboard.VerticalSeekBar
android:layout_marginTop="10dp"
android:id="#+id/seekBarVertical"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:thumb="#drawable/thumbhorizontal"/>
<SeekBar
android:id="#+id/seekBarHorizontal"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:thumb="#drawable/thumbhorizontal" />
</LinearLayout>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#333333">
<Button
android:id="#+id/fire"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="#string/fire"/>
<Button
android:id="#+id/settings"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="#string/settings"/>
</LinearLayout>
</LinearLayout>

Android ScrollView fillViewport not working

I have a simple layout with a name on the top, and a button which I want to be at the bottom of the screen, or beyond that in case I add more items.
So I am using a ScrollView with a LinearLayout as follows:
<ScrollView 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:background="#android:color/white"
android:fillViewport="true"
android:focusableInTouchMode="true"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
tools:context=".ItemDetailActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- Name -->
<RelativeLayout
android:id="#+id/top_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" >
<TextView
android:id="#+id/name_label"
style="#style/Header_Label_TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/name" />
<TextView
android:id="#+id/name_value"
style="#style/Value_Label_TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/name_label" />
</RelativeLayout>
<!-- button -->
<RelativeLayout
android:id="#+id/ButtonLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" >
<Button
android:id="#+id/add_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:onClick="editItem"
android:text="#string/button_edit" />
</RelativeLayout>
<requestFocus />
</LinearLayout>
This is what I get:
How do I make it so that the button appears at the bottom of the screen or beyond. Upon searching online, most answers were to set the `android:fillViewport="true", but that is not helping. What am I doing wrong?
Change your Button layout like this:
<RelativeLayout
android:id="#+id/ButtonLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom" >
<Button
android:id="#+id/add_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:onClick="editItem"
android:layout_alignParentBottom="true"
android:text="#string/button_edit" />
</RelativeLayout>
This should work -
Use Relative layout as a child for the scroll view .
Set layout_alignParentBottom = true for the Button.
Sample XML
<ScrollView 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:background="#android:color/white"
android:fillViewport="true"
android:focusableInTouchMode="true"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
tools:context=".ItemDetailActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Name -->
<RelativeLayout
android:id="#+id/top_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" >
<TextView
android:id="#+id/name_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Name" />
<TextView
android:id="#+id/name_value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/name_label" />
</RelativeLayout>
<!-- button -->
<RelativeLayout
android:id="#+id/ButtonLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="#+id/add_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:onClick="editItem"
android:text="Edit" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
In scroll view, use RelativeLayout instead of LinearLayout and set your bottom view property
android:layout_alignParentBottom="true"
If you're using LinearLayout (which is more performant than RelativeLayout), you should set android:gravity="bottom" & android:layout_height="match_parent" like this:
<LinearLayout
android:id="#+id/ButtonLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom" >
<Button
android:id="#+id/add_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:onClick="editItem"
android:text="#string/button_edit" />
</LinearLayout>
Reference: https://demonuts.com/android-fillviewport/

Categories

Resources