Android Stack
Android Stack

Reputation: 4314

Multiple Gradient for single Textview

Refering agradient to textview regard as normal action can be achieved in android ,

as below :

 android:background="@drawable/gredient"

and gradient XML will be as below code :

<?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
  <gradient android:startColor="#B22222" android:centerColor="#000000"
          android:endColor="#B22222" android:angle="0" /> 
    <corners android:radius="15dp"  /> 
 </shape>

what im asking about is there is away in android to apply multiple gradients to single textview consisting of may paragraphs , so each paragraph will has different gradient .

any advice will be appreciated ,

thanks

Upvotes: 1

Views: 1014

Answers (3)

Joe
Joe

Reputation: 14059

You might want to consider using a WebView instead of TextView (if you need more advanced formatting), but if you want to stick to a TextView, LineBackgroundSpan is your friend :)

Although there might be some better solution (most likely involving a custom Layout-derived class), here's some sample code that should do what you want just by implementing a special LineBackgroundSpan interface:

public class MyActivity extends Activity {

    private Editable mEditable;

    private class MySpan implements LineBackgroundSpan {
        private final Rect rect = new Rect();
        private final Drawable drawable;
        private int next = 0;

        public MySpan(Drawable drawable) {
            this.drawable = drawable;
        }

        @Override
        public void drawBackground(Canvas c, Paint p, int left, int right, int top, int baseline,
                int bottom, CharSequence text, int start, int end, int lnum) {
            if (start == end) return;
            if (next == 0) {
                next = mEditable.nextSpanTransition(start, Integer.MAX_VALUE, MySpan.class);
                rect.left = left;
                rect.top = top;
            }
            if (next == end) {
                rect.right = right;
                rect.bottom = bottom;
                c.save();
                drawable.setBounds(rect);
                drawable.draw(c);
                c.restore();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final Resources res = getResources();
        final Drawable gd1 = res.getDrawable(R.drawable.gd1);
        final Drawable gd2 = res.getDrawable(R.drawable.gd2);
        final Drawable gd3 = res.getDrawable(R.drawable.gd3);
        final Drawable gd4 = res.getDrawable(R.drawable.gd4);

        final TextView tv = new TextView(this);
        setContentView(tv);

        tv.setText("Paragraphs with drawable background:\n", BufferType.EDITABLE);
        mEditable = tv.getEditableText();

        final String text = "paragraph text ";
        appendPara("###\n".replaceAll("#", text), gd1);
        appendPara("##############\n".replaceAll("#", text), gd2);
        appendPara("#######\n".replaceAll("#", text), gd3);
        appendPara("###########\n".replaceAll("#", text), gd4);
    }

    private void appendPara(String string, Drawable gd) {
        final int start = mEditable.length();
        mEditable.append(string);
        final int end = mEditable.length();
        mEditable.setSpan(new MySpan(gd), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

}

Upvotes: 1

Aqif Hamid
Aqif Hamid

Reputation: 3521

As, you need multiple gradients for your multiple Paragraphs then why not to have one TextView per Paragraph and setting unique gradient for each TextView.

If you donot know how many paragraphs you will have,

You must create a LinearLayout whose height is wrap-content and at run time for every paragraph create a TextView with fill_parent width and wrap_context height and add that TextView to your parent (LinearLayout) view. and in code set backgrounds to your TextViews.

Upvotes: 0

udiboy1209
udiboy1209

Reputation: 1502

I don't think it is possible with android's default TextView.

You can create your own view, by extending you class with View, and use methods like Canvas.drawText(...) and Canvas.drawBitmap(...) in the view's onDraw() method, to create each paragraph with a different gradient.

This can also help you control line-height and paragraph-spacing.

Upvotes: 0

Related Questions