Image in layout is not completely on top, where I want it - java

Edit, it's on top, now it's zoomed in
If it's possible, i would want it to fill the entire screen, width wise, with the image, completely on top, leaving no space between the top and the image. This is not working for me.
This is the main.xml code that I have, it could be completely wrong, please tell me if that's the case.
This is how it looks in the emulator, I want it to fill the entire top side from left to right, completely on the top.
Any help would be appreciated, i'm pretty new to this, so don't be scared to tell me i'm wrong :)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.rodekruis.MainActivity">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/rkz_logo"
/>
</RelativeLayout>
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button9"
android:layout_alignLeft="#+id/button10"
android:layout_marginBottom="30dp"
android:layout_gravity="top"
android:clickable="true"
android:text="Nieuws" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
<Button
android:id="#+id/button10"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/button8"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text=" Afspraak maken" />
<Button
android:id="#+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text=" Bezoek tijden" />
<Button
android:id="#+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text="Contact" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text=" Geef je mening!" />
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text=" Route begeleiding" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignLeft="#+id/button5"
android:layout_below="#+id/button8"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text="Specia-listen" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Button
android:id="#+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="BWC" />
<Button
android:id="#+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="Agenda" />
<Button
android:id="#+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text="Praktische
informatie" />
</LinearLayout>
</LinearLayout>

try this,
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:src="#drawable/rkz_logo"
/>

just remove your paddings. I hope it will help
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"

Remove two line in layout root
android:gravity="center"
android:paddingTop="#dimen/activity_vertical_margin"
Try this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
tools:context=".MainActivity">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginBottom="30dp"
android:clickable="true"
android:text="Nieuws" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
<Button
android:id="#+id/button10"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text=" Afspraak maken" />
<Button
android:id="#+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text=" Bezoek tijden" />
<Button
android:id="#+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text="Contact" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text=" Geef je mening!" />
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text=" Route begeleiding" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignLeft="#+id/button5"
android:layout_below="#+id/button8"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text="Specia-listen" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Button
android:id="#+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="BWC" />
<Button
android:id="#+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="Agenda" />
<Button
android:id="#+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text="Praktische informatie" />
</LinearLayout>

android:layout_alignParentTop="true" on the parent of the ImageView is ignored because its parent (the root view) is not a RelativeLayout. Try changing the root view to RelativeLayout, and remove its padding (at least the top one).

Related

LinearLayout with fixed buttons width

Firsty, I've created something like that:
BadLayout
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="76dp"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/button1"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="120dp"
android:layout_height="140dp"
android:layout_marginEnd="10dp"
android:layout_weight="1"
android:backgroundTint="#color/colorPrimary"
android:text="Button1"
android:textSize="18sp"/>
<Button
android:id="#+id/button2"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="120dp"
android:layout_height="140dp"
android:layout_marginStart="10dp"
android:layout_weight="1"
android:backgroundTint="#color/colorPrimary"
android:text="Button2"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/LinearLayout1"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/button3"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="120dp"
android:layout_height="140dp"
android:layout_marginEnd="10dp"
android:layout_weight="1"
android:backgroundTint="#color/colorPrimary"
android:text="Button3"
android:textSize="18sp" />
<Button
android:id="#+id/button4"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="120dp"
android:layout_height="140dp"
android:layout_marginStart="10dp"
android:layout_weight="1"
android:backgroundTint="#color/colorPrimary"
android:text="Button4"
android:textSize="18sp" />
</LinearLayout>
But this layout doesn't work properly. Buttons don't have fixed width. The wider the screen is, the wider buttons are. It doesn't look good for me, because I want buttons to be perfect squares (e.g. 120dp x 120dp). I also want them to be centered. It should looks like that: GoodLayout. How to do that?
you can try this code to manage your view according to image you added:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="#dimen/activity_vertical_margin">
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="76dp"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_marginEnd="10dp"
android:backgroundTint="#color/colorPrimary"
android:text="Button1"
android:textSize="18sp"/>
<Button
android:id="#+id/button2"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_marginStart="10dp"
android:backgroundTint="#color/colorPrimary"
android:text="Button2"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/LinearLayout1"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/button3"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_marginEnd="10dp"
android:backgroundTint="#color/colorPrimary"
android:text="Button3"
android:textSize="18sp" />
<Button
android:id="#+id/button4"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_marginStart="10dp"
android:backgroundTint="#color/colorPrimary"
android:text="Button4"
android:textSize="18sp" />
</LinearLayout>
Try this code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="76dp"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="2">
<Button
android:id="#+id/button1"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="120dp"
android:layout_height="140dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:backgroundTint="#color/colorPrimary"
android:text="Button1"
android:textSize="18sp"/>
<Button
android:id="#+id/button2"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="120dp"
android:layout_height="140dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:backgroundTint="#color/colorPrimary"
android:text="Button2"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/LinearLayout1"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="2">
<Button
android:id="#+id/button3"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="120dp"
android:layout_height="140dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:backgroundTint="#color/colorPrimary"
android:text="Button3"
android:textSize="18sp" />
<Button
android:id="#+id/button4"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="120dp"
android:layout_height="140dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:backgroundTint="#color/colorPrimary"
android:text="Button4"
android:textSize="18sp" />
</LinearLayout>
Try this answer it will surely work on any sreen size without changing size of button
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="#+id/button1"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="120dp"
android:layout_height="120dp"
android:backgroundTint="#color/colorPrimary"
android:text="Button1"
android:textSize="18sp"
/>
<Button
android:id="#+id/button2"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginLeft="20dp"
android:backgroundTint="#color/colorPrimary"
android:text="Button2"
android:textSize="18sp"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/LinearLayout1"
android:layout_marginTop="20dp"
android:orientation="horizontal"
>
<Button
android:id="#+id/button3"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="120dp"
android:layout_height="120dp"
android:backgroundTint="#color/colorPrimary"
android:text="Button3"
android:textSize="18sp" />
<Button
android:id="#+id/button4"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginLeft="20dp"
android:layout_gravity="center_vertical"
android:backgroundTint="#color/colorPrimary"
android:text="Button4"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>

