Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i fit RelativeLayout to screen?

I am facing with problem button shows outside the screen in RelativeLayout. ImageView doesn't o scale picture to give a button visible place.

What I have:

enter image description here

What I want:

enter image description here

Code:

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

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str"
        android:textAppearance="@android:style/TextAppearance.Small"
        android:textColor="?android:attr/textColorTertiary"
        android:layout_centerHorizontal="true"
        />

<ru.mw.widget.DrawableImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:adjustViewBounds="true"
        android:layout_centerHorizontal="true"
        android:scaleType="centerCrop"
        />

<Button
        android:id="@+id/processButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        />

Problem when I change screen orientation: If I use Arun C Thomas's method in landscape mode everything is ok, but in portrait mode I have this (image cropped by left/right edges):

enter image description here What is expected:

enter image description here

like image 745
ADK Avatar asked Oct 05 '22 09:10

ADK


3 Answers

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

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str"
        android:textColor="?android:attr/textColorTertiary"
        android:textAppearance="@android:style/TextAppearance.Small"

        android:layout_centerHorizontal="true"
        />

<ru.mw.widget.DrawableImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:layout_above="@+id/processButton"
     />

<Button
        android:id="@+id/processButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        />
</RelativeLayout>

This is the solution

Now to achieve the Updated requirement add the above layout to layout-land folder in res (if you don't have one create a folder named layout-land in res ) now in default layout folder add this xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center"  >
<ru.mw.widget.DrawableImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:adjustViewBounds="true"
     />

<Button
    android:id="@+id/processButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:text="@string/str" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:text="@string/str"
    android:textColor="?android:attr/textColorTertiary" />

</RelativeLayout>

Thats it.

like image 189
Arun C Avatar answered Oct 16 '22 19:10

Arun C


Try to add android:layout_alignParentBottom="true" to the button:

<Button
    android:id="@+id/processButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/str"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    />
like image 25
Nermeen Avatar answered Oct 16 '22 18:10

Nermeen


  1. Align the button to parent bottom using layout_alignParentBottom="true".
  2. Wrap the ImageView between the TextView and Button

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

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str"
        android:textAppearance="@android:style/TextAppearance.Small"
        android:textColor="?android:attr/textColorTertiary"
        android:layout_centerHorizontal="true"
        />

<Button
        android:id="@+id/processButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        />

<ru.mw.widget.DrawableImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_above="@+id/processButton"
        android:adjustViewBounds="true"
        android:layout_centerHorizontal="true"
        android:scaleType="centerCrop"
        />

</RelativeLayout>
like image 1
Archie.bpgc Avatar answered Oct 16 '22 19:10

Archie.bpgc