Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change background selection color of ListView?

How can I change the selection color on a ListView? By default, when the user selects an item it shows a blue background. I want to change this to dark gray, or something... Thanks for the help!

like image 915
Joey Morani Avatar asked Mar 27 '10 18:03

Joey Morani


People also ask

How to change the background color of listview items on Android?

This example demonstrate about How to change the background color of ListView items on Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java

How to highlight selected selected listview item's background?

After tried for a while, I found the simplest way to highlight selected ListView item's background can be done with only two lines set to the ListView's layout resource: android:choiceMode="singleChoice" android:listSelector="YOUR_COLOR" There's also other way to make it work, like customize activatedBackgroundIndicator theme.

How to set the background drawable to listview custom layout?

Set the background drawable to listview custom layout to be inflated for each row I recommend using a custom listview with a custom adapter. If you have not used a custom adapter you can set the listselector to listview as below

Is there a way to show the current selected item in listview?

Furthermore, there is one properties here on ListView which is called listSelector. It's a drawable as said by the documentation "Drawable used to indicate the currently selected item in the list." I also believe that this should do the trick and yes it do the trick on my emulator but not on my galaxy tab.


1 Answers

If you wanted your ListView to have the style of the Windows Explorer ListView (including the nice appearance with rounded edges in Win7/Vista), you could use a little P/Invoke to accomplish that:

[DllImport("uxtheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int SetWindowTheme(IntPtr hWnd, string appName, string partList);

// You can subclass ListView and override this method
protected override void OnHandleCreated(EventArgs e)
{
    base.OnHandleCreated(e);
    SetWindowTheme(this.Handle, "explorer", null);
}
like image 120
Zach Johnson Avatar answered Nov 09 '22 23:11

Zach Johnson