Evenly spacing buttons android

I'm currently working on an android application. I can't get buttons evenly spaced on a screen.
Below is my code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main_menu"
android:background="#354739"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.cssdapp.MainMenuActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
/>
<Button
android:text="Log Off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#6b5847"
android:id="#+id/logOff"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:text="Manage Farmers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#6b5847"
android:id="#+id/button4"
android:layout_above="#+id/button5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:text="My Account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#6b5847"
android:id="#+id/button5"
android:layout_marginBottom="47dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:text="Manage Farmers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#6b5847"
android:id="#+id/button3"
android:layout_above="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="21dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:text="Order History"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#6b5847"
android:id="#+id/button2"
android:layout_above="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:text="Make an Order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#6b5847"
android:id="#+id/makeOrder"
android:layout_above="#+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="36dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
I want the buttons evenly spaced down the screen below the image. At the minute the image is where I want to be however I can't get the buttons to where I want them to be. I'm trying to use the gui designer however they won't snap into place. I'm trying to figure out the code way to do it.
Any help appreciated.
In Order to divide the buttons on the whole screen evenly and to maintain synchronization on all devices you need to use a LinearLayout
as your parent View And and another LinearLayout as the parent for each button and apply to it the weight attribute to divide them evenly on the screen Cause you can't just apply the weight attribute on the button itself because the (height or width of it depends on if the layout orintation is horizontal or vertical will gets scaled to fill the whole space) here is a code snippet to demonstrate what am saying
`
<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:id="#+id/activity_main_menu"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<Button
android:id="#+id/logOff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Log Off"
android:textAllCaps="false"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button4"
android:text="Manage Farmers"
android:textStyle="bold"
android:textAllCaps="false"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<Button
android:text="My Account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button5"
android:textAllCaps="false"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<Button
android:text="Order History"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:textAllCaps="false"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<Button
android:text="Make an Order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/makeOrder"
android:textAllCaps="false"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>`
I have done some changes, Please check if it works for you
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#354739"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<ImageView
android:id="#+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:src="#drawable/ic_android_black_24dp"/>
<Button
android:id="#+id/logOff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/iv"
android:layout_marginTop="23dp"
android:background="#6b5847"
android:text="Log Off"/>
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button5"
android:layout_marginTop="23dp"
android:background="#6b5847"
android:text="Manage Farmers"/>
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/logOff"
android:layout_marginTop="23dp"
android:background="#6b5847"
android:text="Manage Farmers"/>
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button4"
android:layout_marginTop="23dp"
android:background="#6b5847"
android:text="My Account"/>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button5"
android:layout_marginTop="23dp"
android:background="#6b5847"
android:text="Order History"/>
<Button
android:id="#+id/makeOrder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button2"
android:layout_marginTop="23dp"
android:background="#6b5847"
android:text="Make an Order"/>
</RelativeLayout>

