Skip to content
Advertisement

Define text color based on value using AsyncTask

So basically my AsyncTask get the value from the url and when i execute i wanna show green text or red text if contains (“-“). I have search around and none of the option worked for me. i do have a RecyclerView.ViewHolder but don’t know how to incorporate before i execute. Everything works, except the colors. Thank you in advance

Activity

public class BTCData extends AsyncTask<Void,RecyclerView.ViewHolder,Void> {
    String data = "";
    String dataParsed1h ="";
    String dataParsed24h ="";
    String dataParsed7d ="";
    String percent_change_1h = "";
    String percent_change_24h = "";
    String percent_change_7d = "";
    Activity activity;
    List<Model> items;

    public BTCData() {

    }

    @Override
    protected Void doInBackground(Void... voids) {
        try {
            URL url = new URL ("https://...");
            HttpURLConnection httpURLConnection = (java.net.HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while (line != null){
                line = bufferedReader.readLine();
                data = data+line;
            }

            JSONArray JA = new JSONArray(data);
            for(int i=0 ;i< JA.length(); i++){
                JSONObject JO = (JSONObject) JA.get(i);
                percent_change_1h =  "1H  " + JO.getString("percent_change_1h") + "%";
                percent_change_24h = "24H  " + JO.getString("percent_change_24h") + "%";
                percent_change_7d = "7D  " + JO.getString("percent_change_7d") + "%" ;

                dataParsed1h = dataParsed1h + percent_change_1h;
                dataParsed24h = dataParsed24h + percent_change_7d;
                dataParsed7d = dataParsed7d + percent_change_24h;


            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        MainActivity.btc_percent_change_1h.setText(this.dataParsed1h);
        MainActivity.btc_percent_change_24h.setText(this.dataParsed24h);
        MainActivity.btc_percent_change_7d.setText(this.dataParsed7d);
    }
}```

**View Holder**
public class CoinAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

Adapter adapter;
boolean isLoading;
Activity activity;
List<Model> items;

int visibleThreshold = 5,lastVisibleItem, totalItemcount;

public void setAdapter(Adapter adapter) {
    this.adapter = adapter;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(activity)
            .inflate(R.layout.activity_main,parent,false);
    return new CoinViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    Model item = items.get(position);
    CoinViewHolder holderItem = (CoinViewHolder)holder;

    holderItem.btc_percent_change_1h.setTextColor(item.getPercentage_change_1h().contains("-")?
            Color.parseColor("#FF0000"):Color.parseColor("#32CD32"));
    holderItem.btc_percent_change_24h.setTextColor(item.getPercentage_change_24h().contains("-")?
            Color.parseColor("#FF0000"):Color.parseColor("#32CD32"));
    holderItem.btc_percent_change_7d.setTextColor(item.getPercentage_change_7d().contains("-")?
            Color.parseColor("#FF0000"):Color.parseColor("#32CD32"));

}

@Override
public int getItemCount() {
    return items.size();
}
public void setLoader() {isLoading = true;}
public void updateData (List<Model> models)
{
    this.items = models;
    notifyDataSetChanged();
}
**on Activity**


    public static TextView btc_percent_change_1h;
    public static TextView btc_percent_change_24h;
    public static TextView btc_percent_change_7d;



//Percentage
        btc_percent_change_1h = (TextView) findViewById(R.id.btc_percent_change_1h);
        btc_percent_change_24h = (TextView) findViewById(R.id.btc_percent_change_24h);
        btc_percent_change_7d = (TextView) findViewById(R.id.btc_percent_change_7d);


and finally call...

BTCData process = new BTCData();
        process.execute();

Green times but red if negative value

Advertisement

Answer

Maybe is not the best option, but it works

@Override
    protected void onPostExecute(CoinAdapter aVoid) {
        super.onPostExecute(aVoid);
        MainActivity.btc_percent_change_1h.setTextColor((this.dataParsed1h.contains("-")?
                Color.parseColor("#FF0000"):Color.parseColor("#32CD32")));
        MainActivity.btc_percent_change_1h.setText(this.dataParsed1h);
        MainActivity.btc_percent_change_24h.setTextColor((this.dataParsed24h.contains("-")?
                Color.parseColor("#FF0000"):Color.parseColor("#32CD32")));
        MainActivity.btc_percent_change_24h.setText(this.dataParsed24h);
        MainActivity.btc_percent_change_7d.setTextColor((this.dataParsed7d.contains("-")?
                Color.parseColor("#FF0000"):Color.parseColor("#32CD32")));
        MainActivity.btc_percent_change_7d.setText(this.dataParsed7d);
    }
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement