Search code examples
androidandroid-layoutandroid-linearlayoutnavigation-drawer

layout background image android


I want to change the default background image of my navigation drawer.

I have a .jpg image (1600x900) in drawable folder. I set android:background="@drawable/bg_1" for LinearLayout, but when i debug the app in my device, the image doesn't appear and i have a white background.

This is my nav_header_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/AccountInformation"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/bg_1"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:weightSum="1">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="70dp"
        android:layout_height="60dp"
        android:layout_weight="0.3"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        app:srcCompat="@drawable/contatti" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing">

        <Spinner
            android:id="@+id/AccountSpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/AccountEmail"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true" />

        <TextView
            android:id="@+id/AccountName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android Studio"
           android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

        <TextView
            android:id="@+id/AccountEmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/nav_header_vertical_spacing"
            android:text="[email protected]" />
    </RelativeLayout>

</LinearLayout>

And this is the layout of the activity (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

Solution

  • The attibute android:background="@drawable/bg_1" should be in the NavigationView view not the LinearLayout ViewGroup.

    Hope this help.