Adding textviews under buttons

I've made a lay-out with 10 buttons. In those buttons i have icons, showing what the information inside is about. Under those buttons, I want to have a word to clearify that again.
I have no idea how to add textviews and place them exactly under those buttons. I have tried it from the lay-out, by just pressing Textview and put it under the buttons, but that way my entire lay-out goes downwards, and the position is not right under it, but slightly left of it.
Hope someone can help me, every kind of help is appreciated.
This is my layout, the red circles are where I want the text to be.
Click here to see the image
This is my code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.rodekruis.MainActivity"
android:background="#drawable/rkzafbeelding">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.rodekruis.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageButton1"
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_marginBottom="20dp"
android:layout_marginRight="40dp"
android:layout_marginTop="10dp"
android:layout_gravity="right"
android:src="#drawable/informatiebutton" />
<ImageView
android:layout_marginBottom="50dp"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:scaleType="fitStart"
android:src="#drawable/logoversiejuni" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="0dp"
android:orientation="horizontal">
<ImageButton
android:id="#+id/button10"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/button10"
android:layout_marginRight="5dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/afspraakbutton"
android:text=" Afspraak maken" />
<ImageButton
android:id="#+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/button8"
android:layout_marginRight="5dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/bezoektijdenbutton"
android:text=" Bezoek tijden" />
<ImageButton
android:id="#+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/button8"
android:layout_marginRight="5dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/contactbutton"
android:text=" Contact" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
<ImageButton
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/button8"
android:layout_marginRight="5dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/routebutton"
android:text="Route" />
<ImageButton
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/button8"
android:layout_marginRight="5dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/meningbutton"
android:text="Uw mening" />
<ImageButton
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/button8"
android:layout_marginRight="5dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/vriendenbutton"
android:text="Word vriend" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<ImageButton
android:id="#+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/button8"
android:layout_marginRight="5dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/foldersbutton"
android:text="Folders" />
<ImageButton
android:id="#+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/button8"
android:layout_marginRight="5dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/specialistenbutton"
android:text="Specialisten" />
<ImageButton
android:id="#+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/button8"
android:layout_marginRight="5dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/bwcbutton"
android:text="Brandwond encentrum" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<ImageButton
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/button8"
android:layout_marginRight="5dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/nieuwsbutton"
android:text="Nieuws en Agenda" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="40dp"
android:orientation="horizontal">
<ImageButton
android:id="#+id/button11"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/button8"
android:layout_marginRight="5dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/afspraakbutton"
android:text="Facebook" />
<ImageButton
android:id="#+id/button12"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/button8"
android:layout_marginRight="5dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/afspraakbutton"
android:text="Youtube" />
<ImageButton
android:id="#+id/button13"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/button8"
android:layout_marginRight="5dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/afspraakbutton"
android:text="Twitter" />
<ImageButton
android:id="#+id/button14"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/button8"
android:layout_marginRight="5dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/afspraakbutton"
android:text="LinkedIn" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
You can try just using TextViews in your layout and add the icons to them as compound drawables using:
textView.setCompoundDrawables(null, yourIconDrawable, null, null);
This will put your drawable to the top of the text. To set the size of the drawable call setBounds(...) on it or use setCompoundDrawablesWithIntrinsicBounds(...) on the TextView. You can also set the padding with setCompoundDrawablePadding(int padding).
Can you try this code and check if it works?
<TextView
android:id="#+id/TextView"
android:layout_width="50dp"
android:layout_height="30dp" --> you can change the size that you want
android:layout_below="#+id/imageButton1" --> you can change the size that you want
android:textAlignment="center" />
Try this but this the not The right way to create this type of view the best way to create Using GridView
<LinearLayout
android:weightSum="3"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="113dp">
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="100dp"
android:layout_height="100dp">
<ImageButton
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal"
android:src="#mipmap/ic_launcher"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Title "/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="100dp"
android:layout_height="100dp">
<ImageButton
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal"
android:src="#mipmap/ic_launcher"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Title "/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="100dp"
android:layout_height="100dp">
<ImageButton
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal"
android:src="#mipmap/ic_launcher"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Title "/>
</LinearLayout>
</LinearLayout>
There are a lot of ways to achieve that, one of the most common ways is to use FrameLayout, and another common way is by using RelativeLayout which will give you the ability to predefine the views positions according to eachother(i.e which one is above the other), and following is a good example about how to use RelativeLayout to achieve your purpose:
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- Add Button 1 -->
<Button
android:id="#+id/button1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:src="#drawable/image1" />
<!-- Add Text 1 -->
<TextView
android:id="#+id/text1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_below="#id/button1"
android:text="text1" />
</RelativeLayout>

