Why only one attribute in layout_gravity in my xml working - android

I want my reset button to be at bottom and horizontally centered, but layout_gravity is not working, only one of them is working.
I know it can be done by choosing RelativeLayout as my parent layout but I want to do it with LinearLayout as my parent layout.
I just want reset button as bottom and horizontally centered rest everything is fine.
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.android.courtcounter.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="TEAM A" />
<TextView
android:id="#+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="button3"
android:text="+3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="button2"
android:text="+2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="button1"
android:text="Free Throw" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="TEAM B" />
<TextView
android:id="#+id/team_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="button3_b"
android:text="+3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="button2_b"
android:text="+2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="button1_b"
android:text="Free Throw" />
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:onClick="resetButton"
android:padding="8dp"
android:text="RESET" />
</LinearLayout>

Use this instead if you don't want to go completely relative (inside the parent Linear Layout):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:onClick="resetButton"
android:padding="8dp"
android:text="RESET"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
or if you don't want to keep the reset button inside a relative layout, there doesn't seem to be a solution.
You can check this out as well: How to align views at the bottom of the screen?

Since the rest of your ViewGroups in that layout fill the parent's width, you could simply set the parent LinearLayout's gravity to center_horizontal and it should line up properly, without affecting the other children:
<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"
android:gravity="center_horizontal"
tools:context="com.example.android.courtcounter.MainActivity">
Edit: I didn't notice you also wanted it at the bottom... This can be achieved with an invisible weighted View between your content and the bottom:
...
<View
android:layout_width="1dip"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="resetButton"
android:padding="8dp"
android:text="RESET" />
</LinearLayout>
Edit 2: Removed layout_gravity on your button since it no longer applies.

Related

How do I align text to any position I want to align it to?

This is basically how I want my layout to look like:
There has to be a space separating the imageView from the Text. Ok button in the center, but I think the main challenge is making sure that the bold text is very closely aligned (almost touching the tip) to the regular text and is above it. Also, the added challenge is that all of this must be done using ONLY linear layout. So far, I have been using a vertical orientation linear layout followed by a horizontal orientation linear layout. The horizontal orientation linear layout covers the imageView and the regular text. My main problem is that I can not get the bold text and the regular text to align in the way I need it to.
By the way, I got rid of all the id's and images, since these are all private, but adding them should be no problem.
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="0dp"
android:text="Bold Text"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="13dp"
android:layout_marginTop="12dp"
android:src="#drawable/" />
<TextView
android:id="#+id/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Regular Text"
android:textColor="#color/black" />
</LinearLayout>
<Button
android:id="#+id/"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ok"
/>
</LinearLayout>
Unless this is for a school project or something I wouldn't actually use nested layouts like this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitXY"
app:srcCompat="#drawable/ic_launcher_background" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Bold Text"
android:textStyle="bold" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Edit:
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="10dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitXY"
android:layout_gravity="center_vertical"
app:srcCompat="#drawable/ic_launcher_background" />
Parent should be vertical linear layout, as you have now.
First child should be horizontal linear layout
First child of that should be ImageView
Second child should be BOLD TEXT with gravity=center_horizontal
Second child of your parent should be Regular Text Blah Blah Blah with gravity=center_horizontal
Third child of parent should be ok button with gravity=center_horizontal
If you want to add more space before things, give them android:marginStart="some_value"
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:background="#color/white"
android:padding="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/img"
android:layout_width="75dp"
android:layout_height="50dp"
android:layout_marginEnd="16dp"
android:src="#drawable/dummy_camera" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/tv1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Bold Text"
android:gravity="bottom"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv2"
android:layout_width="match_parent"
android:layout_height="20dp"
android:gravity="top"
android:text="Regular Text"
android:textColor="#color/black" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ok" />
</LinearLayout>
Add this to TextView parameters:
android:baselineAlignBottom="true"

Layout_align not working

