Search code examples
androidxmlandroid-layoutandroid-relativelayoutandroid-framelayout

How to create this View(layout)


Actually i am working on a layout in which i have to put CardView on a ImageView and it only covers the half portion of a ImageView and also add one TextView(Check Attach Image).

Note:

  1. I tried using FrameLayout and also with Relative Layout... and i achieved it but i don't think its a correct way because i hardcoded mostly paramters.

enter image description here

xml code :

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    <ImageView
    android:layout_width="match_parent"
    android:layout_height="170dp"
    android:layout_gravity="center"
    android:background="@drawable/background"
    android:id="@+id/picture">
</ImageView>

    <TextView
        android:text="TEXT VIEW"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="149dp"
        android:layout_marginStart="149dp"
        android:layout_marginTop="24dp"
        android:id="@+id/textView" />
    </FrameLayout>
</LinearLayout>

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="120dp"
    app:cardBackgroundColor="@color/textViewBackground"
    card_view:cardUseCompatPadding="true"
    android:layout_centerHorizontal="true"
    card_view:cardElevation="15sp"
    android:layout_marginTop="153dp" />

Solution

  • try this :

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@android:color/white"
        android:layout_alignParentTop="true"/>
    
    <TextView
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:layout_margin="20dp"
        android:layout_centerHorizontal="true"
        android:background="@color/colorAccent"/>
    
    <android.support.v7.widget.CardView 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="200dp"
        android:layout_marginTop="100dp"
        card_view:cardCornerRadius="8dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        card_view:cardBackgroundColor="@color/colorPrimary">
    
    
    </android.support.v7.widget.CardView>
    </RelativeLayout>
    

    if you don't want to hardcode parameters then refer this link to get device width height at runtime

    Get screen dimensions in pixels

    and then you can use LayoutParams to set margins dynamically.