I read this: How to align linearlayout to vertical center?
But it didn't help me
Basically I am trying to center 2 ImageButtons in the middle of my screen. The two buttons are horizontally orientated in a linearLayout.
I tried to use a relativeLayout with vertical orientation to center the two imagebuttons in the center in the vertical direction but that doesnt seem to do the job.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="#+id/activity_main"
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"
android:background="#color/white"
tools:context="com.example.max.testcase.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loadSomething"
android:padding="5sp"
android:layout_marginTop="5sp"
android:layout_marginBottom="5sp"
android:textAlignment="center"
android:text="Click here to load the site"
android:textColor="#color/white"
android:textSize="16sp"
android:background="#color/blue"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<ImageButton
android:id="#+id/icon1"
android:onClick="method1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:padding="20dp"
android:paddingTop="50dp"
android:adjustViewBounds="true"
android:layout_gravity="left"
android:gravity="center_horizontal"
android:scaleType = "fitCenter"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:background="#color/white"
android:src="#drawable/icon1" />
<ImageButton
android:id="#+id/icon2"
android:onClick="method2"
android:layout_width="0dp"
android:layout_weight= "1"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:padding="20dp"
android:paddingTop="50dp"
android:layout_gravity="right"
android:gravity="center_horizontal"
android:adjustViewBounds="true"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:scaleType="fitCenter"
android:background="#color/white"
android:src="#drawable/icon2"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
The two buttons keep sticking to the top button bar
Put
android:layout_height="match_parent"
to the RelativeLayout,
and replace android:layout_gravity="center" with
android:layout_centerInParent="true"
in its child LinearLayout
Make it like -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="#+id/activity_main"
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"
android:background="#color/white"
tools:context="com.example.max.testcase.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loadSomething"
android:padding="5sp"
android:layout_marginTop="5sp"
android:layout_marginBottom="5sp"
android:textAlignment="center"
android:text="Click here to load the site"
android:textColor="#color/white"
android:textSize="16sp"
android:background="#color/blue"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true">
<ImageButton
android:id="#+id/icon1"
android:onClick="method1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:padding="20dp"
android:paddingTop="50dp"
android:adjustViewBounds="true"
android:layout_gravity="left"
android:gravity="center_horizontal"
android:scaleType = "fitCenter"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:background="#color/white"
android:src="#drawable/icon1" />
<ImageButton
android:id="#+id/icon2"
android:onClick="method2"
android:layout_width="0dp"
android:layout_weight= "1"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:padding="20dp"
android:paddingTop="50dp"
android:layout_gravity="right"
android:gravity="center_horizontal"
android:adjustViewBounds="true"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:scaleType="fitCenter"
android:background="#color/white"
android:src="#drawable/icon2"
/>
</LinearLayout>
</RelativeLayout> </LinearLayout>
your question seems bit unclear but hope this tip will help you
android:layout_gravity
android:layout_gravity is used to set the position of an element in its parent (e.g. a child View inside a Layout).
Supported by LinearLayout and FrameLayout
android:gravity
android:gravity is used to set the position of content inside an element (e.g. a text inside a TextView).
to get and idea copy this XML and understand how it works
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_gravity="left"
android:gravity="center_vertical">
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/first"
android:background="#color/colorPrimary"
android:gravity="left"/>
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/second"
android:background="#color/colorPrimary"
android:gravity="center"/>
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/third"
android:background="#color/colorPrimary"
android:gravity="right"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center_vertical">
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/first"
android:background="#color/colorAccent"
android:gravity="left"/>
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/second"
android:background="#color/colorAccent"
android:gravity="center"/>
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/third"
android:background="#color/colorAccent"
android:gravity="right"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_gravity="right"
android:gravity="center_vertical">
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/first"
android:background="#color/colorPrimaryDark"
android:gravity="left"/>
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/second"
android:background="#color/colorPrimaryDark"
android:gravity="center"/>
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/third"
android:background="#color/colorPrimaryDark"
android:gravity="right"/>
</LinearLayout>
</LinearLayout>
Make sure all parent layouts of Button's have set android:layout_height="match_parent"
Then apply android:layout_centerVertical="true" to the RelativeLayout.
Try this way it will work
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="#+id/activity_main"
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"
android:background="#android:color/white"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loadSomething"
android:padding="5sp"
android:layout_marginTop="5sp"
android:layout_marginBottom="5sp"
android:textAlignment="center"
android:text="Click here to load the site"
android:textColor="#ffffff"
android:textSize="16sp"
android:background="#ff00"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerVertical="true"
android:layout_gravity="center">
<ImageButton
android:id="#+id/icon1"
android:onClick="method1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:padding="20dp"
android:paddingTop="50dp"
android:adjustViewBounds="true"
android:layout_gravity="left"
android:gravity="center_horizontal"
android:scaleType = "fitCenter"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:background="#ffffff"
android:src="#mipmap/ic_launcher" />
<ImageButton
android:id="#+id/icon2"
android:onClick="method2"
android:layout_width="0dp"
android:layout_weight= "1"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:padding="20dp"
android:paddingTop="50dp"
android:layout_gravity="right"
android:gravity="center_horizontal"
android:adjustViewBounds="true"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:scaleType="fitCenter"
android:background="#ffffff"
android:src="#mipmap/ic_launcher"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
Change your layout like this
<?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:orientation="vertical"
android:id="#+id/activity_main"
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"
android:background="#color/white"
tools:context="com.example.max.testcase.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loadSomething"
android:padding="5sp"
android:layout_marginTop="5sp"
android:layout_marginBottom="5sp"
android:textAlignment="center"
android:text="Click here to load the site"
android:textColor="#color/white"
android:textSize="16sp"
android:background="#color/blue"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_alignParentStart="true">
<ImageButton
android:id="#+id/icon1"
android:onClick="method1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:padding="20dp"
android:paddingTop="50dp"
android:adjustViewBounds="true"
android:layout_gravity="left"
android:gravity="center_horizontal"
android:scaleType = "fitCenter"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:background="#color/white"
android:src="#drawable/icon1" />
<ImageButton
android:id="#+id/icon2"
android:onClick="method2"
android:layout_width="0dp"
android:layout_weight= "1"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:padding="20dp"
android:paddingTop="50dp"
android:layout_gravity="right"
android:gravity="center_horizontal"
android:adjustViewBounds="true"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:scaleType="fitCenter"
android:background="#color/white"
android:src="#drawable/icon2"
/>
</LinearLayout>
</RelativeLayout>
add layout_gravity and gravity attribute in root linear layout with value center vertical it will make all his child align in center vertical.
Note: make child height wrap_content, to achieve this. if your child height is match_parent than this will not work.
A simple layout design for center two image button
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loadSomething"
android:layout_margin="20dp"
android:textAlignment="center"
android:text="Click here to load the site"
android:textColor="#color/WhiteSmoke"
android:textSize="16sp"
android:background="#color/Blue"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<ImageButton
android:id="#+id/icon1"
android:onClick="method1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:padding="20dp"
android:adjustViewBounds="true"
android:scaleType = "fitCenter"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:background="#color/WhiteSmoke"
android:src="#mipmap/minus_icon" />
<ImageButton
android:id="#+id/icon2"
android:onClick="method2"
android:layout_width="0dp"
android:layout_weight= "1"
android:layout_height="wrap_content"
android:padding="20dp"
android:adjustViewBounds="true"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:scaleType="fitCenter"
android:background="#color/WhiteSmoke"
android:src="#mipmap/plus_icon"
/>
</LinearLayout>
</RelativeLayout>
Related
I'm trying to put the image above of my LinearLayout but I can not add the attribute to do that, now my layout looks like :
My goal is to get this :
The goal is to put the image a little bit centered, but trying avoiding margins and dirty code...
Here's my layout.xml
<FrameLayout 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:padding="#dimen/activity_horizontal_margin">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<pl.droidsonroids.gif.GifImageView
android:id="#+id/gifImageView"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_width="#dimen/_270sdp"
android:layout_height="#dimen/_240sdp" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<TextView
android:id="#+id/section_label"
style="#style/TextAppearance.AppCompat.Headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
tools:text="Page One" />
<TextView
style="#style/TextAppearance.AppCompat.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:id="#+id/tv_description"
android:alpha="0.7"
android:gravity="center"
android:textColor="#android:color/white" />
</LinearLayout>
</FrameLayout>
Also I'd like to wrap the image adding the wrap_content but if I do this, the image puts on the top and small
if you wont change your main layout as relative layout then try out below code.
<FrameLayout 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:padding="#dimen/activity_horizontal_margin">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/ll">
<pl.droidsonroids.gif.GifImageView
android:id="#+id/gifImageView"
android:layout_width="#dimen/_270sdp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
</FrameLayout>
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:gravity="center"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<TextView
android:id="#+id/section_label"
style="#style/TextAppearance.AppCompat.Headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
tools:text="Page One" />
<TextView
android:id="#+id/tv_description"
style="#style/TextAppearance.AppCompat.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:alpha="0.7"
android:gravity="center"
android:textColor="#android:color/white" />
</LinearLayout>
</RelativeLayout>
Otherwise try out 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:padding="#dimen/activity_horizontal_margin">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/ll">
<pl.droidsonroids.gif.GifImageView
android:id="#+id/gifImageView"
android:layout_width="#dimen/_270sdp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
</FrameLayout>
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:gravity="center"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<TextView
android:id="#+id/section_label"
style="#style/TextAppearance.AppCompat.Headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
tools:text="Page One" />
<TextView
android:id="#+id/tv_description"
style="#style/TextAppearance.AppCompat.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:alpha="0.7"
android:gravity="center"
android:textColor="#android:color/white" />
</LinearLayout></RelativeLayout>
I'm facing problem to put a linearlayout under the listview like what is shown in this image link:
I want the layout with the Add icon and text:"Add New Members" to be displayed under the listview. What am I doing wrong? Pls help me to solve the problem. Thank you very much.
This is the xml file for the layout
<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:orientation="vertical"
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.thomasbrown.myapplication.MemberActivity"
android:background="#android:color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/groupID"
android:textSize="20sp"
android:layout_marginBottom="15dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ListView
android:id="#+id/ListView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#android:color/white"
android:layout_weight="1" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
app:srcCompat="#drawable/add"
android:id="#+id/imageView2"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/addMember"
android:text="Add New Members"
android:textSize="18sp"
android:textColor="#android:color/holo_red_dark"
android:layout_weight="1"
android:layout_gravity="center_vertical" />
</LinearLayout>
</LinearLayout>
<Button
android:text="Leave this group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/leaveGroupButton"
android:background="#color/colorPrimary"
android:layout_gravity="bottom|end" />
</LinearLayout>
Problem is you have android:layout_weight="1" on your ListView which will make it expand to full and give no space to your LinearLayout
If you want to make "Add New Members" button scrollable and relative to ListView height, use NestedScrollView as a container of ListView and "Add New Members" custom button LinearLayout.
Here is full XML code:
<?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:orientation="vertical"
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"
android:background="#android:color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/groupID"
android:textSize="20sp"
android:layout_marginBottom="15dp" />
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/ListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white" />
<LinearLayout
android:id="#+id/layout_add_new_member"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
app:srcCompat="#drawable/add"
android:id="#+id/imageView2"
android:layout_marginLeft="16dp"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:id="#+id/addMember"
android:text="Add New Members"
android:textSize="18sp"
android:textColor="#android:color/holo_red_dark"
android:layout_weight="1"
android:layout_gravity="center_vertical" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<Button
android:text="Leave this group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/leaveGroupButton"
android:background="#color/colorPrimary"
android:layout_gravity="bottom|end" />
</LinearLayout>
OUTPUT:
If you want to make "Add New Members" button fixed at bottom, use RelativeLayout as a container of ListView and "Add New Members" custom button LinearLayout.
Here is full XML code:
<?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:orientation="vertical"
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"
android:background="#android:color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/groupID"
android:textSize="20sp"
android:layout_marginBottom="15dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:id="#+id/layout_add_new_member"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<ImageView
app:srcCompat="#drawable/add"
android:id="#+id/imageView2"
android:layout_marginLeft="16dp"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:id="#+id/addMember"
android:text="Add New Members"
android:textSize="18sp"
android:textColor="#android:color/holo_red_dark"
android:layout_weight="1"
android:layout_gravity="center_vertical" />
</LinearLayout>
<ListView
android:id="#+id/ListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#id/layout_add_new_member"
android:background="#android:color/white" />
</RelativeLayout>
<Button
android:text="Leave this group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/leaveGroupButton"
android:background="#color/colorPrimary"
android:layout_gravity="bottom|end" />
</LinearLayout>
OUTPUT:
Hope this will help~
<ScrollView ..
<RelativeLayout..
<LinearLayout // vertical orientation
<ListView ../>
<LinearLayout // your linear layout
layout_below="#+id/listviewid"
</LinearLayout>
</RelativeLayout>
</ScrollView>
Try this view hierarchy.
Try RelativeLayout and use android:layout_above as shown below:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/your_listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/your_layout_with_buttons" />
<LinearLayout
android:layout_alignParentBottom="true"
android:id="#+id/your_layout_with_buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
//your buttons come here
</LinearLayout>
</RelativeLayout>
You have to make changes in you list view item for this, check this.
Add below lines into Adapter get view method:
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = LayoutInflater.from(TestListActivity.this).inflate(R.layout.dummy, viewGroup,false);
LinearLayout layout = (LinearLayout)view.findViewById(R.id.add_bottom_ll);
if(i==getCount()-1){
layout.setVisibility(View.VISIBLE);
}else{
layout.setVisibility(View.GONE);
}
return view;
}
List view Item view xml
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp">
<ImageView
android:id="#+id/profile"
android:layout_width="72dp"
android:layout_height="72dp"
android:src="#drawable/arrow" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/profile"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/profile"
android:gravity="center_vertical"
android:text="user Name"
android:textSize="18sp" />
</RelativeLayout>
<LinearLayout
android:id="#+id/add_bottom_ll"
android:layout_width="363dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView2"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:src="#drawable/index" />
<TextView
android:id="#+id/addMember"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="Add New Members"
android:textColor="#android:color/holo_red_dark"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
View(Activity or Fragment) xml:
<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:orientation="vertical"
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.thomasbrown.myapplication.MemberActivity"
android:background="#android:color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/groupID"
android:textSize="20sp"
android:layout_marginBottom="15dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ListView
android:id="#+id/ListView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#android:color/white"
/>
</LinearLayout>
<Button
android:text="Leave this group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/leaveGroupButton"
android:background="#color/colorPrimary"
android:layout_gravity="bottom|end" />
</LinearLayout>
I haven't tried but you try this and let me know:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/groupID"
android:textSize="20sp"
android:layout_marginBottom="15dp" />
<Button
android:text="Leave this group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/leaveGroupButton"
android:background="#color/colorPrimary"
android:layout_alignParentBottom="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/leaveGroupButton"
android:layout_below="#id/groupID">
<ListView
android:id="#+id/ListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#android:color/white" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/ListView1">
<ImageView
app:srcCompat="#drawable/add"
android:id="#+id/imageView2"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/addMember"
android:text="Add New Members"
android:textSize="18sp"
android:textColor="#android:color/holo_red_dark"
android:layout_weight="1"
android:layout_gravity="center_vertical" />
</LinearLayout>
</RelativeLayout>
I want to display some text in the middle of the screen, below header and above footer. Since this text is very long I nested it in ScrollView. I have tried a number of solutions: this, this, this, this, and a bunch more...
At first I had a problem of the two relative_layouts overlapping and text being cut of. The most accepted answer is to use layout_above and layout_below, but when I use layout_above, the text is never displayed.
This is what my xml looks like:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/linear_layout_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/gray"
android:baselineAligned="false"
android:orientation="horizontal"
android:padding="5dp"
android:weightSum="3">
<ImageView
android:id="#+id/profile_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="10dp"
android:contentDescription="#string/profile_photo"
android:src="#drawable/default_profile"
tools:ignore="RtlHardcoded" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/scrollViewAndStuff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_above="#+id/ln_layout_footer"
android:layout_below="#+id/linear_layout_header">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
tools:ignore="UselessParent">
<TextView
android:id="#+id/meetupDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="#string/text"/>-
</ScrollView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/ln_layout_footer"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="horizontal"
android:paddingTop="10dp">
<LinearLayout
android:id="#+id/ln_layout"
android:layout_width="fill_parent"
android:background="#color/gray"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<TextView
android:id="#+id/red"
android:textColor="#color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="#string/red" />
<TextView
android:id="#+id/blue"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#color/white"
android:layout_weight="1"
android:gravity="center"
android:text="#string/blue" />
<TextView
android:id="#+id/green"
android:layout_width="match_parent"
android:textColor="#color/white"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="#string/green" />
</LinearLayout>
</RelativeLayout>
in your ln_layout_footer
set android:layout_height="wrap_content"
Edited
<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:orientation="vertical">
<RelativeLayout
android:id="#+id/linear_layout_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/dark_gray_pressed"
android:baselineAligned="false"
android:orientation="horizontal"
android:padding="5dp"
android:weightSum="3">
<ImageView
android:id="#+id/profile_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="10dp"
android:contentDescription="#string/action_settings"
android:src="#drawable/img_splash_logo"
tools:ignore="RtlHardcoded" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/scrollViewAndStuff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/ln_layout_footer"
android:layout_below="#+id/linear_layout_header">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
tools:ignore="UselessParent">
<TextView
android:id="#+id/meetupDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="#string/txt_price" />-
</ScrollView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/ln_layout_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:orientation="horizontal"
android:paddingTop="10dp">
<LinearLayout
android:id="#+id/ln_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/dark_gray"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<TextView
android:id="#+id/red"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="#string/txt_quantity"
android:textColor="#color/white" />
<TextView
android:id="#+id/blue"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="#string/txt_material"
android:textColor="#color/white" />
<TextView
android:id="#+id/green"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="#string/msg_enter_user_name"
android:textColor="#color/white" />
</LinearLayout>
</RelativeLayout>
In you layout using two properties that why it doesn't work. So you can remove one properties here is your working code
<RelativeLayout
android:id="#+id/scrollViewAndStuff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linear_layout_header">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp">
<TextView
android:textColor="#000"
android:id="#+id/meetupDescription"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:text="#string/text" />-
</ScrollView>
</RelativeLayout>
you can use this android:layout_below="#+id/linear_layout_header or this android:layout_above="#+id/ln_layout_footer, one property at a time. or simply copy and past
I have a RelativeLayout with a LinearLayout inside it which should stick to the bottom of the screen. I have added the android:layout_alignParentBottom="true" property to it, and this works fine in the emulator, but on a real device this LinearLayout falls partially outside of the screen (I can only see the ImageViews but not the TextViews).
Screenshot from emulator
Screenshot from device
This is the full XML layout 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: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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.test.app.MainActivity"
>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
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/cityTextView"
android:fontFamily="sans-serif-medium"
android:textSize="25sp"
android:textColor="#37404d"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/summaryTextView"
android:fontFamily="sans-serif-light"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/detailsView"
android:fontFamily="sans-serif-light"
android:textSize="60sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not bad"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"/>
<ImageView
android:id="#+id/mainImageView"
android:layout_width="230dp"
android:layout_height="230dp"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="#+id/mainOneImageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:src="#drawable/notbad"/>
<TextView
android:id="#+id/mainOneView"
android:fontFamily="sans-serif-light"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not bad" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/mainTwoImageView"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:src="#drawable/notbad"/>
<TextView
android:id="#+id/mainTwoView"
android:fontFamily="sans-serif-light"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not bad" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/mainThreeImageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:src="#drawable/notbad"/>
<TextView
android:id="#+id/mainThreeView"
android:fontFamily="sans-serif-light"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not bad" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
That is because of the scrolling behaviour, in that case, the last items of whatever will be scrolling are out of the screen actually. I guess what you could do is add a marginBottom to the root linearlayout?
Try this one
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android: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"
>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/layout2"
android:orientation="vertical">
<TextView
android:id="#+id/cityTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:textColor="#37404d"
android:textSize="25sp"/>
<TextView
android:id="#+id/summaryTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:textSize="18sp"/>
<TextView
android:id="#+id/detailsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:fontFamily="sans-serif-light"
android:text="Not bad"
android:textSize="60sp"/>
<ImageView
android:id="#+id/mainImageView"
android:layout_width="230dp"
android:layout_height="230dp"
android:layout_gravity="center"
android:src="#drawable/notbad"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/mainOneImageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:src="#drawable/notbad"/>
<TextView
android:id="#+id/mainOneView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:text="Not bad"
android:textSize="30dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/mainTwoImageView"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:src="#drawable/notbad"/>
<TextView
android:id="#+id/mainTwoView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:text="Not bad"
android:textSize="30sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/mainThreeImageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:src="#drawable/notbad"/>
<TextView
android:id="#+id/mainThreeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:text="Not bad"
android:textSize="30dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I am trying to center the text in my TextView and Button. I currently have the following 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="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_weight="0.15"
android:layout_height="0dp"
android:gravity="center"
android:text="#string/CouponFinder"
android:textSize="30sp"
android:textColor="#FF0000"
android:typeface="monospace" />
<ImageView
android:id="#+id/couponImage"
android:layout_width="match_parent"
android:layout_weight="0.7"
android:layout_height="0dp"
android:src="#drawable/coupon" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_weight="0.15"
android:layout_height="0dp"
android:text="#string/continueButton"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
</RelativeLayout>
However, the text in the Button and TextView are to the right and hence some of the text is off the screen... I am also getting the following warning for my LinearLayout:
This LinearLayout layout or its RelativeLayout parent is possibly useless
Any help would be greatly appreciated!!
Try this
[<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="0.15"
android:gravity="center"
android:hint="asdasasdasdd"
android:text="adasdasdasd"
android:textAlignment="gravity"
android:textColor="#FFffff"
android:textSize="30sp"
android:typeface="monospace" />]
Try this,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/couponImage"
android:layout_width="wrap_content"
android:layout_weight="0.7"
android:layout_height="wrap_content"
android:src="#drawable/coupon" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:gravity="center"
android:text="continueButton"
android:textSize="30sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:text="CouponFinder"
android:textColor="#FF0000"
android:textSize="30sp"
android:typeface="monospace" />
</RelativeLayout>
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"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#000000" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_weight="0.15"
android:layout_height="0dp"
android:gravity="center"
android:text="CouponFinder"
android:textSize="30sp"
android:textColor="#FF0000"
android:typeface="monospace" />
<ImageView
android:id="#+id/couponImage"
android:layout_width="match_parent"
android:layout_weight="0.7"
android:layout_height="0dp"
android:src="#drawable/icon" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_weight="0.15"
android:layout_height="0dp"
android:text="continueButton"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
You should make the RelativeLayout a LinearLayout, and get rid of the internal LinearLayout, unless you plan to make modifications and add more things inside the RelativeLayout and outside of the LinearLayout.
Try setting the layout-gravity on the items instead of gravity.
Hope this helps :)
Try this way:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:gravity="center"
android:text="CouponFinder"
android:textColor="#FF0000"
android:textSize="30sp"
android:typeface="monospace" />
<ImageView
android:id="#+id/couponImage"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:src="#drawable/icon" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:gravity="center"
android:text="continueButton"
android:textSize="30sp" />
</LinearLayout>
The warning is because you are using only one LinearLayout as the Child View for RelativeLayout.So remove your RelativeLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_weight="0.15"
android:layout_height="0dp"
android:gravity="center"
android:text="#string/CouponFinder"
android:textSize="30sp"
android:textColor="#FF0000"
android:typeface="monospace" />
<ImageView
android:id="#+id/couponImage"
android:layout_width="match_parent"
android:layout_weight="0.7"
android:layout_height="0dp"
android:text="#string/CouponFinder" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_weight="0.15"
android:layout_height="0dp"
android:text="#string/continueButton"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
Try the above data.I got this by the above data
What you've done is correct, but to see the changes, you just need to restart your IDE. I found the answer here.