I am new to android studio and had earlier used layout_alignParentBottom="true" and other alignment commands which worked well.
I was even getting the recommendation comments that android studio gives if it recognises a command.
But Now I created a new project and Android_studio here the code
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context="com.example.shaurya.courtcounter.MainActivity">
<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:background="#ffdab9"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Team A"
android:textAppearance="?android:textAppearanceLarge" />
<TextView
android:id="#+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="0"
android:textAppearance="?android:textAppearanceMedium" />
<Button
android:id="#+id/threepoints"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="increment_three"
android:text="3 Points" />
<Button
android:id="#+id/twopoints"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="increment_two"
android:text="2 Points" />
<Button
android:id="#+id/onepoints"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="increment_one"
android:text="Free Throw" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="reset" />
</LinearLayout>
``
At the extreme end of the cde there is a button which I a trying to align but alignment is not working
align attributes you used:
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
dosen't belong to linear layout behaviors, it's belonging to RelativeLayout.
so if you need to reach center for reset button with linearLayout, use
android:layout_gravity="center|anygravityYouWant"
and android:gravity="center".
also you facing some issue with width and height for linearLayout, because you are using ConstraintLayout as parent Layout.
Simply use the Relative Layout as your Parent Layout-
<?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"
android:background="#ffdab9">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Team A"
android:textAppearance="?android:textAppearanceLarge" />
<TextView
android:id="#+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="0"
android:textAppearance="?android:textAppearanceMedium" />
<Button
android:id="#+id/threepoints"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="increment_three"
android:text="3 Points" />
<Button
android:id="#+id/twopoints"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="increment_two"
android:text="2 Points" />
<Button
android:id="#+id/onepoints"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="increment_one"
android:text="Free Throw" />
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="reset" />
</RelativeLayout>

layout_gravity="bottom" doesn't work

Hello stackoverflow community!
I'm newbie both in programming and on this site, but let's get to the point.
I wrote an app:
<?xml version="1.0" encoding="utf-8"?>
<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:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"
tools:context="com.example.android.courtcounter.MainActivity"
android:padding="24dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="Team A"
android:textSize="14sp"
android:fontFamily="sans-serif-medium"/>
<TextView
android:id="#+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="0"
android:textSize="56sp"
android:textColor="#000000"
android:fontFamily="sans-serif-light" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="plus3a"
android:text="+3 Points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="plus2a"
android:text="+2 Points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="plus1a"
android:text="Free throw" />
</LinearLayout>
<View
android:layout_width="1dp"
android:background="#android:color/darker_gray"
android:layout_height="match_parent">
</View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"
tools:context="com.example.android.courtcounter.MainActivity"
android:padding="24dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="Team B"
android:textSize="14sp"
android:fontFamily="sans-serif-medium"/>
<TextView
android:id="#+id/team_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="0"
android:textSize="56sp"
android:textColor="#000000"
android:fontFamily="sans-serif-light"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="plus3b"
android:text="+3 Points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="plus2b"
android:text="+2 Points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="plus1b"
android:text="Free throw" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:onClick="scoreReset"
android:text="reset" />
</LinearLayout>
</LinearLayout>
I have a problem with positioning a button at the end.
I want it to be at the bottom of a screen and centered horizontally.
I know I could use relative layout but I decided to check if I can do this using linear layout. And seems that I can't... "bottom" attribute is being ignored.
Simply do one change.
Remove this line from the last button
android:layout_gravity="bottom|center_horizontal"
And add this line in the Linear layout which is containing your last button
android:gravity="bottom|center"
Here is the output:
Follow this link to get a clear idea about gravity and layout_gravity. It'll surely help you in your upcoming designs.
You can add this View before last button:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Add this -->
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:onClick="scoreReset"
android:text="reset" />
</LinearLayout>
Try encapsulating the main layout in one LinearLayout with layout_weight = 1, layout_width = "match_parent", layout_height = "match_parent"and button at bottom in another LinearLayout with layout_width="match_parent" and gravity set to bottom|center_horizontal.
This should work :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="bottom|center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:onClick="scoreReset"
android:text="reset"/>
</LinearLayout>
use gravity="bottom" instead of layout_gravity it should help.

Linear Layout is not properly showing the component when using android studio

