Search code examples
javaandroidlistviewfragmenttextcolor

Change Text color for Listview in Tabbed Fragment


i created a tabbed fragment with viewpager. There is simple listview to show data in farg1. But color of text is coming as white. How to change it to black. listview does not give Textcolor property to set. below is code used for fragtheme applied to fragment:

<style name="FragTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:textColor">#000000</item>
        <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
        <!--   <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>-->
    </style>

Below is code for layout xml:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tex1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Recently Used Mobile Number List "
            android:textSize="15sp" />

        <ListView
            android:id="@+id/targetlist" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

presently set background as black to make it readable.


Solution

  • you can create list_textview.xml and define color in that.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:gravity="center_vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <TextView
            android:id="@+id/list_content"
            android:textColor="#000000"
            android:gravity="center"
            android:text="sample"
            android:layout_margin="4dip"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        />
    </LinearLayout>
    

    use the same xml in adapter instead of using android.R.layout.simple_list_item_1

    arrayAdapter = new ArrayAdapter(getActivity(),R.layout.list_textview, R.id.list_content,recentlistarray); 
    recentlist.setAdapter(arrayAdapter);
    

    OR

    ArrayAdapter adapter=new ArrayAdapter(
                this, android.R.layout.simple_list_item_1, listItems){
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view =super.getView(position, convertView, parent);
    
                TextView textView=(TextView) view.findViewById(android.R.id.text1);
                textView.setTextColor(Color.BLACK);
    
                return view;
            }
        };
        recentlist.setAdapter(arrayAdapter);