Search code examples
androidcolorstextviewspannablestring

different color on each line in textview


I'm having issues trying to set a different color on each line depending on the first character. This is what I currently have. Nothing ever populates in the textview.

TextView output=(TextView) findViewById(R.id.textView1);
    File file = new File("/sdcard/file.txt");
    StringBuilder text = new StringBuilder();
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            if (line.substring(0,1).equals("r")) {
                appendColoredText(output, line, Color.RED);
            } else if (line.substring(0,1).equals("y")) {
                appendColoredText(output, line, Color.YELLOW);
            } else if (line.substring(0,1).equals("c")) {
                appendColoredText(output, line, Color.CYAN);
            } else {
                appendColoredText(output, line, Color.BLACK);
            }
            //text.append('\n');
        }
        output.setText(text);
    }
    catch (IOException e) {
        Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

public static void appendColoredText(TextView tv, String text, int color) {
    int start = tv.getText().length();
    tv.append(text);
    int end = tv.getText().length();

    Spannable spannableText = (Spannable) tv.getText();
    spannableText.setSpan(new ForegroundColorSpan(color), start, end, 0);
}

Solution

  • public void displayOutput()
    {
        TextView output=(TextView) findViewById(R.id.textView1);
        output.setMaxLines(20000);
        //File sdcard = Environment.getExternalStorageDirectory();
        File file = new File("/sdcard/file.txt");
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            while ((line = br.readLine()) != null) {
                if (line.substring(0,1).equals("R")) {
                    appendColoredText(output, line, Color.RED);
                } else if (line.substring(0,1).equals("Y")) {
                    appendColoredText(output, line, Color.YELLOW);
                } else if (line.substring(0,1).equals("C")) {
                    appendColoredText(output, line, Color.CYAN);
                } else {
                    appendColoredText(output, line, Color.WHITE);
                }   
            }
        }
        catch (IOException e) {
            Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
    
    public static void appendColoredText(TextView tv, String text, int color) {
        int start = tv.getText().length();
        tv.append(text);
        int end = tv.getText().length();
        Spannable spannableText = (Spannable) tv.getText();
        spannableText.setSpan(new ForegroundColorSpan(color), start, end, 0);
        tv.append("\n");
    }
    

    This is the code that ended up working for me

    My goal was to read a text file line by line and depending on the first character of the line assign that line in a textview a color. My problem with the original code was that nothing ever showed up in the textview in any color.