This is code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Email:"
android:textSize="30dp"
android:paddingLeft="0dp"
android:paddingTop="10dp"/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:singleLine="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:paddingTop="10dp"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:text="Home" />
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:text="About" />
</LinearLayout>
</LinearLayout>
I want to practice a layout in android studio and I'm getting an error while implementing the code.
This is the text which goes outside the mobile screen:
Why does the blue box go outside the mobile screen?
You're not properly setting the layout. The childViews of your parentView(LinearLayout) was aligned horizontally without setting the layout weight. You should read the documentation on LinearLayout. However, to fix your layout, you can add another container to hold the first row of your layout to make the second row visible on the screen. Your current code contains all the childViews in a single row which makes the other views not visible. Try the code below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Email:"
android:textSize="30dp"
android:paddingLeft="0dp"
android:paddingTop="10dp"/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:singleLine="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:paddingTop="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:text="Home" />
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:text="About" />
</LinearLayout>
</LinearLayout>
You are setting the layout_width of each view yourself. You should know that there is a maximum width of screen that is available. Moreover, you are setting the width of button as match_parent that is not proper way when the neighbouring views have some fixed width. If your are using LinearLayout try layout_weight attribute. Play around and try to experiment.
For example:
<?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">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:paddingLeft="0dp"
android:paddingTop="10dp"
android:text="Email:"
android:textSize="30dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_weight="2"
android:singleLine="true" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:paddingTop="10dp"
android:text="Login" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:text="Home" />
<TextView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:text="About" />
</LinearLayout>
</LinearLayout>

Put buttons at bottom of screen with LinearLayout?

