I have an ListActivity and i am displaying one list with:
setListAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_single_choice, mStringList));
By default text color of list items is white, I want to change text color of text views in the list to black.
How should i do it?
This example demonstrates how to change the color and font of Android ListView. 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.
Just create an anonymous subclass of ArrayAdapter and override getView(). Let super. getView() handle all the heavy lifting. Since simple_list_item_1 is just a text view you can customize it (e.g. set textColor) and then return it.
Another simplest way is to create a layout file containing the textview you want with textSize, textStyle, color etc preferred by you and then use it with the ArrayAdapter.
e.g. mytextview.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tv" android:textColor="@color/font_content" android:padding="5sp" android:layout_width="fill_parent" android:background="@drawable/rectgrad" android:singleLine="true" android:gravity="center" android:layout_height="fill_parent"/>
and then use it with your ArrayAdapter as usual like
ListView lst = new ListView(context); String[] arr = {"Item 1","Item 2"}; ArrayAdapter<String> ad = new ArrayAdapter<String>(context,R.layout.mytextview,arr); lst.setAdapter(ad);
This way you won't need to create a custom adapter for it.
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