-3

![I had coded it in linear layout (code given below) but want to know how to do the same using relative layout, or is that just a bad idea given the layout?

linear layout -

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

   <ImageView
    android:src="@drawable/ocean"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:scaleType="centerCrop"
  />

<TextView
    android:text="You're invited!"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white"
    android:textSize="45sp"
    android:paddingTop="16dp"
    android:paddingLeft="16dp"
    android:background="#009688"/>

<TextView
    android:text="Bonfire at the beach"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white"
    android:paddingTop="8dp"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:textSize="24sp"
    android:background="#009688"/>

][1]When using relative layout my ImageView does not appear on the screen:

  <RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
  >

  <ImageView
    android:src="@drawable/ocean"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:scaleType="centerCrop"
    android:id="@+id/ocean"
  />
</RelativeLayout>

4 Answers4

0

android:layout_height="0dp" This is wrong in your ImageView

Change this to

android:layout_height="match_parent"

or

android:layout_height="wrap_content"

Basically you set your height as 0. So nothing to show.

Edit :

Also you can remove this android:layout_weight="1". No need for weight in RelativeLayout

Amsheer
  • 7,046
  • 8
  • 47
  • 81
  • RelativeLayouts do not support weight. You need to use a LinearLayout as a parent container if you want to use weights. – Anil Meenugu Jun 24 '15 at 07:24
  • I think you thinking reverse. No need weight in Relative Layout i accept. Then remove the attribute. Why should we go to the LinearLayout? – Amsheer Jun 24 '15 at 07:26
0

Its because you are using android:layout_height="0dp"

Please use android:layout_height="wrap_content"

Neal Ahluvalia
  • 1,538
  • 1
  • 10
  • 20
0

Change your ImageView's height like this.

<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
  >

  <ImageView
    android:src="@drawable/ocean"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:scaleType="centerCrop"
    android:id="@+id/ocean"
  />
Bullionist
  • 2,070
  • 3
  • 25
  • 42
0

You can only use layout_weight in LinearLayout. Try this.

 <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertizal">

 <ImageView
   android:src="@drawable/ocean"
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:layout_weight="1"
   android:scaleType="centerCrop"
   android:id="@+id/ocean"/>

Noman Rafique
  • 3,735
  • 26
  • 29