How to change the Text Color of a listView?

0

I'm trying to change the TEXT color of this listView I made following some examples on the internet, but the most I got was the comment from the line 39 txt.setTextColor (Color.GREEN); The problem is that this only changes the color when clicked and even then the color does not return to normal. : / I need a solution to make the appearance a little more beautiful, I've tried several tutorials from other websites but they did not work very well.

I would like a solution that is as simple as possible for studies. But anything that works is worth it, too. :)

The Main class:

public class Main extends Activity {
    public ListView list;
    static int numPerildo = 2;
    static String perildoAtual;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        list = (ListView) findViewById(R.id.list);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View view, int position, long id) {

                TextView txt = (TextView) view;     //funciona
                //txt.setTextColor(Color.GREEN);
                //TextView txt = (TextView) view.findViewById(R.id.post);

                //mensagem.setTitle("Escolha qual o perildo");
                //mensagem.setMessage( txt.getText().toString() );
                //mensagem.setNeutralButton("OK", null);
                //txt.setTextColor(color.white);

                //trace("Item selecionado : " + position);

                if(txt.getText().toString().equals("valor 1")){
                    numPerildo = 5;
                    telaPerildo();
                }

                perildoAtual = txt.getText().toString();
            }
        });

    }

    public void telaPerildo(){
        Intent i = new Intent(this, Periodo.class);
        startActivity(i);
    }

    static final String[] Cursos = new String[] {"valor 1", 
        "valor 2", 
        "valor 3"};


    public void toast (String msg){
        Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();
    } 

    private void trace (String msg){
        toast (msg);
    }

}

A Main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/azulClaro"
    tools:context="${packageName}.${activityClass}" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Escolha o seu curso:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/text" >

    </ListView>

</RelativeLayout>
    
asked by anonymous 22.07.2014 / 22:59

2 answers

4

So I understand, you want to change the color of all items in ListView .

Since you are using android.R.layout.simple_list_item_1 , which is provided by Android and is using ArrayAdapter , you can not change the color with this setting.

Changing the color of the text in the onItemClick method of OnItemClickListener will in fact only change during the click and will not return to normal.

There are two options:

Use a custom layout, similar to simple_list_item_1 with modification in TextView , as below:

item.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:textColor="@color/suaCor"
/>

You must respect the id that is assigned to TextView so that ArrayAdapter knows how to find TextView .

And use in your Activity this way:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item, Cursos);

    list = (ListView) findViewById(R.id.list);
    list.setAdapter(adapter);

    // Restante do código...
}

The second option is to extend the ArrayAdapter to modify the color when instantiating the item. This way:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Cursos) {

        @Override
        public View getView (int position, View convertView, ViewGroup parent) {

            View view = super.getView(position, convertView, parent);

            // Como o simple_list_item_1 retorna um TextView, esse cast pode ser feito sem problemas
            ((TextView) view).setTextColor(suaCor);

            return view;
        }

    };

    list = (ListView) findViewById(R.id.list);
    list.setAdapter(adapter);

    // Restante do código...
}

The two alternatives are possible, the first one is more "correct", because you change directly in the layout of the item and also more possibilities of customization and is more performative than the second one.

The second is a little forced (using anonymous classes), because it overloads the behavior of the Adapter, to do a lot in this way in terms of visibility logic and etc (one can create its own class that extends the ArrayAdapter in a separate file, gets more organized).

In short, for this simple case I recommend the first hehe

    
22.07.2014 / 23:11
3

You need to create a custom layout for ListView , and then use that layout on the adapter.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/lisItem"
        android:textColor="@color/green"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
    
22.07.2014 / 23:06