4

I want to set the background color of an entire line in TextView and not necessarily the part of the line covered by text (which I can do by using Span).

E.g. If say line 0, can accomodate 25 characters, but only has 12 characters. If I say

SpannableString s = new SpannableString("abcdefghijkl");
s.setSpan(new BackgroundColorSpan(0xffffffff), 0, 11, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
TextView t = new TextView();
t.setText(s);

This would set background color of half the line. But I want to have the entire line (entire width of TextView) to be filled with the background color. Also I want this to happen for only one line, not the entire TextView background.

Any idea how this can be achieved?

N Jay
  • 1,774
  • 1
  • 17
  • 36
Time Travel
  • 41
  • 1
  • 3
  • Check : http://stackoverflow.com/questions/8105545/can-i-select-a-color-for-each-text-line-i-append-to-a-textview – EvZ Jan 11 '13 at 16:14
  • No this would set only the background of the part of the line having text. I want the entire line (even empty space) to have the same background color. – Time Travel Jan 11 '13 at 16:21
  • I think that you should change 11 to s.length(), you will have the total length of your String and set its background – Reda Dec 02 '13 at 09:35
  • look at http://stackoverflow.com/questions/22939862/textview-with-background-color-and-line-spacing/30096905#30096905 – qw1nz Sep 25 '15 at 13:40

4 Answers4

0

Wrap your TextView in a RelativeLayout, and put another view behind it:

<RelativeLayout
    android:id="@+id/container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <View android:id="@+id/bgcolor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@color/whatever"
        />
    <TextView android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</RelativeLayout>

Now, on global layout, find the height of a line of your TextView, and set the bgcolor view to that height:

final TextView textView = (TextView) findViewById(R.id.textview);
final ViewTreeObserver vto = textView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    public void onGlobalLayout() {
        int lineHeight = textView.getLineHeight();
        // May need to also getLineSpacingExtra, etc. not sure.
        View bgColor = findViewById(R.id.bgcolor);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) 
                bgColor.getLayoutParams();
        lp.height = lineHeight;
        bgColor.setLayoutParams(lp);
        // May or may not want to remove the listener:
        vto.removeGlobalOnLayoutListener(this);
    }
}
dokkaebi
  • 9,004
  • 3
  • 42
  • 60
  • Yes, this seems like a solution. But I need to set more than 1 line with background color. And what if the user scrolls through the lines. How would the background color View move? – Time Travel Jan 11 '13 at 16:47
  • Ohh, you're trying to highlight a particular line? – dokkaebi Jan 11 '13 at 16:49
  • In that case I think I would go with a ListView, and split your text into lines based on its width. Then you get line highlighting for free. – dokkaebi Jan 11 '13 at 16:50
  • Ok I'll explore this. Also 1 more question, what if I want to make the text editable (probably if there was some methid in TextView, I could use the same for EditText and create this Editable text box with line highlighting - any suggestions?) – Time Travel Jan 11 '13 at 16:54
  • That complicates things a bit more; then you have to think about what happens when a user edit pushes some text onto the next line. Which line retains the highlight? Also, the same scenario kind of eliminates the ListView option. In that case, I think I would stick with something like my answer above, and just keep repositioning the highlight view(s) when the text changes. Use EditText#addTextChangedListener for that. The more we talk about this, the more it feels like we might be missing something, too. This must have been done before... – dokkaebi Jan 11 '13 at 17:00
  • Thanks, this is quite some information using which I can start working on what I aim to achieve. If I have any doubts I would probably post here or another question. :) – Time Travel Jan 11 '13 at 17:16
0

Your goal is to use something like LineaLayout with vertical orientation,
that will have TextView as a "colorable line".

1) Create a class that extends LinearLayout.
2) Write basic functions to add "colorable line","paint it" & e.t.c.
3) Use your view in layouts XML.

EvZ
  • 11,889
  • 4
  • 38
  • 76
  • Thanks, although dokkaebi's answer seems most appropriate, and easiest for me to implement what I require, but thanks for providing an idea on how to create my own view. This would be helpful. Thanks. – Time Travel Jan 11 '13 at 17:07
0
    TextView name = (TextView) findViewById(R.id.name);
    String iName = "YOYOYOYO";

    ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(184, 184,
            184));

    SpannableString ssb = new SpannableString(iName);
    ssb.setSpan(fcs, 0, iName.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    name.setText(ssb);
Yauraw Gadav
  • 1,706
  • 1
  • 18
  • 39
-1

TextView (just like every other android View) have the method setBackgroundColor

t.setBackgroundColor(0xffffffff);
Budius
  • 39,391
  • 16
  • 102
  • 144