I added scrollview to a relative layout, but it scrolls more than I needed. If I remove the scrollview, it will not show the last two buttonImages. So I added srollview.
Here is the code.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_image"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="58dp"
android:src="#drawable/logo" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/imageView1"
android:layout_marginTop="92dp"
android:background="#drawable/button_layout"
android:gravity="left|center_vertical"
android:paddingLeft="25dp"
android:text="Team Bio"
android:textColor="#F8F8FF"
android:textSize="30dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button1"
android:layout_marginTop="15dp"
android:background="#drawable/button_layout"
android:gravity="left|center_vertical"
android:paddingLeft="25dp"
android:text="Contact Us"
android:textColor="#F8F8FF"
android:textSize="30dp" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button2"
android:layout_marginTop="14dp"
android:background="#drawable/button_layout"
android:gravity="left|center_vertical"
android:paddingLeft="25dp"
android:text="Facebook"
android:textColor="#F8F8FF"
android:textSize="30dp" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button3"
android:layout_marginTop="14dp"
android:background="#drawable/button_layout"
android:gravity="left|center_vertical"
android:paddingLeft="25dp"
android:text="Twitter"
android:textColor="#F8F8FF"
android:textSize="30dp" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button4"
android:layout_marginTop="14dp"
android:background="#drawable/button_layout"
android:gravity="left|center_vertical"
android:paddingLeft="25dp"
android:text="Youtube"
android:textColor="#F8F8FF"
android:textSize="30dp" />
</RelativeLayout>
</ScrollView>
I tried changing lots of things, but I can't make it work.
Would using a LinearLayout help? I'm not sure you need a RelativeLayout inside the ScrollView with just those buttons listed there.
Something like this (extra properties, ids, etc. need adding)...
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:background="#drawable/background_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Add buttons here... -->
</LinearLayout>
</ScrollView>
This will scroll if it needs to, and not otherwise.
Related
I want to create an horizontal scroll view for my app
(I have six buttons needed to be distributed equally), everything
works fine when using smaller display (nexus 6 1440x 2560), but when using bigger display (pixel C 2560x1800) I got some space. I remarked
that LinearLayout width cover only the content even though I'm using
match_parent in android:layout_width
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.xieon_2.Home_dashboard">
<LinearLayout
android:id="#+id/main_vertical_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="30dp">
<HorizontalScrollView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal">
<ImageButton
android:id="#+id/bedroom_but"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:background="#drawable/round_shape"
android:src="#drawable/bedroom_icon" />
<ImageButton
android:id="#+id/kitchen_but"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="9dp"
android:background="#drawable/round_shape"
android:src="#drawable/kitchen_icon" />
<ImageButton
android:id="#+id/living_room_but"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="9dp"
android:background="#drawable/round_shape"
android:src="#drawable/living_room" />
<ImageButton
android:id="#+id/kids_room_but"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="9dp"
android:background="#drawable/round_shape"
android:src="#drawable/kids_room_icon" />
<ImageButton
android:id="#+id/bathroom_but"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="9dp"
android:background="#drawable/round_shape"
android:src="#drawable/bathroom_icon" />
<ImageButton
android:id="#+id/Guestroom_but"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="9dp"
android:background="#drawable/round_shape"
android:src="#drawable/guest_room" />
</LinearLayout>
</HorizontalScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="----------------------------------------------------------------"
android:textColor="#color/RoyalBlue"
android:textSize="20sp" />
<Button
android:id="#+id/control_but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:text="Control" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp">
<ImageView
android:id="#+id/temp_icon_2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:src="#drawable/temperature_icon" />
<TextView
android:id="#+id/temp_val"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/temp_icon_2"
android:text="26"
android:textColor="#color/RoyalBlue"
android:textSize="40sp" />
<TextView
android:id="#+id/degree_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/temp_val"
android:text="°"
android:textColor="#color/RoyalBlue"
android:textSize="40sp" />
<TextView
android:id="#+id/pcnt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:text="%"
android:textColor="#color/RoyalBlue"
android:textSize="40sp" />
<TextView
android:id="#+id/hum_val_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#+id/pcnt"
android:text="85"
android:textColor="#color/RoyalBlue"
android:textSize="40sp" />
<ImageView
android:id="#+id/humidity_icon_2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_toStartOf="#+id/hum_val_2"
android:src="#drawable/humidity_icon" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp">
<ImageView
android:id="#+id/noise_icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/noise_unit"
android:src="#drawable/air_quality" />
<TextView
android:id="#+id/noise_val"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/noise_icon"
android:text="15"
android:textColor="#color/RoyalBlue"
android:textSize="40sp" />
<TextView
android:id="#+id/noise_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toEndOf="#+id/noise_val"
android:text="PPM"
android:textColor="#color/RoyalBlue"
android:textSize="28sp" />
<TextView
android:id="#+id/security_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/noise_val"
android:layout_alignParentEnd="true"
android:layout_marginEnd="12dp"
android:text="ON"
android:textColor="#color/RoyalBlue"
android:textSize="35sp" />
<ImageView
android:id="#+id/security_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBottom="#+id/noise_val"
android:layout_toStartOf="#+id/security_state"
android:src="#drawable/home_security"
android:layout_marginTop="10dp"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
app with large display
app with small display
I'm trying to display information contained on a json with this code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MarkerActivity">
<Button
android:id="#+id/button"
style="#android:style/Widget.Material.Button.Small"
android:layout_width="34dp"
android:layout_height="38dp"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/textView"
android:layout_marginEnd="36dp"
android:layout_marginTop="-1dp"
android:text="+" />
<TextView
android:id="#+id/textView2"
android:layout_width="115dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="149dp"
android:text="Status"
android:textSize="18sp"
android:textStyle="bold"
tools:text="Status" />
<TextView
android:id="#+id/mytextview"
android:layout_width="282dp"
android:layout_height="79dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="15dp"
android:layout_marginTop="27dp"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView" />
<ImageView
android:id="#+id/imageView"
android:layout_width="40dp"
android:layout_height="52dp"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/mytextview"
android:layout_marginStart="15dp"
android:src="#drawable/ic_mappin" />
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="9dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:background="?android:attr/listDivider" />
<View
android:id="#+id/divider2"
android:layout_width="match_parent"
android:layout_height="7dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="134dp"
android:background="?android:attr/listDivider" />
<ScrollView>
<TextView
android:id="#+id/textView"
android:layout_width="115dp"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/textView2"
android:layout_below="#+id/divider"
android:text="Comments"
android:textSize="18sp"
android:textStyle="bold"
tools:text="Comments" />
<TextView
android:id="#+id/jsonData1"
android:layout_width="155dp"
android:layout_height="83dp"
android:layout_alignStart="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_gravity="center_horizontal"
android:text="STATS"
android:textAppearance="#android:style/TextAppearance.Material.Small"
android:textSize="16sp" />
<TextView
android:id="#+id/jsonData2"
android:layout_width="157dp"
android:layout_height="117dp"
android:layout_alignStart="#+id/textView2"
android:layout_below="#+id/textView"
android:layout_gravity="center_horizontal"
android:text="COMMENTS"
android:textAppearance="#android:style/TextAppearance.Material.Small"
android:textSize="16sp" />
</ScrollView>
</RelativeLayout>
I need a scrollable view, due the fact that my json file could contain many entries and could fill all the space on the display.
With this code my program crash. Any idea how to solve it? I tried in many ways but always the same situation
ScrollView needs to have one child, so wrap your 3 TextViews in a LinearLayout layout and set it's height to wrap_content
ScrollView=>LinearLayout=>Textviews
ScrollView can have only one child but you put 3 child in scrollView. thats why your app is crash.
You need to put all child (all three textView) in one ViewGroup. then your app will run correctly. here you can see
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MarkerActivity">
<Button
android:id="#+id/button"
style="#android:style/Widget.Material.Button.Small"
android:layout_width="34dp"
android:layout_height="38dp"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/textView"
android:layout_marginEnd="36dp"
android:layout_marginTop="-1dp"
android:text="+" />
<TextView
android:id="#+id/textView2"
android:layout_width="115dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="149dp"
android:text="Status"
android:textSize="18sp"
android:textStyle="bold"
tools:text="Status" />
<TextView
android:id="#+id/mytextview"
android:layout_width="282dp"
android:layout_height="79dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="15dp"
android:layout_marginTop="27dp"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView" />
<ImageView
android:id="#+id/imageView"
android:layout_width="40dp"
android:layout_height="52dp"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/mytextview"
android:layout_marginStart="15dp"
android:src="#drawable/ic_mappin" />
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="9dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:background="?android:attr/listDivider" />
<View
android:id="#+id/divider2"
android:layout_width="match_parent"
android:layout_height="7dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="134dp"
android:background="?android:attr/listDivider" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="115dp"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/textView2"
android:layout_below="#+id/divider"
android:text="Comments"
android:textSize="18sp"
android:textStyle="bold"
tools:text="Comments" />
<TextView
android:id="#+id/jsonData1"
android:layout_width="155dp"
android:layout_height="83dp"
android:layout_alignStart="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_gravity="center_horizontal"
android:text="STATS"
android:textAppearance="#android:style/TextAppearance.Material.Small"
android:textSize="16sp" />
<TextView
android:id="#+id/jsonData2"
android:layout_width="157dp"
android:layout_height="117dp"
android:layout_alignStart="#+id/textView2"
android:layout_below="#+id/textView"
android:layout_gravity="center_horizontal"
android:text="COMMENTS"
android:textSize="16sp"
android:textAppearance="#android:style/TextAppearance.Material.Small"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
I have just created an expandable RecyclerView with different views as every parent item have a child My case is when expanding parent item it view it's child with a vertical spacing between each child Item like this
as this is my main XML File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dedede"
tools:context="abtech.waiteriano.com.waitrer.MainPaymentActivity">
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="19dp"
android:background="#ffffff">
<EditText
android:id="#+id/checkNoET"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imageView7"
android:editable="false"
android:ems="10"
android:gravity="right"
android:text="505050505050"
android:textSize="12dp">
<requestFocus />
</EditText>
<TextView
android:id="#+id/checkNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkNoET"
android:layout_alignBottom="#+id/checkNoET"
android:layout_marginStart="10dp"
android:layout_toEndOf="#+id/imageView7"
android:text="Check#"
android:textColor="#color/colorPrimary"
android:textSize="12dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/checkNoET"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/checkNoET"
app:srcCompat="#drawable/check" />
<EditText
android:id="#+id/editText"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkNoET"
android:layout_alignBottom="#+id/checkNoET"
android:layout_alignParentEnd="true"
android:editable="false"
android:ems="10"
android:gravity="end"
android:text="5"
android:textSize="12dp">
<requestFocus />
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText"
android:layout_alignBottom="#+id/editText"
android:layout_alignStart="#+id/editText"
android:layout_marginStart="13dp"
android:text="Coves"
android:textColor="#color/colorPrimary"
android:textSize="12dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText"
android:layout_toStartOf="#+id/editText"
app:srcCompat="#drawable/covers" />
<EditText
android:id="#+id/tableNo"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_below="#+id/checkNoET"
android:layout_toEndOf="#+id/imageView7"
android:editable="false"
android:ems="10"
android:gravity="end"
android:text="25"
android:textSize="12dp">
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tableNo"
android:layout_alignBottom="#+id/tableNo"
android:layout_alignStart="#+id/checkNo"
android:text="Table"
android:textColor="#color/colorPrimary"
android:textSize="12dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView9"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/checkNoET"
app:srcCompat="#drawable/tablepay" />
<EditText
android:id="#+id/editText14"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tableNo"
android:layout_alignBottom="#+id/tableNo"
android:layout_alignParentEnd="true"
android:editable="false"
android:text="15/10/2017 03:15PM"
android:textColor="#000000"
android:textSize="10dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView10"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText14"
android:layout_toStartOf="#+id/editText14"
app:srcCompat="#drawable/calendar" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/relativeLayout2"
android:layout_marginTop="14dp"
android:background="#ffffff">
<android.support.v7.widget.RecyclerView
android:id="#+id/parentList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_below="#+id/relativeLayout3"
android:layout_marginTop="10dp"
android:background="#ffffff">
<GridView
android:id="#+id/paymentsTypeGV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="80dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"></GridView>
</RelativeLayout>
</RelativeLayout>
my problem is from my xml design and if i is from xml design the problem is in the parent list item ot the child list item or from the main XML File sorry if any thing is not clear enough
Here's a thought: try removing any padding on the text elements.
android:includeFontPadding="false"
If this works, then this answer will become duplicate to:
Android: TextView: Remove spacing and padding on top and bottom
I have a relative layout where i already aligned almost all elements like i want, but there are two elements at the right bottom(textView and the icon of forbiden photo), they are aligned related to the parent, the parent is not the photo.
I want to align those elements to the bottom of the image that i have:
here is a
expected results:
the rectangle is where i want the textView and the camera forbiden icon!
and here is the xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="142dp"
android:layout_gravity="center"
android:layout_margin="#dimen/card_margin"
android:elevation="3dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
card_view:cardCornerRadius="#dimen/card_specie_radius">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.example.afcosta.inesctec.pt.android.Helpers.NexusBoldTextView
android:id="#+id/Avaliation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/plantName"
android:layout_marginStart="92dp"
android:layout_marginTop="15dp"
android:text="Avalie a fotografia" />
<com.example.afcosta.inesctec.pt.android.Helpers.NexusBoldTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/plantPhoto"
android:textColor="#color/base"
android:layout_marginTop="4dp"
android:layout_toStartOf="#+id/cameraForbiden"
android:text="TextView" />
<ImageView
android:id="#+id/reportImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/color_cursor_white" />
<ImageView
android:id="#+id/plantPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp" />
<ImageView
android:id="#+id/userIcon"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentBottom="true"
android:layout_below="#id/plantPhoto"
android:src="#drawable/ic_user"
/>
<com.example.afcosta.inesctec.pt.android.Helpers.NexusBoldTextView
android:id="#+id/plantName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/plantPhoto"
android:textColor="#color/nephritis"
android:textSize="18sp" />
<ImageView
android:id="#+id/starIcon"
android:layout_width="23dp"
android:layout_height="23dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/plantName"
android:src="#drawable/ic_star"
android:layout_marginLeft="10dp"
/>
<ImageView
android:id="#+id/cameraForbiden"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_toRightOf="#+id/plantName"
android:layout_marginLeft="10dp"
android:src="#drawable/ic_no_photos"
android:layout_below="#id/plantPhoto"
/>
<com.example.afcosta.inesctec.pt.android.Helpers.NexusBoldTextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/plantPhoto"
android:textColor="#color/base"
android:layout_marginEnd="29dp"
android:layout_marginTop="8dp"
android:layout_toEndOf="#+id/userIcon"
android:layout_toLeftOf="#+id/userIcon"
android:paddingLeft="10px"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Use following code:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/plantPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="5dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/userIcon"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="#drawable/ic_user"
/>
<com.example.afcosta.inesctec.pt.android.Helpers.NexusBoldTextView
android:id="#+id/plantName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/nephritis"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
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>