Android studio , how to align multiple text view and edit texts in linear layout(horizontal)

I'm a beginner in android development, my project requires me to build a simple UI app using two fragments (home and field). The home will show the starup page with 5 buttons. when we click on these buttons, it will then call field fragment and show fields like:
conductivity:----,height:----
and etc to fill up, like inserting manual data.
my issue here is , the project requires me to use linearlayout in horizontal alignment for the field frag, when i do that and add edit text and text views, its lined in same line as horizontal, but what i need is it to appear one and the other below it: SOme thing like
conductivity(text view): ___________(editText)
[another line]
height(text view):________(editText)
and so on. SOrry this might be a simple issue but i have been stuck on this for 2 days now. Help me
my code for field fragment.xml is as follows:
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hp1_textView"
android:id="#+id/hp1_textView"
android:textSize="32sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/hp1_textView"
android:layout_centerHorizontal="true"
android:id="#+id/linearLayout">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/conductivity"
android:id="#+id/textView"/>
<EditText android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_weight="1"
android:hint="#string/conductivity_field" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="pH:"
android:id="#+id/textView2" />
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText2"
android:layout_weight="1"
android:hint="0.-14" />
</LinearLayout>
</RelativeLayout>
THis is how i want it to display
Use this xml in your code as it is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:weightSum="2">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Conductivity (uS): "
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="745.2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:weightSum="2">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="pH: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="7.1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:weightSum="2">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Moisture(%): "
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="0-100" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:weightSum="2">
<TextView
android:id="#+id/textVie4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:text="Dissolved oxygen(ppm): "
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="0-100" />
</LinearLayout>
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="5dp"
android:text="Save Log Entry" />
<Button
android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="5dp"
android:text="Show Log Entry" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:padding="5dp"
android:weightSum="3">
<Button
android:id="#+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:padding="5dp"
android:text="Previouse" />
<Button
android:id="#+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:padding="5dp"
android:text="Next" />
<Button
android:id="#+id/btn5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:padding="5dp"
android:text="Home" />
</LinearLayout>
</RelativeLayout>
I hope its solve your problem.....
Try below with android:weightSum and LinearLayout with android:orientation
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/hp1_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="hp1"
android:textSize="32sp" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="12">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="conductivity"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:hint="conductivity_field" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:orientation="horizontal">
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="pH:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:hint="0.-14" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:orientation="horizontal">
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Moisture(%):"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:hint="0-100" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Disolved Oxygen(ppm)"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:hint="0-100" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="2"
android:orientation="vertical"
android:weightSum="2">
<Button
android:id="#+id/Button11"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_weight="1"
android:gravity="center"
android:text="Save log entry"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/Button12"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_weight="1"
android:gravity="center"
android:hint="Show log entries"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="2"
android:orientation="horizontal"
android:weightSum="3">
<Button
android:id="#+id/Button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:gravity="center"
android:text="conductivity"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/Button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:hint="conductivity_field" />
<Button
android:id="#+id/Button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:hint="conductivity_field" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
If my understanding of your scenario is correct, to display them one below the other as in the image you have supplied, the orientation of the linearLayout must be set to vertical. If you use orientation horizontal the drawing of widgets will falls to the next line, only after it has drawn widgets completely all the way up to the width of your phone( and then it jumped to the next line) .So the LinearLayout should be changed to vertical orientation
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/hp1_textView"
android:layout_centerHorizontal="true"
android:id="#+id/linearLayout">

layout_weight not adjusting size properly

