0
votes

I am facing a problem with setting background image for my app. I have linear layout with two imageviews. One with logo and weight 1 and one with background image with weight 3. I want the second one to be always centered horizontally and have sides cropped in vertical orientation and bottom cropped in horizontal orientation. CENTER_CROP almost does the job but I want to have the top of my image drawn. I don't care about the bottom.

In this configuration vertical orientation is perfect but horizontal cuts the top of image and I want the top to be always visible. Bottom can be cropped

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_margin="50dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/masters_logo"
        android:id="@+id/imageView" />

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

</LinearLayout>

That's what I want to achieve.

Vertical orientation: vertical

Horizontal orientation: horizontal

2

2 Answers

0
votes

If you want to set background into your login screen, you should add android:background="@drawable/masters_login_background" attribute into your root LinearLayout. Otherwise, you should use, for example, RelativeLayout as root element and add inside this your ImageView as top element, smth like this:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/masters_login_background"/>

        <!-- other code here -->

</RelativeLayout>
0
votes

I had to edit the ans as I understood your question wrongly. Please use the below code. This will postion your bg img in the background and logo above the bg img.

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="50dp"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/masters_logo"
            android:id="@+id/imageView" />



    </LinearLayout>