0

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 https://www.youtube.com/watch?v=UuCkg_P6_Ws

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.graphics.Color;
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;
}

@Override
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(R.id.icon_coin);
    icon_coin.setImageBitmap(coinItem.get(position).getIcon_coin());

    TextView text_last = (TextView) convertView.findViewById(R.id.text_last);
    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(R.id.text_coin);
    text_coin.setText(coinItem.get(position).getText_coin());

    TextView text_bid = (TextView) convertView.findViewById(R.id.text_bid);
    text_bid.setText("売値");

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

    TextView text_ask = (TextView) convertView.findViewById(R.id.text_ask);
    text_ask.setText("買値");

    TextView text_ask_price = (TextView) convertView.findViewById(R.id.text_ask_price);
    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;
    btc_prelast="0";
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            coin_btc.clone().enqueue(new Callback<Ticker>() {
                @Override
                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));
                    coinAdapter.diff(btc_last,btc_prelast);
                    btc_prelast = btc_last;
                    coinAdapter.notifyDataSetChanged();

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

                }
            });
13
  • Post your adapter code Dec 23, 2017 at 5:45
  • I tried but this way changes all item color at the same time
    – NooNoo
    Dec 23, 2017 at 5:46
  • Post the your adapter code Dec 23, 2017 at 5:47
  • Check the answer @NooNoo Dec 23, 2017 at 5:51
  • I add the code , sorry
    – NooNoo
    Dec 23, 2017 at 5:59

3 Answers 3

0

Simple putting the else condition would work.

Assuming your default colour for TextView is Black.

if(x < 0 ){
  textview.setTextColor(Color.RED);
}else if(x > 0){
  textview.setTextColor(Color.GREEN);
}else{
  //your textview default colour
  textview.setTextColor(Color.BLACK);
}
3
  • I have 12 items and this way change all item. So I use position but listview is recycled , so out of screen item is also change
    – NooNoo
    Dec 23, 2017 at 6:03
  • So Use this code. It will help you. I understand your issue Dec 23, 2017 at 6:06
  • Check this answer @NooNoo Dec 23, 2017 at 6:47
0

I'm assuming that inside your listview you already have a TextView in the layout. All you have to do is call TextView#setTextColor. An example would be:

TextView t = (TextView)v.findViewById(R.id.textviewname);
t.setTextColor(Color.RED);
1
  • I have 12 items and this way change all item. So I use position but listview is recycled , so out of screen item is also change
    – NooNoo
    Dec 23, 2017 at 5:58
0

In your Getview Method, Check your margin.

    if(margin > 0){textview.setTextColor(Color.GREEN);}
    else if(margin < 0){textview.setTextColor(Color.RED);}
    else {textview.setTextColor(Color.BLACK);}

Check this codition.

1
  • Please share code if query is exist, else glad to hear your query has resolved. Dec 23, 2017 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.