Change text color of android.R.layout.simple_list_item_multiple_choice

3.3k Views Asked by At

I am trying to figure out how I can change the text color of android.R.layout.simple_list_item_multiple_choice

I know you can create your own listview with checkboxes but I really do not want to have to do that as the android.R.layout.simple_list_item_multiple_choice works perfectly, I would just like to know how to change the Text color.

I have come accross this question here but I do not understand how I can use the most voted up answer.

How can I override android.R.layout.simple_list_item_multiple_choice textview so I can change the color of it. Thank you.

3

There are 3 best solutions below

0
On BEST ANSWER

Just make custom layout - simple_list_item_multiple_choice.xml like this -

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    android:textColor="#FF0000"
/>

and use it in place of android.R.layout.simple_list_item_multiple_choice.xml

0
On

You can change color easily. You just have to override the getView() method. And inflate the android.R.layout.simple_list_item_multiple_choice in particular view. now you can access the whole template. So that you can get the TextView used in this template by finding using id android.R.id.text1. after that you can change the behavior of textview.

ArrayAdapter<String> list = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, your_data_container) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = LayoutInflater.from(InvitationFlowActivity.this).inflate(android.R.layout.simple_list_item_multiple_choice, null);
                TextView tv = (TextView) v.findViewById(android.R.id.text1);
                tv.setTextColor("Your_color");
            }
        };

And if you are using custome adapter then you can also override the same getView() method and you can customize it as per your requirement.

0
On

There are two ways to do this. 1. Override the getView method 2. Use custom layout instead of standard layout.

No. 2 is better as you have more control on the same and you can edit at your ease. Alt+Click will clone the required layout to your res/layout folder. Make changes to the new layout and use it in the adapter.

ArrayAdapter aa = new ArrayAdapter(getApplicationContext(),R.layout.text_change,listItems);
        listView.setAdapter(aa);

Here listItems is the String array of list. text_change.xml is the updated layout xml listView is your listview in the main layout.

In order to dynamically update color of content use the following code

AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position==0)
                {
                    //Intent intent = new Intent(CoffeeShop.this,DrinkCategoryActivity.class);
                    //startActivity(intent);
                    TextView txtV = (TextView)view;
                    txtV.setTextColor(Color.BLUE);
                }
                else
                {
                    TextView txtV = (TextView)view;
                    txtV.setTextColor(Color.YELLOW);
                    Toast toast = Toast.makeText(getApplicationContext(),(CharSequence)"These Methods are yet to be made public", Toast.LENGTH_SHORT);
                    toast.show();
                }

            }
        };
        ListView listView = (ListView)findViewById(R.id.list_opt);
        listView.setOnItemClickListener(itemClickListener);

Adding onclicklistner for the view will allow updating the color of the text upon clicking. You can add respective logic as to which color should the text be displayed.