I have the following code, how do I make it so that the 3 buttons are at the bottom?
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:gravity="center"
android:text="#string/observer"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:context=".asdf"
android:weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="1" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="2" />
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="3" />
</LinearLayout>
You need to ensure four things:
Your outside LinearLayout has layout_height="match_parent"
Your inside LinearLayout has layout_weight="1" and layout_height="0dp"
Your TextView has layout_weight="0"
You've set the gravity properly on your inner LinearLayout: android:gravity="center|bottom"
Notice that fill_parent does not mean "take up all available space". However, if you use layout_height="0dp" with layout_weight="1", then a view will take up all available space (Can't get proper layout with "fill_parent").
Here is some code I quickly wrote up that uses two LinearLayouts in a similar fashion to your code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/db1_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/cow"
android:layout_weight="0"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="1" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="2" />
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="3" />
</LinearLayout>
</LinearLayout>
The result looks like similar to this:
You can use a RelativeLayout and align it to the bottom with android:layout_alignParentBottom="true"
Create Relative layout and inside that layout create your button with this line
android:layout_alignParentBottom="true"
You can do this by taking a Frame layout as parent Layout and then put linear layout inside it. Here is a example:
<FrameLayout 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="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="16sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="16sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="16sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="16sp"/>
</LinearLayout>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="bottom"
/>
</FrameLayout>
first create file name it as footer.xml
put this code inside it.
<?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="78dp"
android:layout_gravity="bottom"
android:gravity="bottom"
android:layout_weight=".15"
android:orientation="horizontal"
android:background="#drawable/actionbar_dark_background_tile" >
<ImageView
android:id="#+id/lborder"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".14"
android:background="#drawable/action_bar_left_button"
android:src="#drawable/overlay" />
<ImageView
android:id="#+id/unknown"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".14"
android:background="#drawable/action_bar_left_button"
android:src="#drawable/notcolor" />
<ImageView
android:id="#+id/open"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".14"
android:background="#drawable/action_bar_left_button"
android:src="#drawable/openit"
/>
<ImageView
android:id="#+id/color"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".14"
android:background="#drawable/action_bar_left_button"
android:src="#drawable/colored" />
<ImageView
android:id="#+id/rborder"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/action_bar_left_button"
android:src="#drawable/frames"
android:layout_weight=".14" />
</LinearLayout>
then create header.xml and put this code inside it.:
<?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="#dimen/action_bar_height"
android:layout_gravity="top"
android:baselineAligned="true"
android:orientation="horizontal"
android:background="#drawable/actionbar_dark_background_tile" >
<ImageView
android:id="#+id/contact"
android:layout_width="37dp"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_weight=".18"
android:scaleType="fitCenter"
android:background="#drawable/action_bar_left_button"
android:src="#drawable/logo"/>
<ImageView
android:id="#+id/share"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_weight=".14"
android:background="#drawable/action_bar_left_button"
android:src="#drawable/share" />
<ImageView
android:id="#+id/save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".14"
android:background="#drawable/action_bar_left_button"
android:src="#drawable/save" />
<ImageView
android:id="#+id/set"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".14"
android:background="#drawable/action_bar_left_button"
android:src="#drawable/set" />
<ImageView
android:id="#+id/fix"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".14"
android:background="#drawable/action_bar_left_button"
android:src="#drawable/light" />
<ImageView
android:id="#+id/rotate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".14"
android:background="#drawable/action_bar_left_button"
android:src="#drawable/ic_menu_rotate" />
<ImageView
android:id="#+id/stock"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".14"
android:background="#drawable/action_bar_left_button"
android:src="#drawable/stock" />
</LinearLayout>
and then in your main_activity.xml and put this code inside it :-
<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="fill_parent"
tools:context=".MainActivity"
android:id="#+id/relt"
android:background="#drawable/background" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="78dp"
android:id="#+id/down"
android:layout_alignParentBottom="true" >
<include
android:layout_width="fill_parent"
android:layout_height="78dp"
layout="#layout/footer" >
</include>
</LinearLayout>
<ImageView
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/down"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/inc"
>
</ImageView>
<include layout="#layout/header"
android:id="#+id/inc"
android:layout_width="fill_parent"
android:layout_height="50dp"></include>
happy coding :)
<LinearLayout
android:id="#+id/LinearLayouts02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom|end">
<TextView
android:id="#+id/texts1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="2"
android:text="#string/forgotpass"
android:padding="7dp"
android:gravity="bottom|center_horizontal"
android:paddingLeft="10dp"
android:layout_marginBottom="30dp"
android:bottomLeftRadius="10dp"
android:bottomRightRadius="50dp"
android:fontFamily="sans-serif-condensed"
android:textColor="#color/colorAccent"
android:textStyle="bold"
android:textSize="16sp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</LinearLayout>
Just add layout_weight="1" to in your linearLayout which having Buttons.
Edit :- let me make it simple
follow something like below, tags name may not be correct, it is just an Idea
<LL>// Top Parrent LinearLayout
<LL1 height="fill_parent" weight="1" "other tags as requirement"> <TV /><Butons /></LL1> // this layout will fill your screen.
<LL2 height="wrap_content" weight="1" orientation="Horizontal" "other tags as requirement"> <BT1 /><BT2/ ></LL2> // this layout gonna take lower part of button height of your screen
<LL/> TOP PARENT CLOSED
You can bundle your Button(s) within a RelativeLayout even if your Parent Layout is Linear. Make Sure the outer most parent has android:layout_height attribute set to match_parent.
And in that Button tag add
'android:alignParentBottom="True" '
You can use the Space widget with layout_weight=1 to fill up the space between your widgets and the bottom buttons. See example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/date_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:hint="#string/date_hint">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/date_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/time_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:hint="#string/time_hint">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/time_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:inputType="datetime" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/food_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="#string/food_hint">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/food_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/calories_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="#string/calories_hint">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/calories_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/add_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/add_button" />
</LinearLayout>
Add android:windowSoftInputMode="adjustPan" to manifest - to the corresponding activity:
<activity android:name="MyActivity"
...
android:windowSoftInputMode="adjustPan"
...
</activity>
In my case I was trying to put a complete Linear Layout at the bottom in Linear Layout. When I tried using the "layout_gravity" to bottom it was not working. So I changed the root layout to the Frame Layout and it worked perfectly.
So If you want to put a layout at bottom try using Frame Layout and put everything else inside a nested Linear or Relative Layout.

Categories

Resources