Tigran Babajanyan
Tigran Babajanyan

Reputation: 2025

set background png on Linearlayout without affecting layout size

Background image doesn't fill Linearlayout and changes that size

  <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="@mipmap/dashboard_background"
      android:orientation="vertical"
      android:padding="10dp">

enter image description here

desirable look is this

enter image description here

How can I reach that?

Upvotes: 1

Views: 920

Answers (2)

Demigod
Demigod

Reputation: 5635

Background always stretches to its View size. So you can't really control how it will look like. Instead, put an ImageView as a bottom most (meaning that it will lay under everything else) element in your layout, and use your background image as a source (src) for this ImageView, and so you will be able to control the look of the background with scaleType (probable you would want to use centerCrop or fitCenter). More about scaleType here.

In the end your layout should look something like this:

<?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">

  <ImageView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:scaleType="fitCenter"
      android:src="@mipmap/dashboard_background" />

  <FrameLayout
      android:id="@+id/content_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent">

    <!-- Put your view elements here -->

  </FrameLayout>

</FrameLayout>

Upvotes: 2

Caspar Geerlings
Caspar Geerlings

Reputation: 1061

You should use a frame layout with an imageview and a linearlayout inside

<FrameLayout
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:src="@drawable/background"/>

<LinearLayout
    ---Your Layout---
</LinearLayout>

Upvotes: 2

Related Questions