I have a linearLayout which is surrounded by a scrollView in order to make it for smaller screens scrollable. In the linear layout I have 3 childs which are also linearlayout.
This is working fine. But I also want to support larger screens. At the moment I get with a larger screen space left on the bottom of the screen. I want to have the buttons (navigation bar) be always at the bottom. for larger screens (height) I would like to add at the top of the screen an empty view which fills out the space that is more provided by the larger screen.
How can I achieve that? i tried a lot with weight attribute but didnt get it working.
Here the layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#8db9f6"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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="#8db9f6">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_marginTop="8dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:text="test"
android:id="#+id/test"
android:paddingLeft="10dp"
android:textStyle="bold"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<View
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"/>
<ImageButton
android:id="#+id/btnName"
android:scaleType="center"
android:layout_width="60dp"
android:layout_height="48dp"
android:src="#android:drawable/ic_menu_edit">
</ImageButton>
</LinearLayout>
<EditText
android:id="#+id/edittest"
android:editable="false"
android:hint="#string/test"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
<TextView
android:text="#string/optionalText"
android:id="#+id/test1"
android:paddingLeft="10dp"
android:textSize="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="#+id/space"
android:layout_width="fill_parent"
android:layout_marginTop="4dp"
android:layout_height="3sp"
android:background="#333142">
</TextView>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:text="TEST TESt"
android:id="#+id/test2"
android:paddingLeft="10dp"
android:layout_marginTop="5dp"
android:textStyle="bold"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<View
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/progressbar"/>
<ImageButton
android:id="#+id/btnPicture"
android:scaleType="center"
android:layout_width="60dp"
android:layout_height="48dp"
android:src="#android:drawable/ic_menu_edit">
</ImageButton>
</LinearLayout>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#ffffff"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="150dip">
<ImageView
android:id="#+id/test3"
android:layout_width="400dip"
android:layout_height="150dip"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="#drawable/test1"/>
<TextView
android:id="#+id/text"
android:layout_gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8dp"
android:text="test test"/>
</FrameLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:layout_marginTop="3dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/scaleMinus"
android:textSize="14dp"
android:layout_weight="1"
android:layout_gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="#string/zoomOut"/>
<Button
android:id="#+id/scalePlus"
android:textSize="14dp"
android:layout_weight="1"
android:layout_gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="#string/zoomIn"/>
</LinearLayout>
<TextView
android:id="#+id/balken"
android:layout_width="fill_parent"
android:layout_marginTop="4dp"
android:layout_height="3sp"
android:background="#333142">
</TextView>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:text="test test"
android:id="#+id/test3"
android:paddingLeft="10dp"
android:textStyle="bold"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<View
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"/>
<EditText
android:id="#+id/test4"
android:editable="false"
android:layout_width="100dp"
android:layout_height="wrap_content">
</EditText>
<TextView
android:text="test"
android:id="#+id/mtest4"
android:paddingLeft="10dp"
android:textStyle="bold"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<View
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"/>
<ImageButton
android:id="#+id/test5"
android:scaleType="center"
android:layout_width="60dp"
android:layout_height="48dp"
android:src="#android:drawable/ic_menu_edit">
</ImageButton>
</LinearLayout>
<TextView
android:id="#+id/balken"
android:layout_width="fill_parent"
android:layout_marginTop="4dp"
android:layout_height="3sp"
android:background="#333142">
</TextView>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:background="#000000"
android:paddingTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/save"
android:textSize="18dp"
android:layout_weight="1"
android:layout_gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="#string/save"/>
<Button
android:id="#+id/cancel"
android:textSize="18dp"
android:layout_weight="1"
android:layout_gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="#string/abort"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
Wrap the entire layout in a Relative Layout. Use a relative layout as your root element and set the Linear Layout containing your buttons to layout_alignParentBottom="true" and the ScrollView set to layout_alignParentTop="true".
The LinearLayout containing the buttons and the ScrollView should be direct children of the RelativeLayout.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout <-- containing the layout minus the buttons
android:id="#+id/SV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<The Rest Of The Layout>
</LinearLayout>
<LinearLayout
android:id="#+id/LLButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Buttons>
</LinearLayout>
</ScrollView>
If you want the buttons to be always at the bottom of the screen alignParentBottom. If you want the buttons to always be at the bottom of the scrollview alignBelow="#id/SV"
android:layout_height="wrap_content" in your inner LinearLayout is your trouble here, changing it to 'fill_parent' should help. In general, you may want to use hierarchyviewer to help troubleshoot layout issues like this.
Related
I have a complicated linear layout, with some buttons and edittext:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top|center_horizontal"
android:orientation="vertical"
android:weightSum="1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="400dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="0dp"
android:text="#string/stop"
android:textSize="18dp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/text"
android:hint="#string/hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/choose"
android:layout_alignParentLeft="true"
android:minWidth="150dp"
android:textSize="10sp"/>
<Button
android:id="#+id/text2"
android:hint="#string/favourites"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/choose"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/text"
android:textSize="10sp"
/>
<Button
android:id="#+id/text3"
android:hint="#string/clean"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/choose"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/text"
android:textSize="10sp"
/>
</LinearLayout>
<Button
android:id="#+id/button"
android:text="#string/request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text2"
android:onClick="lookUp"
android:textSize="12sp"
/>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#3d455b"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button">
<HorizontalScrollView
android:id="#+id/hscrll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_gravity="left"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/table_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="false"
android:stretchColumns="2">
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
Everything looks at it should on a large screen (tablet): I have the first line with the edittext and two buttons, on the line below I have the other button, and below it I have the scrollview (with the table).
When I switch to normal screen (smartphone), only the first line is appearing, the rest not being visible.
What could the solution be?
In Small Screen device there are problem with margin you set, try to
remove android:layout_marginBottom="400dp" from your ParentLayout and manage your Layout according them
At the second linear_layout (that have horizontal orientation) all of your views, have match parent width that cause non of them are visible. I think it's what you want:
`
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/text"
android:hint="#string/hint"
android:layout_width="0dp"
android:layout_weight = "1"
android:layout_height="wrap_content"
android:minWidth="150dp"
android:textSize="10sp"/>
<Button
android:id="#+id/text2"
android:hint="#string/favourites"
android:layout_width="0dp"
androidlayout_weight = "1"
android:layout_height="wrap_content"
android:textSize="10sp"
/>
<Button
android:id="#+id/text3"
android:hint="#string/clean"
android:layout_width="0dp"
android:layout_weight = "1"
android:layout_height="wrap_content"
android:textSize="10sp"
/>
</LinearLayout>
`
<RelativeLayout
android:id="#+id/OtherWeatherInfo_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/margin_space"
android:layout_marginLeft="#dimen/margin_space"
android:layout_marginRight="#dimen/margin_space" >
<RelativeLayout
android:id="#+id/OtherWeatherInfo"
android:layout_width="#dimen/weather_space"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:layout_alignParentLeft="true"
android:background="#drawable/rect_block" >
<LinearLayout
android:id="#+id/humidityLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/humidityTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/str_humidity"
android:textSize="#dimen/weather_size"
android:textStyle="bold" >
</TextView>
<TextView
android:id="#+id/humidityValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/str_no_data"
android:textColor="#color/color_white"
android:textStyle="bold" >
</TextView>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/OtherWeatherInfo2"
android:layout_width="#dimen/weather_space"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:layout_toRightOf="#+id/OtherWeatherInfo"
android:background="#drawable/rect_block" >
<LinearLayout
android:id="#+id/visiLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/visiTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/str_visi"
android:textSize="#dimen/weather_size"
android:textStyle="bold" />
<TextView
android:id="#+id/visiValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/str_no_data"
android:textColor="#color/color_white"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
The above code is written by me to achieve the following figure:.But what I am achieving is this: or this:.
I have absolutely no clue about what is the mistake I am doing here??I am just using a main relative layout
OtherWeatherInfo_main and within that I am embedding two relative layouts to represent the two weather attributes. #dimen/weather_space is 145 dp.I am unable to align the two relative layout within the main layout equally.Any help will be appreciated.
Wrap the two RelativeLayouts inside a LinearLayout with horizontal orientation and then assign both the RelativeLayouts equal weight.
In general, something like this:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weight="0.5" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weight="0.5" />
</LinearLayout>
Remove either this
android:layout_alignParentRight="true"
or this
android:layout_toRightOf="#+id/OtherWeatherInfo"
from your second layout.
Also you can wrap both your RelativeLayouts to the horizontal LinearLayout and use android:layout_weight attribute
Try this way,if you wan achieved your design using LinearLayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/txtCityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kolkata"/>
<ImageView
android:id="#+id/imgWeather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_marginTop="20dp"/>
<TextView
android:id="#+id/txtTemprature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="28 C"
android:layout_margin="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Humidity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="29"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginLeft="20dp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Humidity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="29"/>
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/txtDateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fri 28"
android:layout_margin="10dp"/>
</LinearLayout>
I've been trying to find a way to move my buttons to the bottom half in my xml file. Currently they are in the top half.
I am trying to follow this solution found here, but I am afraid that it is inapplicable.
Put buttons at bottom of screen with LinearLayout?
My buttons.xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TableLayout android:id="#+id/tableLayout1"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<Button
android:id="#+id/block_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_weight="0.09"
android:background="#ff5ac3ff"
android:drawableLeft="#drawable/ic_action"
android:gravity="center_horizontal|center_vertical"
android:text="#string/block_apps"
android:textSize="22sp" />
<Button
android:id="#+id/security_settings_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff5ac3ff"
style="?android:attr/borderlessButtonStyle"
android:layout_weight="0.09"
android:drawableLeft="#drawable/ic_settings"
android:gravity="center_horizontal|center_vertical"
android:text="#string/security_settings"
android:textSize="22sp"/>
<Button
android:id="#+id/blacklist_whitelist_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff5ac3ff"
style="?android:attr/borderlessButtonStyle"
android:drawableLeft="#drawable/ic_blacklist_red"
android:gravity="center_horizontal|center_vertical"
android:text="#string/blacklist_whitelist"
android:textSize="22sp"
android:layout_weight="0.09"/>
</LinearLayout>
You should try something like this. Basically, if you're using layout_weight, you need to set the height or width (depending or your orientation: vertical or horizontal) to "0dp". Otherwise, the view won't use the weight attribute. Try this xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/emptySpace"
android:layout_height="0dp"
android:layout_width="fill_parent"
android:layout_weight="3" />
<Button
android:id="#+id/block_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="right|center_vertical"
android:layout_weight="1"
android:background="#ff5ac3ff"
android:drawableLeft="#drawable/ic_action"
android:gravity="center_horizontal|center_vertical"
android:text="#string/block_apps"
android:textSize="22sp" />
<Button
android:id="#+id/security_settings_button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#ff5ac3ff"
style="?android:attr/borderlessButtonStyle"
android:layout_weight="1"
android:drawableLeft="#drawable/ic_settings"
android:gravity="center_horizontal|center_vertical"
android:text="#string/security_settings"
android:textSize="22sp"/>
<Button
android:id="#+id/blacklist_whitelist_button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#ff5ac3ff"
style="?android:attr/borderlessButtonStyle"
android:drawableLeft="#drawable/ic_blacklist_red"
android:gravity="center_horizontal|center_vertical"
android:text="#string/blacklist_whitelist"
android:textSize="22sp"
android:layout_weight="1"/>
</LinearLayout>
Btw, I changed your tablelayout to an empty textview, but you should be able to use any other type of view there. It's just there to fill up space.
Also, with layout_weights, be careful with layouts that extend beyond the screen size (like scrollview). That will throw off your weights. Layout weights are best suited for known layout dimensions.
I would suggest you to divide the whole layout into 6 equal LinearLayouts and then set the buttons in the last three layouts..
Try this code..
<LinearLayout android:orientation="horizontal" android:layout_height="fill_parent" android:layout_width="fill_parent">
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"></LinearLayout>
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"></LinearLayout>
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"></LinearLayout>
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
<Button
android:id="#+id/block_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_weight="0.09"
android:background="#ff5ac3ff"
android:drawableLeft="#drawable/ic_action"
android:gravity="center_horizontal|center_vertical"
android:text="#string/block_apps"
android:textSize="22sp" />
</LinearLayout>
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
<Button
android:id="#+id/security_settings_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff5ac3ff"
style="?android:attr/borderlessButtonStyle"
android:layout_weight="0.09"
android:drawableLeft="#drawable/ic_settings"
android:gravity="center_horizontal|center_vertical"
android:text="#string/security_settings"
android:textSize="22sp"/>
</LinearLayout>
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
<Button
android:id="#+id/blacklist_whitelist_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff5ac3ff"
style="?android:attr/borderlessButtonStyle"
android:drawableLeft="#drawable/ic_blacklist_red"
android:gravity="center_horizontal|center_vertical"
android:text="#string/blacklist_whitelist"
android:textSize="22sp"
android:layout_weight="0.09"/>
</LinearLayout>
</LinearLayout>
Please do tell me what the output is..
I've tested this, it should work, you'll have to change the button attributes since I was using test content & images.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TableLayout android:id="#+id/tableLayout1"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="bottom"
android:orientation="vertical">
<Button
android:id="#+id/block_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff5ac3ff"
android:drawableLeft="#drawable/ic_launcher"
android:gravity="center_horizontal|center_vertical"
android:text="#string/block_apps"
android:textSize="22sp" />
<Button
android:id="#+id/security_settings_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff5ac3ff"
style="?android:attr/borderlessButtonStyle"
android:drawableLeft="#drawable/ic_launcher"
android:gravity="center_horizontal|center_vertical"
android:text="security settings"
android:textSize="22sp"/>
<Button
android:id="#+id/blacklist_whitelist_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff5ac3ff"
style="?android:attr/borderlessButtonStyle"
android:drawableLeft="#drawable/ic_launcher"
android:gravity="center_horizontal|center_vertical"
android:text="black white list"
android:textSize="22sp"/>
</LinearLayout>
</LinearLayout>
I'm using a layout for a configuration panel comprising a LinearLayout (vertical) containing two sub-views: a main subview that's a ScrollView (it has lots of stuff in it - more than fits on the screen, so I'm using the ScrollView to deal with that) and a panel that appears when something in the configuration panel changes (with the usual things: a button to save and a button to revert). The problem is that I can't convince the ScrollView to shrink to allow the "save/revert" panel any space on the screen.
Here's an excerpt of the layout file: [EDIT: added contents of ScrollView]
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView">
<TextView
style="?android:listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/tweeterLK_options"
android:layout_marginTop="15sp"
android:id="#+id/textView5"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fgIntervalPrompt"
android:id="#+id/fgEnable"
android:checked="true"
android:paddingRight="5dp"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fgInterval"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckBox android:id="#+id/bgEnable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bgIntervalPrompt"
android:checked="true"
/>
<Spinner android:id="#+id/bgInterval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="#string/bgIntervalPrompt"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/tlkConfig_cacheDepth_prompt"
android:id="#+id/textView"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cacheDepth"
android:prompt="#string/tlkConfig_cacheDepth_prompt"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tlkConfig_preload_avatar"
android:id="#+id/preload_avatar"
android:checked="true"
android:layout_weight="1"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tlkConfig_preload_images"
android:id="#+id/preload_images"
android:checked="false"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/account"
style="?android:listSeparatorTextViewStyle"
android:layout_marginTop="15sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/signed_in_as"
android:id="#+id/signed_in_as"
android:visibility="visible"/>
<Button android:id="#+id/sign_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sign_in"
android:visibility="gone"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sign_out"
android:id="#+id/sign_out_button"
android:visibility="visible"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/lk_options_header"
style="?android:listSeparatorTextViewStyle"
android:layout_marginTop="14sp"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lkServer_enableLK"
android:id="#+id/lkServer_enable"
android:checked="false"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/lkServer_selection">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/lkServer_serverListLabel"
android:id="#+id/lkServerLabel"
android:layout_gravity="center_vertical"
android:layout_marginRight="3dp"/>
<Spinner android:id="#+id/lkServerSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="#string/lkServerSpinnerPrompt"
android:longClickable="true"/>
</LinearLayout>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lkServer_inUse"
android:id="#+id/lkServer_useRecommendation"
android:checked="false"/>
</LinearLayout>
</ScrollView>
<LinearLayout android:id="#+id/settings_changed"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:visibility="gone">
<Button android:id="#+id/save"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="#string/save_settings"
/>
<Button android:id="#+id/revert"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="#string/reset_settings"
/>
</LinearLayout>
</LinearLayout>
When I make the #id/save layout visible (using setVisibility(View.VISIBLE)) it doesn't cause that ScrollView to resize. Any thoughts on why that is and what I should do about it?
I have 2 options: a) fix the size of the scrollview so that it always leaves room for the panel, or b) get rid of the panel and just pop up a dialog when the back button is pressed. I like the second one better mostly because I already have that working.
There is no content within the scrollview.Please add contents
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView">
//Place your layouts and contents here for things to get scroll
</ScrollView>
I was wondering what I am doing wrong here, why does surrounding these buttons in a RelativeLayout cause an error? I haven't edited layouts much, can I not just stick in a Relative or Linear layout like that?
If I don't surround the buttons in a layout they appear beneath each other, I am just trying to experiment in having them beside each other horizontally.
I think the way I have done this whole layout is bad because vertically it looks quite squished.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout android:id="#+id/topButtons"
android:layout_margin="4dip"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/deviceConnect"
android:layout_margin="8dip"
android:layout_weight="3"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="Connect2"/>
<LinearLayout android:orientation="vertical"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:orientation="horizontal"
android:layout_margin="8dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="Connected Adapter:"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Spinner android:id="#+id/deviceSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"/>
<TextView android:id="#+id/currentSettings"
android:layout_marginLeft="8dip"
android:text="Current Settings: Not Connected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:orientation="horizontal"
android:layout_margin="8dip"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="Baud:"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Spinner android:id="#+id/baudSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_margin="8dip"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="Data:"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Spinner android:id="#+id/dataSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_margin="8dip"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="Parity:"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Spinner android:id="#+id/paritySpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_margin="8dip"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="Stop:"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Spinner android:id="#+id/stopSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<Button android:id="#+id/updateSettings"
android:layout_margin="8dip"
android:layout_weight="3"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="Update\nSettings"/>
</LinearLayout>
<RelativeLayout>
<Button android:id="#+id/Command"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Command"/>
<Button android:id="#+id/Command"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Command"/>
<Button android:id="#+id/Command"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Command"/>
<Button android:id="#+id/Command"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Command"/>
<Button android:id="#+id/Command"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Command"/>
</RelativeLayout>
<jackpal.androidterm.emulatorview.EmulatorView
android:id="#+id/emulatorView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="true"
android:focusableInTouchMode="true"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" >
<EditText
android:id="#+id/term_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:imeOptions="actionNone|flagNoExtractUi"
android:inputType="text|textImeMultiLine" />
<Button
android:id="#+id/term_entry_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="#string/entry_send" />
</LinearLayout>
As others have pointed out already, you need to define a layout_width and layout_height for your RelativeLayout. This should fix your error, unless there are other issues you have. But I thought it was worth noting what the DOCS say about layouts
A RelativeLayout is a very powerful utility for designing a user interface because it can eliminate nested view groups and keep your layout hierarchy flat, which improves performance. If you find yourself using several nested LinearLayout groups, you may be able to replace them with a single RelativeLayout.
You have many nested LinearLayouts so you may consider using a RelativeLayout for better performance and readability. You can also use android:orientation="horizontal" in a nested LinearLayout to get your buttons next to each other. But, again, a RelativeLayout may be better in your situation instead of having all of the nested layouts.
Your relativeLayout has no size.
Define layout_height and layout_width to it
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">