NooNoo NooNoo - 3 months ago 45
Java Question

How to change text color individual item dynamically?

How to change text color individual item of listview dynamically?
I want to implement the following function in Android.

  1. Call API regularly and get each price data

  2. Print in listview

  3. Calculate each price margin

  4. If a certain item's margin is minus, change color to red. plus is green.

This function is often seen in Cryptocurrency Apps.
like that

I implemented 1, 2, and 3.
How to implement 4?
If you know anything about that please let me know.

public class CoinAdapter extends ArrayAdapter<CoinItem> {
Context context;
LayoutInflater layoutInflater;
ArrayList<CoinItem> coinItem;
float x;
import android.content.Context;
import android.content.res.ColorStateList;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;

public class CoinAdapter extends ArrayAdapter<CoinItem> {
Context context;
LayoutInflater layoutInflater;
ArrayList<CoinItem> coinItem;
float x;

public CoinAdapter(Context context, int resource, ArrayList<CoinItem> coinItem) {
super(context, resource, coinItem);
this.context = context;
this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.coinItem = coinItem;

public float diff(String btc_last, String btc_prelast) {
x = Float.parseFloat(btc_last) - Float.parseFloat(btc_prelast);
return x;

public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = layoutInflater.inflate(R.layout.item_ticker, null);

ImageView icon_coin = (ImageView) convertView.findViewById(;

TextView text_last = (TextView) convertView.findViewById(;
text_last.setText(coinItem.get(position).getText_last() + "円");
if (x > 0) text_last.setTextColor(Color.GREEN);
else if (x < 0) text_last.setTextColor(Color.RED);
else text_last.setTextColor(Color.BLACK);

TextView text_coin = (TextView) convertView.findViewById(;

TextView text_bid = (TextView) convertView.findViewById(;

TextView text_bid_price = (TextView) convertView.findViewById(;
text_bid_price.setText(coinItem.get(position).getText_bid_price() + "円");

TextView text_ask = (TextView) convertView.findViewById(;

TextView text_ask_price = (TextView) convertView.findViewById(;
text_ask_price.setText(coinItem.get(position).getText_ask_price() + "円");

return convertView;

Calling API code is like that

final Handler handler = new Handler();
final int delay = 1000;
handler.postDelayed(new Runnable() {
public void run() {
coin_btc.clone().enqueue(new Callback<Ticker>() {
public void onResponse(Call<Ticker> call, Response<Ticker> response) {
btc_last = Float.toString(response.body().getLast());
btc_bid = Float.toString(response.body().getBid());
btc_ask = Float.toString(response.body().getAsk());
coinItem.set(0, new CoinItem(BitmapFactory.decodeResource(getResources(), icon_coin[0]), btc_last, name_coin[0], btc_bid, btc_ask));
btc_prelast = btc_last;

public void onFailure(Call<Ticker> call, Throwable t) {



Answer Source

Simple putting the else condition would work.

Assuming your default colour for TextView is Black.

if(x < 0 ){
}else if(x > 0){
  //your textview default colour
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download