Coder
Coder

Reputation: 3130

Android Imageview on top of LinearLayout

I want to display an image on top of linearlayout enter image description here

I have tried wrapping it in Framelayout and Relative layout but it doesn't seem to work.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">


    <RelativeLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/cricle" />

    </RelativeLayout>           


    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:padding="20dp"
        android:background="@color/blue"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:gravity="center"/>



    </LinearLayout>

</FrameLayout>

Upvotes: 1

Views: 3758

Answers (3)

Rajesh Panchal
Rajesh Panchal

Reputation: 1170

make your relative layout width match_parent and set margin top for linear layout

<?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:orientation="vertical">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:background="@color/blue"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textStyle="bold"/>


</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:src="@drawable/cricle"/>

</RelativeLayout>

Upvotes: 0

yong.k
yong.k

Reputation: 678

Try to use match_parent and layout_marginTop

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#aaaaaa"
        android:orientation="vertical"
        android:padding="20dp"
        android:layout_marginTop="70dp" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:gravity="center"
            />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/circle" />

    </RelativeLayout> 

</FrameLayout>

Upvotes: 0

Francesc
Francesc

Reputation: 29260

You can use ConstraintLayout for this. You'll have to add a constraint to the LinearLayout for its top, depending on what else you have in there.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="#ffffff80"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </LinearLayout>

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/your_drawable"
        app:layout_constraintBottom_toTopOf="@id/linear_layout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/linear_layout" />

</android.support.constraint.ConstraintLayout>

Upvotes: 2

Related Questions