Question

I'm having difficulties changing textview's textcolor in my listview.

I have a row that consists of more than one textview but only one textview is visible.

I want the text of that textview to change the color of textview depending on the other textview's value in that row. I'm pretty sure you'll understand what I mean when you see the code.

runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        MainActivity.this, strojeviList,
                        R.layout.list_item, new String[] { TAG_SIFSTROJA,
                                TAG_NAZIVSTROJA, TAG_AKTIVAN, TAG_AUTH,
                                TAG_IP, TAG_POCETAK},
                        new int[] { R.id.sifstroja, R.id.nazivstroja, R.id.aktivan,
                                R.id.auth, R.id.ip,R.id.pocetak});
                // updating listview
                setListAdapter(adapter);
             View v;
            TextView blab= (TextView)findViewById(R.id.tekst);
           blab.setTextColor(getResources().getColor(R.color.zelena));

             ListView lv=getListView();

                TextView akt;
                TextView naziv;
                int bla=lv.getCount();
                Log.d(TAG_AUTH,String.valueOf(bla));
                for (int i = 0; i < lv.getCount(); i++) {
                    v=(View) lv.getAdapter().getView(i,null,null);
                    akt = (TextView) v.findViewById(R.id.aktivan);
                    naziv=(TextView)v.findViewById(R.id.nazivstroja);
                    String aktiv=akt.getText().toString();
                    String aktivtekst=naziv.getText().toString();

                    Log.d(aktiv, aktiv);
                    if(aktiv.equals("1"))
                    {
                        Log.d("uso","uso sam");
                        Log.d("uso",aktivtekst);

                        naziv.setTextColor(getResources().getColor(R.color.zelena));

                    }
                    else {
                        Log.d("nisam aktivan","nisam aktivan");
                        Log.d("nisam aktivan",aktivtekst);
                        naziv.setTextColor(getResources().getColor(R.color.crvena));
                    }
                }


            }

I can get textview values and if part works just fine but color is not changed, so what can I do to change the color?

Was it helpful?

Solution

You need to move this code inside the adapter's getView method, for example:

....
ListAdapter adapter = new SimpleAdapter(
                    MainActivity.this, strojeviList,
                    R.layout.list_item, new String[] { TAG_SIFSTROJA,
                            TAG_NAZIVSTROJA, TAG_AKTIVAN, TAG_AUTH,
                            TAG_IP, TAG_POCETAK},
                    new int[] { R.id.sifstroja, R.id.nazivstroja, R.id.aktivan,
                            R.id.auth, R.id.ip,R.id.pocetak}){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = super.getView(position, convertView, parent);
    TextView akt = (TextView) v.findViewById(R.id.aktivan);
    TextView naziv=(TextView)v.findViewById(R.id.nazivstroja);
    String aktiv=akt.getText().toString();
    String aktivtekst=naziv.getText().toString();

    Log.d(aktiv, aktiv);
    if(aktiv.equals("1"))
    {
           Log.d("uso","uso sam");
           Log.d("uso",aktivtekst);
           naziv.setTextColor(getResources().getColor(R.color.zelena));

     }
     else {
           Log.d("nisam aktivan","nisam aktivan");
           Log.d("nisam aktivan",aktivtekst);
            naziv.setTextColor(getResources().getColor(R.color.crvena));
     }  
    return v;
  }
};
setListAdapter(adapter);

TextView blab= (TextView)findViewById(R.id.tekst);
blab.setTextColor(getResources().getColor(R.color.zelena));

OTHER TIPS

Try this....

naziv.setTextColor(Color.parseColor(getResources().getColor(R.color.crvena)));

There are so many methods to change the textColor of TextView at runtime:

  • textView.setTextColor(Color.rgb(0,0,0));

  • textView.setTextColor(Color.RED);

  • textView.setTextColor(getResources().getColor(R.color.red));

The thing to notice here is this:

int android.graphics.Color.GREEN = -16711936 [0xff00ff00]
int com.example.R.color.green = 2130968577 [0x7f040001]

you can see that both are not same but the /res/values/color.xml goes like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#ff0000</color>
    <color name="green">#00ff00</color>
</resources>

So I think that because of the difference in both android.graphics.Color.GREEN and com.example.R.color.green we are not getting the expected results while using textView.setTextColor(R.color.green);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top