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!
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
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.
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
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With