I want to made view looking like this.
I have a problem with FAB placement. I want to place it on the center and bottom of RelativeLayout.
You can use FrameLayout as a container like this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:background="#ccc"
android:orientation="vertical">
<!--MARGIN WILL BE HALF OF HEIGHT OF FAB-->
<RelativeLayout
android:id="#+id/relative2"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="bottom"
android:layout_marginBottom="25dp"
android:background="#color/colorPrimary"></RelativeLayout>
<View
android:id="#+id/view1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="bottom|center_horizontal"
android:background="#color/colorAccent" />
</FrameLayout>
</LinearLayout>
Related
I have these nested layouts and i want to put a LinearLayout anchored to the bottom (internally) of the first RelativeLayout but i don't know how to do it. Can someone help me? I want something like this: https://imgur.com/eCCYS8Q
<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="#fff"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.65"
android:background="#drawable/background_gradient">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.35">
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
To make layout_weight work properly set android:layout_height="0dp" for both relative layouts
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.65"
android:background="#color/colorPrimary">
<LinearLayout
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_height="match_parent"
android:background="#color/colorAccent">
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.35"
android:background="#color/colorPrimaryDark">
</RelativeLayout>
</LinearLayout>
change 10dp to whatever suits you.
I set the colors just to distinguish the layouts
If you want to align a view to the bottom of a relative layout you need to use
android:layout_alignParentBottom="true"
in the view that is going to be stuck at the bottom of the relative
Align LinearLayout to the bottom using id.
take the id of relative layout and then align LinearLayout to the bottom at the end of relative layout id.
Since you need weighttoo in your UI so This will work for you
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.65"
android:background="#drawable/background_gradient">
<LinearLayout
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content">
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.35">
</RelativeLayout>
</LinearLayout>
I have created a layout. But at the bottom there is a white space left. I want the content of the relative layout at the extreme bottom of the screen. i.e seekbar and button at the bottom.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/images">
<com.example.acer.myapplication.ZoomView
android:id="#+id/iop"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<SeekBar
android:id="#+id/seekBar7"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button61"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#id/seekBar7"
android:layout_centerInParent="true"
android:background="#drawable/play" />
</RelativeLayout>
</LinearLayout>
I want the white space at the bottom should be replaced with the content of relative layout.
I have uploaded the image
use this 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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/images">
<com.example.acer.myapplication.ZoomView
android:id="#+id/iop"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<SeekBar
android:id="#+id/seekBar7"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button61"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#id/seekBar7"
android:layout_centerInParent="true"
android:background="#drawable/play" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
instead of wrap_content You should put match_parent.
That would help it out. I hope you know about wrap_content and match_parent.If you don't look at this
https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
I am creating a user profile screen in Android. In this screen have user profile wallpaper and user profile image. Here the grey background is profile wallpaper and red background is going to profile image. I need to set the red bg layout to the horizontal and vertical center of the grey bg layout..
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rlActivityNotificationsAndProfile">
<include layout="#layout/layout_tool_bar"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/profile_wallpaper_container_height"
android:id="#+id/rlProfileWallpaper"
android:layout_below="#+id/toolbar">
<LinearLayout
android:layout_width="match_parent"
android:background="#646464"
android:orientation="vertical"
android:layout_height="#dimen/profile_wallpaper_height">
<LinearLayout
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="#A52525">
</LinearLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:id="#+id/fabEditProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="#dimen/base_ten_dp"
android:layout_marginEnd="#dimen/base_ten_dp"
android:layout_marginStart="#dimen/base_ten_dp"
android:scaleType="center"
android:src="#drawable/add"
app:elevation="#dimen/priority_1_elevation"
app:fabSize="1"/>
</RelativeLayout>
</LinearLayout>
You can just set your parent LinearLayout's gravity to center (just add 1 line):
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rlActivityNotificationsAndProfile">
<include layout="#layout/layout_tool_bar"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/profile_wallpaper_container_height"
android:id="#+id/rlProfileWallpaper"
android:layout_below="#+id/toolbar">
<LinearLayout
android:layout_width="match_parent"
android:background="#646464"
android:orientation="vertical"
android:layout_height="#dimen/profile_wallpaper_height"
android:gravity="center">
<LinearLayout
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="#A52525">
</LinearLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:id="#+id/fabEditProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="#dimen/base_ten_dp"
android:layout_marginEnd="#dimen/base_ten_dp"
android:layout_marginStart="#dimen/base_ten_dp"
android:scaleType="center"
android:src="#drawable/add"
app:elevation="#dimen/priority_1_elevation"
app:fabSize="1"/>
</RelativeLayout>
</LinearLayout>
I want to achieve my layout with image-view and relative layout as like as picture shows here.
I tried with this code but not works.
<RelativeLayout
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textview_ratting"
android:layout_margin="5dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/imageView"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:src="#drawable/icon"/>
</RelativeLayout>
This worked for me...Provide negative paddingTop equal to half of the size of the imageview .
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="70dp"
android:background="#color/colorAccent" />
<ImageView
android:id="#+id/widget_title_icon"
android:paddingTop="-70dp"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_gravity="top|center_horizontal"
android:adjustViewBounds="true"
android:contentDescription="#string/action_settings"
android:scaleType="fitStart"
android:src="#mipmap/ic_launcher" />
</FrameLayout>
We normally use FrameLayout for this case:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="192dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:background="#color/colorAccent"/>
<ImageView
android:id="#+id/widget_title_icon"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="top|center_horizontal"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:src="#mipmap/ic_launcher"/>
</FrameLayout>
We can also play with android:paddingTop="-10dp" as you like.
See FrameLayout definition:
Child views are drawn in a stack, with the most recently added child
on top.
Hope this will help....
android:layout_marginTop="-35dp" -35dp is the half of your image size (70dp/2) but margin in the reverse direction so use "-" sign.
<RelativeLayout
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textview_ratting"
android:layout_margin="5dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/imageView"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="-35dp"
android:src="#drawable/icon"/>
</RelativeLayout>
used FrameLayout, using frameLayout you can do it.
For this, you should use FrameLayout. FrameLayout provides a layered layout to overlap items in UI. Example:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center|center_horizontal"
android:background="#color/colorAccent" />
<ImageView
android:id="#+id/widget_title_icon"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_marginBottom="100dp"
android:layout_gravity="center|center_horizontal"
android:src="#mipmap/ic_launcher" />
</FrameLayout>
Arpan has already proposed a similar answer. He is using padding and margin of both elements, whereas this one uses layout gravity and relative margin of ImageView. Doing this will always place the contents in the center of the screen on all devices.
ImageView(android:layout_marginBottom) = 0.5 * RelativeLayout(android:layout_height)
Please use circleimageview and relativelayout as a child of framelayout.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="0dp"
android:paddingBottom="0dp"
android:paddingTop="0dp">
</RelativeLayout>
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cir_img_ptv"
android:layout_width="#dimen/cir_img_diameter_ptv"
android:layout_height="#dimen/cir_img_diameter_ptv"
android:layout_gravity="center|top"
android:layout_marginTop="#dimen/cir_img_top_margin_ptv"
android:scaleType="centerCrop"
android:src="#drawable/profile_user"
app:civ_border_color="#color/white"
app:civ_border_width="#dimen/cir_img_stroke_ptv"
app:civ_fill_color="#color/white"
app:civ_border_overlay="true"/>
</FrameLayout>
Try this code,
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relCustomerEditProfile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_marginTop="100dp">
<LinearLayout
android:id="#+id/linRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="50dp"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/imgBackground"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:layout_alignBottom="#+id/relProfile"
android:layout_centerHorizontal="true"
android:background="#drawable/bg_black"></RelativeLayout>
<RelativeLayout
android:id="#+id/relProfile"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_centerHorizontal="true">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/imgViewProfilePhoto"
android:layout_width="90dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:background="#drawable/red"></de.hdodenhof.circleimageview.CircleImageView>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Hereby,I am attaching screenshot,
Hope, it helps you!!
Im trying to create the following Layout:
The border of the RelativeLayout is realized with a rectangle shape as background, but I cannot get the ImageView overlap the RelativeLayout with half of it in and half of it out of the border. Is this even possible with RelativeLayout (And if not: What alternative do I have)?
layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/imageresource"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<RelativeLayout
android:padding="#dimen/item_inner_margin"
android:background="#drawable/item_border"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/relativeLayout"
android:layout_below="#+id/imageView">
<!-- Stuff in the layout -->
</RelativeLayout>
</RelativeLayout>
Thanks in advance
danijoo
This might be what you're looking for:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="70dp"
android:background="#fff"
android:elevation="4dp"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical"
android:paddingTop="30dp">
<!-- add content here -->
</LinearLayout>
</RelativeLayout>
<ImageView
android:id="#+id/imageview1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:background="#android:color/holo_blue_bright"
android:elevation="4dp"
/>
</RelativeLayout>