I have a linear layout inside a relative layout, and I want to make the buttons 'taller' to fit the height of the linear layout.
I tried adding padding, as you can see in the screenshot, the layout is 'taller' but the buttons are not. I tried 'fill_parent', 'match_parent' but it didn't make a difference. How do I make the buttons expand in height?
<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:id="#+id/timerLayout">
<Chronometer
android:id="#+id/chronometer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text="#string/chronometer"
android:textSize="50sp" />
<LinearLayout
android:id="#+id/buttonLayout"
android:orientation="horizontal"
android:paddingLeft="4.0dp"
android:paddingTop="50.0dp"
android:paddingRight="4.0dp"
android:paddingBottom="4.0dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/btnReset"
android:layout_width="0.0dp"
android:layout_height="fill_parent"
android:drawableLeft="#drawable/ic_restore_white_24dp"
android:drawableStart="#drawable/ic_restore_white_24dp"
android:text="#string/pause_btn"
android:layout_weight="1.5" />
<Button
android:id="#+id/btnStart"
android:layout_width="0.0dp"
android:layout_height="fill_parent"
android:text="#string/start_btn"
android:drawableLeft="#android:drawable/ic_media_play"
android:drawableStart="#android:drawable/ic_media_play"
android:layout_weight="1.5" />
<Button
android:id="#+id/btnSave"
android:layout_width="0.0dp"
android:layout_height="fill_parent"
android:text="#string/save_btn"
android:drawableLeft="#drawable/ic_save_white_24dp"
android:drawableStart="#drawable/ic_save_white_24dp"
android:layout_weight="1.0" />
</LinearLayout>
The extra space you are seeing is from the padding values in your linear layout, and your buttons are already filling the parent linear layout. But your linear layout has specified the following.
android:layout_height="wrap_content"
If you want to see buttons with a larger height, change the height value for your linear layout to something else like:
android:layout_height="300dp"
your linearlayout says height:wrap_content
so the child elements will takeup only as much space as specified.
Try giving either minHeight for linearlayout or simply remove linearlayout and in your buttons specifiy "alignparentbottom = true".
For left-most button give alignparentStart = true.
Take a look at your xml layout file very closely
<LinearLayout
android:id="#+id/buttonLayout"
android:orientation="horizontal"
android:paddingLeft="4.0dp"
android:paddingTop="50.0dp" //this guy
android:paddingRight="4.0dp"
android:paddingBottom="4.0dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
the android:paddingTop is the space on the top of every child in your LinearLayout so if you remove it the buttons will fit.
Related
My requirement is to achieve this in LinearLayout. It is having only one child view.
In horizontal orientation, a button needs to be at center horizontal, if layout_gravity is set.
my layout xml is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="200dp"
android:background="#ffff00"
android:layout_width="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button" />
</LinearLayout>
But the result is like below image
What i am expecting is, when the layout_gravity is set to center_horizontal , the button center to the horizontal. Why this is not working
The problem with yours is not with your actual element but with your parent element. When you define the same property to the parent element, your issue will be resolved.
add the following attribute to the LinearLayout
android:gravity="center_horizontal"
You can achieve it in two Method,
Set android:gravity="center_horizontal" in layout properties
or
Set android:layout_gravity="center" in Button properties
Your Layout to be in "Vertical" Orientation
We need to figure out the question is:
The widget can only use vertical attribute (like top, center_vertical, bottom) when it's root layout useorientation: horizontal
The widget can only use horizontal attribute (like left, center_horizontal, right) when it's root layout use orientation: vertical
So that you need to do like this(tested & works):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="200dp"
android:background="#ffff00"
android:layout_width="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button" />
</LinearLayout>
Test Image
Set center horizontal gravity to the parent:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="200dp"
android:background="#ffff00"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
or just change parent orientation to horizontal
Actually you cant. Your root layout is horizontal orientationL android:orientation="horizontal". If you want it work, just change root's orientation to vertical (I dont test yet!)
I have a CircularMeter class which is derived from Button. The problem is that it is not resizing even if the weight given is 0.5 (ie. half the vertical screen).
<LinearLayout
android:id="#+id/WidgetDataLayout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_weight="0.5"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
<com.test.CircularMeter
android:id="#+id/cm1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Any Ideas ?
You can take help from this example to make your circularmeter fill half of the vertical screen
eg.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="0.5"
android:background="#123456"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#123456"
android:text="#string/hello_world" />
</LinearLayout>
</LinearLayout>
There's no layout_weight in CircularMeter.
It's parent LinearLayout has both android:layout_alignParentTop="true" and android:layout_weight="0.5" which is not correct since layout_alignParentTop is a layout attribute for a RelativeLayout parent while android:layout_weightis an attribute for a LinearLayout parent. The layout XML does not show what is the parent layout actually is. android:layout_height="0dp" would make the layout invisible unless there really is a vertical LinearLayout as a parent.
Also note that layout_weight="0.5" won't make the view size half the screen. After doing first pass layout for a linear layout, the weight mechanism just distributes any remaining space in proportion of element weights. By default an element's weight is 0. So if you have just one element with non-zero weight, it will get all remaining space in its linear layout parent.
I am displaying a textview(I intend this to fill the entire screen excluding the button below it) and a button(small one at the bottom) in an activity. I want textview to be placed aove the button. I don't want to hardcode any height/width.
Hence
For button I have kept height ad width as word_wrap
For textview I have kept width as fill parent.
What I want to know is that, is there anyway by whcih I can specify textview height to be screenheight-button height. I want to this in xml file but not dnamically inside my code.
To achieve this you have to create something like this :
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button1"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
and you have a textview which will stay above your button and will fit the entire screen.
Use RelativeLayout, set textview's dimensions as fill_parent, then place button below it,
set button's android:layout_below="#+id/textview" and button's dimensions as wrap_content.
Check the result in visual designer or in device, it should work
You can use layout_weight attribute
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:text="TextView" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
I have an Android application that goes about like 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">
<RelativeLayout
android:id="#+id/toplayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.90" >
<Button
android:id="#+id/button_1_of_10"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/text_0x1701" />
<Button
android:id="#+id/button_2_of_10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/button_1_of_10"
android:layout_below="#+id/button_1_of_10"/>
<!--Another 8 buttons-->
<RelativeLayout
android:id="#+id/contentpane"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/button_1_of_10" >
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/actionbuttonslayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.10">
</RelativeLayout>
</LinearLayout>
I need the 10 buttons to fill up the entire height from the top to the footer (the 0.1 weighted RelativeLayout), while all the buttons keep an equal height. However, I want to know whether there is a kind of equivalent to the layout_weight from LinearLayout, but for RelativeLayouts, as it's not performant to have nested weights in LinearLayouts. I'm not really looking for other solutions, because I still have some thing to try, but I want to know whether or not this is possible?
My question, just to be clear, is: Is it possible to have an amount of Buttons with an equal height in a RelativeLayout and at the same time fill up all the available space?
To use weight you need to use a LinearLayout, just make a linear layout with the buttons take the space you want and then on each button inside have android:layout_height="0dp" and also a android:layout_weight="1"
You don't need to sum all the weights to 1, just think of items with the same weight have the same size
So my layout looks basically like this:
<ScrollView>
<RelativeLayout>
<BunchOfViews/>
<ImageView android:layout_alignParentBottom="true"/>
</RelativeLayout>
</ScrollView>
I have the ScrollView so all of the layout always is visible no matter the height of the screen. The problem is that on a very high screen, I still want my ImageView to be at the bottom. However, a child of a ScrollView don't seem to have a defined bottom. The View is placed at the top of the layout. How can I solve this problem in a neat way?
I ran into the same issue. I never could find a very pleasing solution, but here is how I did it. Maybe someone else has a better way, I hate adding layouts that don't do anything.
My hack was to add a dummy linearlayout at the bottom of the scrollview that has fill_parent to take up all the room and force the scrollview to fill the screen. Then add whatever component I want to that linearlayout.
Here is one of my layouts that does this:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="15px" >
<!-- bunch of components here -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/spinner"
android:layout_marginTop="5px"
android:gravity="center_horizontal|bottom"
android:paddingTop="2px" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20px"
android:paddingRight="20px"
android:text="Delete" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
I had the same issue and found this page:
http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/
Basically, you set the ScrollView's android:fillViewport to true, which will allow the child view to expand to the same height as the ScrollView itself, filling out the space. You then just need to have one of the child controls' layout_height set to fill_parent and layout_weight to 1, causing that control to "spring" to fill the empty space.
Note that if the contents of the ScrollView are already tall enough to fill the ScrollView, the android:fillViewport has no effect, so the setting only kicks in when needed.
My final XML looks like similar to this:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_height="fill_parent"
android:layout_weight="1">
<!-- this expands to fill the empty space if needed -->
</LinearLayout>
<!-- this sits at the bottom of the ScrollView,
getting pushed out of view if the ScrollView's
content is tall enough -->
<ImageView
android:layout_height="wrap_content"
android:src="#drawable/footer_image">
</ImageView>
</LinearLayout>
</ScrollView>
it seems that the linearlayout isn't necessary, all that is important is the fillViewPort.
you could just use
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottomParent="true">
now that you have specified the relativelayout to be at least the size of the screen.
its work for me perfectly android:fillViewport="true"
Joel Malone's answer Adding view to bottom of layout inside a scrollview does that trick. My solution is almost the same, except that I use Space widget to do the work of filling the rest height inside the parent layout. Like this:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
>
<android.support.v4.widget.Space
android:layout_height="match_parent"
android:layout_weight="1"
/>
<ImageView
android:layout_height="wrap_content"
android:src="#drawable/footer_image"
/>
</LinearLayout>
</ScrollView>
Attention:
fill_parent in the other answers is noted as deprecated for a long time;
Compare to an empty LinearLayout to fill the rest of parent layout, I think Space is much more appropriate(it's designed to do that job.)
Finally, don't forget that important attribute at the beginning of ScrollView: android:fillViewport="true". They together make this trick.
just put this attribute android:fillViewport="true" to your ScrolView
and you will got what you need
On your view that you want to be at the bottom use android:gravity="bottom"
From: http://code.google.com/p/k9mail/source/browse/k9mail/trunk/res/layout/account_setup_basics.xml?r=1314
This should help you:
<RelativeLayout
android:layout_marginTop="-45dip"
android:padding="0dip"
android:layout_alignParentBottom="true"
android:gravity="bottom|right"
android:background="#android:drawable/bottom_bar"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<Button
android:id="#+id/manual_setup"
android:text="#string/account_setup_basics_manual_setup_action"
android:minWidth="#dimen/button_minWidth"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginBottom="-4dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="false"
/>
<Button
android:id="#+id/next"
android:text="#string/next_action"
android:minWidth="#dimen/button_minWidth"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:drawableRight="#drawable/button_indicator_next"
android:layout_marginBottom="-4dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="false"
/>
</RelativeLayout>
ScrollView view = new ScrollView( this );
ScrollView.LayoutParams lps = new FrameLayout.LayoutParams( FILL_PARENT, FILL_PARENT, Gravity.CENTER );
LinearLayout layout = new LinearLayout( this );
// what ever you want in the Layout
view.addView( layout, lps );
I tried alot to align the Scroll View to bottom of the screen but thats not possible according to this link.
https://newbedev.com/how-do-i-align-views-at-the-bottom-of-the-screen
The way i found was to create a view with 1dp height and aa id (lets say android:id="#+id/bottomView") at the bottom of your XML page.
now just add these attributes to your scroll view..
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true"
android:layout_alignBottom="#+id/bottomView"
>
Here is how you can use a ConstraintLayout to align the image view at the bottom:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/content_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Text 1"
app:layout_constraintBottom_toTopOf="#+id/space_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent" />
<Space
android:id="#+id/space_view"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/image_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_view" />
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="..."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/space_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>