view not snapped to top on LinearLayout - android

I have a LinearLayout and its XML is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Button" />
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button" />
</LinearLayout>
and it is the result is:
as you can see the top left button has a little margin from top but as the code shows there are no margins.
why is this happening?
also there is a weird solution which is if you set gravity:top to all buttons you will get the expected result. but why is it even needed because linearlayout(horiz) should start adding items from top left to top right.

I was referring some documents and SO threads to look for the solution as the question seemed very intresting to me.
Finally I have found out the reason.
A horizontal LinearLayout aligns the baselines of all its child controls by default. So the first line of text in your multi-line button is vertically aligned with the single line of text in the other buttons.
To disable this behaviour, set android:baselineAligned="false" on the LinearLayout.
All credit goes to #Karu for this answer : https://stackoverflow.com/a/8290258/4211264

This is due to text wrapping in first button. Other two buttons take too match space on screen. And first button try's to stay on screen by self wrapping. I don't know what is your task. If you want to keep buttons in one line try to use layout_weight = 1 on all buttons.

This is because you have given different weights to every button. You need to give equal weight to every button. Replace your layout file code with this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />

Related

Button changes vertical position in LinearLayout with weights when one button has long text

I create a layout like this:
<?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="horizontal"
android:padding="16dp">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:text="Short text" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="Short text" />
</LinearLayout>
The result is as expected, the two buttons share the width and have the same vertical position:
When I give one of the buttons a long text that will let the button grow vertically (which is intended), this happens:
The button with the long text changes its vertical position and its top is slightly below the other buttons top.
The effect does not show in a ConstraintLayout.
Any ideas on 1. why this is happening and 2. how this can be fixed within a LinearLayout?
Edit: targetSdk and compileSdk are 30.
Button will expand horizontally in most of the settings whether set weight in button and width wrap_content in button or in cascading linear layout so the preferable soln is as follows it will align horizontally equally as required:
<?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="horizontal"
android:padding="16dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Short text" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Short text can expand and exapnd and expand" />
</LinearLayout>
I will answer your 2 questions,
This is happening because you are using wrap content which results in wrapping the text when the text reaches the weight of button.
In order to solve your problem you can use either single line which results in showing your text as "This is a lon...", or you can use the gravity of your parent as center so that even if text reaches the limit both the buttons will be aligned center and will not be looking annoying.
But if you want both the buttons to be of same height, you need to compromise your text size or your design.

Children views having equal height

I have a horizontal LinearLayout with two buttons with text inside.
The LinearLayout's and the buttons layout_height is wrap_content.
The text of one of the two buttons takes two lines unlike the other's which takes one line.
So at the end one of the buttons has a bigger height than the other which is something that I don't want.
I know how to solve this programmatically. So the reason I making this question is to know whether I can solve this through xml.
One possible solution is, for the button whose text is one line, set
layout_height="match_parent"
and it works fine.
But the problem here is that generally I don't know which button will occupy the biggest height.
In essence what I want to do is: having a LinearLayout with Views inside
I want to set the height of all the children Views to be equal to the height of the View which when has its content wrapped has the max height.
And the question is if this is possible through xml?
An example XML. Also I forgot to add that I already have 0dp in the widths.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/some_string" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/some_other_string" />
</LinearLayout>
I guess what you search is android:baselineAligned="false", this will avoid that the text are on the same baseline and the buttons will start at the same y position on the screen.
Here without android:baselineAligned="false":
And here with:
However if you also want that both buttons are equal sized try this layout example:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="aaaaa"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="bbbbbbb bbbbbbbb bbbbbbbb bbbbbbbbbbbb bbbbbb"/>
</LinearLayout>
That will look like this:
Use this thing to set weigh Sum and weight layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:orientation="horizontal" >
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button 1" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button 2" />
</LinearLayout>
</LinearLayout>

Linear Layout margin added by default. How to avoid that? Android

I have created 4 buttons in linear layout. This is my xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<Button android:id="#+id/button1"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:text="Account"
/>
<Button android:id="#+id/button2"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:text="History"
/>
<Button
android:id="#+id/button3"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:text="Settings"
/>
</LinearLayout>
But above layout adding margins all sides for all 3 buttons. Why is that margins is added by default? How to avoid that?
Try this,it works like you want,
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button android:id="#+id/button1"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="Account"
android:background="#80ffdd10"
/>
<Button android:id="#+id/button2"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#800aad10"
android:text="History"
/>
<Button
android:id="#+id/button3"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="Settings"
android:background="#8c9eff"
/>
</LinearLayout>
These are the shadow by default of Android Button Widget. However If you want to attach one button with another forcefully then add the following properties to each buttons as per needed
android:layout_marginRight="-5dp"
android:layout_marginLeft="-5dp"
Note : Increase or decrease the value (Currently -5dp) of the properties as it suits.
In button, set android:layout_width="0dp" rather than wrap_content
It's because you have given weight attribute to buttons in horizontal LinearLayout, each Button will fill 1/3 of the root layout horizontally.
And if you are using a vertical Linear Layout, you should set android:layout_height="0dp"
Hope this will help you.
You can use custom background, it will solve your problem
android:background="#8c9eff"

Flat buttons, listview android

I have a hard time pointing out what UI elements are used here? Is it buttons or listviews?
This view is not seems as a list view. They are either Buttons or ImageButtons fitted in some layout.
If you want to make this in a ListView, you are gonna have a hard time. The best would be to make it in a GridLayout, if you are not that confident in working with that then the easiest solution would be a LinearLayout in the root with vertical orientation, and then add a LinearLayout with horizontal orientation where the childs have `weight="1"
Something like this (Just for the first two buttons):
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp">
<FrameLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:clickable="true"
android:layout_marginRight="10dp"
android:onClick="onChildOneClick">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button 1"
android:background="android:color/green" />
</FrameLayout>
<FrameLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:clickable="true"
android:layout_marginLeft="10dp"
android:onClick="onChildTwoClick" />
</LinearLayout>
</LinearLayout>
Inside your framelayout you can put a ImageView + TextView to make it look like the sketch

Android LinearLayout make button doesn't work

I have many buttons in my layout and they work well. I want to put the two buttons on the same row, so I make a LinearLayout horizontal, but when I do that, the second button's click event doesn't work. It doesn't show me my textview
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/bAnswerQuestoinShowChoices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Choices"
android:visibility="invisible" />
<Button
android:id="#+id/bAnswerQuestionShowHints"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Hint 1"
android:visibility="invisible" />
</LinearLayout>
If I remove the LinearLayout it works fine, but I want the linearlayout because I want the two buttons to be on the same row.
android:layout_height="match_parent" //<----------------------this is the issue make it warp content
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" //<----------------------this is the issue make it warp content
android:orientation="horizontal" >

Categories

Resources