Search code examples
androidlistviewtextcolor

Change the text color of a list generated by ArrayAdapter


I am fairly new to Android. I am looking to change the text color of a list, nested within a navigation drawer. I have a resource file with a string-array and 5 items. I then populate the navigation drawer using an ArrayAdapter but I created the "menu" as a ListView. ListView does not have a textColor property that I can set. How can I change the ListView text color?

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private String[] menuItemsForDrawer;
    private DrawerLayout drawerLayout;
    private ListView gpsJobcardDrawerList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        menuItemsForDrawer = getResources().getStringArray(R.array.gps_jobcard_menu_items);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        gpsJobcardDrawerList = (ListView) findViewById(R.id.left_drawer);

        gpsJobcardDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuItemsForDrawer));
    }
}

activity_main.xml

<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">
    <!-- Main Content View (Must be first child in the DrawerLayout -->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/dark_grey_color"/>
    <!-- Navigation Drawer -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="@drawable/orange_color" />
</android.support.v4.widget.DrawerLayout>

gps_jobcard_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="gps_jobcard_menu_items">
        <item>Item 1</item>
        <item>Item 2</item>
        <item>Item 3</item>
        <item>Item 4</item>
        <item>Item 5</item>
    </string-array>
</resources>

Solution

  • The current resource android.R.layout.simple_list_item_1 you are using is an android resource. If you need to change its look and feel then you should create your own resource and use it. This is how we do it.

    Create an xml layout say navigation_item.xml like this

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:textColor="#25383C" <!-- any color you need -->
    />
    

    and change

    gpsJobcardDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuItemsForDrawer));
    

    to

    gpsJobcardDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.navigation_item, menuItemsForDrawer));