2

Can somebody please help me to change the text color of list view without using any base adapter. I have used android.R.layout.simple_list_item_single_choice for displaying option button, But by default the text inside the list view is displaying in white color i need to change it to black color. Is there any way to change the text to black without using custom adapter. I have pasted the code which i used to create list view with option button.enter image description here

  ListView lvCheckBox = (ListView)findViewById(R.id.lvCheckBox);
            save = (ImageView)findViewById(R.id.button1);
            lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            lvCheckBox.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice, values));
Prabhu
  • 590
  • 2
  • 9
  • 22

6 Answers6

1

Just change the default layout of the ListView item

Instead of :

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_listview_item, list);

use

adapter = new ArrayAdapter<String>(this, R.layout.customLayout, list);

and customLayout.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:textSize="20dp" />
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
0

enter image description here

Use this code.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.custom_spinner_textview_layout, from);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerAlarmTone.setAdapter(adapter);

Xml LayoutFile

< ?xml version="1.0" encoding="utf-8"?>
<TextView 
  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="16.0sp" 
    android:textColor="@color/black"
    android:gravity="left"/>
Chirag
  • 56,621
  • 29
  • 151
  • 198
0

Instead of default android.R.layout.simple_list_item_single_choice you can use a custom xml file consisting of a textview and pass this xml file to ArrayAdapter. In this way you can easily change textview color. But still I recomend you to go for a custom adapter for more flexibility.

Braj
  • 2,164
  • 2
  • 26
  • 44
0

Yes! you are correct, I tried your code. By default it shows white color background and Textcolor also. when we press it showing text because of selector color. so it becomes visible to us. I accept the above answer. but we can made our customization in particular portion only. no need to create separate Layout or Textview.

ListView lvCheckBox = (ListView)findViewById(R.id.lvCheckBox);
                save = (ImageView)findViewById(R.id.button1);
    lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    lvCheckBox.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, values) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                CheckedTextView checkedTxtView = (CheckedTextView) super.getView(position, convertView, parent);

                String yourValue = values.get(position);
                checkedTxtView.setText(yourValue);
                checkedTxtView.setTextColor(this.getResources().getColor(android.R.color.black));
                return textView;
            }

    });

I have created sample hello world project and tried this. It works for me. I hope it would help u to achieve your task.

And one more thing, You need CheckedTextView so you are using android.R.layout.simple_list_item_single_choice. If your creating custom layout as per above some examples, you have to use CheckedTextView in those custom layout design. because, In above examples they have used simple textview only. So care on that.

0

You could use Custom Selector, for example, for listview:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"    +
 android:drawable="@drawable/listitem_pressed" />
<item android:state_focused="true" android:drawable="@drawable/listitem_selected" />
 </selector>

Reference: Another Ask

Community
  • 1
  • 1
Max Pinto
  • 1,463
  • 3
  • 16
  • 29
-1

Bro one solution is that you change the background color black of listview your text automatically will b visible. or you have to set a custom adapter on your listview. when you inflate the custom layout you can set the textview color according to your need.

Mudassar Shaheen
  • 1,397
  • 1
  • 9
  • 15