I am trying to make a calculator app. I have a outer linearlayout which is vertical orientation and then have nested linear layouts which have a horizontal orientation where my buttons will go. Only the last nested linear layout is not adjusting the width properly of the buttons as I want the '0' button to take half the width and the '.' and '=' button to take a quarter of the width. I gave the '=' button a layout weight of 0.5 and the '.' & '=' button a layout weight of 0.25 but it still won't work properly.
Here is a screenshot of the graphical layout:
XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#000000"
android:weightSum="1">
<TextView
android:text="0"
android:layout_width="wrap_content"
android:layout_weight = "0.25"
android:layout_height="0dp"
android:textSize="40sp"
android:layout_gravity="right"
android:textColor="#FFFFFF"
android:id="#+id/tvDisplay"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="0.15"
android:weightSum="1">
<Button
android:id="#+id/bClear"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="C"
android:textSize="30sp" />
<Button
android:id="#+id/bChangeSign"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="+/-"
android:textSize="30sp" />
<Button
android:id="#+id/bPercent"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="%"
android:textSize="30sp" />
<Button
android:id="#+id/bDivide"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:background="#FF9900"
android:text="/"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="0.15">
<Button
android:text="7"
android:background="#FFFFFF"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:id="#+id/b7"/>
<Button
android:text="8"
android:background="#FFFFFF"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:id="#+id/b8"/>
<Button
android:text="9"
android:background="#FFFFFF"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:id="#+id/b9"/>
<Button
android:id="#+id/bMultiply"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:background="#FF9900"
android:text="x"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="0.15">
<Button
android:text="4"
android:background="#FFFFFF"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:id="#+id/b4"/>
<Button
android:text="5"
android:background="#FFFFFF"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:id="#+id/b5" />
<Button
android:text="6"
android:background="#FFFFFF"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:id="#+id/b6" />
<Button
android:text="-"
android:background="#FF9900"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:id="#+id/bSubtract"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="0.15">
<Button
android:text="1"
android:background="#FFFFFF"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:id="#+id/b1" />
<Button
android:text="2"
android:background="#FFFFFF"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:id="#+id/b2" />
<Button
android:text="3"
android:background="#FFFFFF"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:id="#+id/b3" />
<Button
android:text="+"
android:background="#FF9900"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:id="#+id/bPlus" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:orientation="horizontal"
<Button
android:id="#+id/b0"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.5"
android:background="#FFFFFF"
android:text="0"
android:textSize="30sp" />
<Button
android:id="#+id/bDecimal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="."
android:textSize="30sp" />
<Button
android:id="#+id/bEquals"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="0.2dp"
android:layout_weight="0.25"
android:background="#FF9900"
android:text="="
android:textSize="30sp" />
</LinearLayout>
The solution to this problem is remarkably simple.
Android is great, and the tools for developing it are great, but then you come across behaviors like this and you realize that it certainly isn't perfect yet.
Go ahead and change the android:layout_width in your 3 buttons at the bottom to
android:layout_width="0dp"
Now, this was the way this was explained to me. The layout_weight attribute tries to balance out the widths to be in the correct ratios, but it takes into account the widths of the buttons it is trying to balance. By setting the width to absolutely nothing (0dp), you're taking away any sort of widths that may have interfered with layout_weigth's attempts to balance the ratios. Basically, so that there are no other values that could throw off the shifting of the widths.
I hope that helps. Good luck :)
test well
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical"
android:weightSum="1" >
<TextView
android:id="#+id/tvDisplay"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="right"
android:layout_weight="0.25"
android:text="0"
android:textColor="#FFFFFF"
android:textSize="40sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:id="#+id/bClear"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="C"
android:textSize="30sp" />
<Button
android:id="#+id/bChangeSign"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="+/-"
android:textSize="30sp" />
<Button
android:id="#+id/bPercent"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="%"
android:textSize="30sp" />
<Button
android:id="#+id/bDivide"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FF9900"
android:text="/"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:id="#+id/b7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="7"
android:textSize="30sp" />
<Button
android:id="#+id/b8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="8"
android:textSize="30sp" />
<Button
android:id="#+id/b9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="9"
android:textSize="30sp" />
<Button
android:id="#+id/bMultiply"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FF9900"
android:text="x"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:id="#+id/b4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="4"
android:textSize="30sp" />
<Button
android:id="#+id/b5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="5"
android:textSize="30sp" />
<Button
android:id="#+id/b6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="6"
android:textSize="30sp" />
<Button
android:id="#+id/bSubtract"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FF9900"
android:text="-"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:id="#+id/b1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="1"
android:textSize="30sp" />
<Button
android:id="#+id/b2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="2"
android:textSize="30sp" />
<Button
android:id="#+id/b3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="3"
android:textSize="30sp" />
<Button
android:id="#+id/bPlus"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FF9900"
android:text="+"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:id="#+id/b0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.5"
android:background="#FFFFFF"
android:text="0"
android:textSize="30sp" />
<Button
android:id="#+id/bDecimal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:text="."
android:textSize="30sp" />
<Button
android:id="#+id/bEquals"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.1dp"
android:layout_weight="0.25"
android:background="#FF9900"
android:text="="
android:textSize="30sp" />
</LinearLayout>

Categories