0

I have an activity with DrawerLayout and Listview as a navigation drawer:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"></RelativeLayout>
<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:dividerHeight="1dip"
    android:divider="@drawable/gradient" />

The activity has Holo theme applied to it successfully set in the onCreate (light or dark based on the preferences) but the listview is transparent and I can see behind it.

How can I make the listview have the same theme as the rest of the layout? The listview item has this layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="1dip"
android:paddingBottom="1dip"
android:paddingLeft="3dp">
<ImageView
    android:id="@+id/icon"
    android:layout_width="32dip"
    android:layout_height="32dip"
    android:src="@drawable/job_history"
    android:layout_gravity="center_vertical" />
<TextView
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textColor="#fff"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:text="this it a title" />

Giorgi
  • 30,270
  • 13
  • 89
  • 125

3 Answers3

1

use android:background. My Navigation Drawer has a ListView and i applied

android:background="#f1f1f1"

using it on LinearLayout should do it too.

user666
  • 492
  • 4
  • 18
  • If I specify background like that it will have the same color in both light and Dark theme which isn't what I want. – Giorgi Mar 04 '14 at 21:09
  • You can set background color through code, linear.setBackgroundColor, based on your Theme – user666 Mar 04 '14 at 21:13
  • Is there any other way than hard-coding the color in the code? – Giorgi Mar 04 '14 at 21:14
  • If you don't want to use setBackgroundColor, i don't know you can change the layout based on theme, actually they are not changing color based on theme originally. Since they are transparent, they take the background color.That's all i can say – user666 Mar 04 '14 at 21:20
  • So why is the listview transparent? When I put the listview in the other layout file they have the background based on the current theme. – Giorgi Mar 04 '14 at 21:22
  • As i said, they are taking the background color which is your theme's color , normally. Try putting listview top of a textview and you will have the same result as navigation drawer – user666 Mar 04 '14 at 21:24
  • Thanks. I used code from http://stackoverflow.com/a/3668872/239438 to get the background color of the current theme. – Giorgi Mar 05 '14 at 09:51
0

I would recommend giving your ListView a background

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:dividerHeight="1dip"
    android:divider="@drawable/gradient"
    android:background="@color/list_background" />
Rob
  • 1,162
  • 2
  • 18
  • 43
0

you could just use:

android:background="?android:windowBackground"

and apply this to your list, so your list will always use the window background specified in the currently applied theme. No hardcoding, no coding, just styling. e.g.:

 <ListView android:background="?android:windowBackground"
    android:id="@+id/list_drawer_start"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
Denny1989
  • 639
  • 1
  • 9
  • 15