Randa Khaled
Randa Khaled

Reputation: 119

Changing TextView Style Android applications

How can I change TextView Style in android application to any design I prefer?

for exmaple in messaging: to show a message inside a balloon (like in iPhone inBox) .

thanks,

Upvotes: 1

Views: 1188

Answers (2)

Pavandroid
Pavandroid

Reputation: 1586

You can change the background attribute of the textview in the XML or programmatically you can do this. Use 9-patch tool to create the background image . so, there will be no issues with image stretching.

One more option is to create an XML in the resource folder as shown below where you can do lot of changes(Gradients,Corners,padding etc) to the Image

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <gradient android:startColor="#FF00FF" android:endColor="#FFFF00"
            android:angle="270"/>
    <solid android:color="#00000000"/>
    <stroke android:width="1dp" android:color="@color/round_rect_shape_border"/>
     <padding android:left="7dp" android:top="7dp"
            android:right="7dp" android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>

use this as the background of the Textview.

Upvotes: 3

hqt
hqt

Reputation: 30276

Answer for your question not only true for textview but other view in android.

You will need a new your-self class that extends view you need to change style.

for example:

public class NewTextView extends TextView{

  public NewTextView(){} //just constructor

  @Override
  public void onDraw(Canvas canvas){
  //this is a main method that do your work.
  //for example, you will draw a `baloon` like iPhone
  }

Here is an example code, that draw a straight line in each line of EditText (like you are typing in a paper). You can see this code and learning to do like it.

Again: to do this, you should has some knowledge about drawing in android (Canvas or OpenGL).

public class EditTextExtra extends EditText {
    private Rect Rect;
    private Paint Paint;

    public EditTextExtra (Context context, AttributeSet attrs) {
        super(context, attrs);
        Rect = new Rect();
        Paint = new Paint();
        Paint.setStyle(Paint.Style.FILL_AND_STROKE);
        Paint.setColor(Color.BLACK);       
    }

    @Override
    protected void onDraw(Canvas canvas) {

        int count = getHeight()/getLineHeight();
        if(getLineCount() > count){
            count = getLineCount();   // for long text with scrolling
        }
        Rect r = Rect;
        Paint paint = Paint;

        int baseline = getLineBounds(0, r); // first line

        for (int i = 0; i < count; i++) {
            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
            baseline += getLineHeight(); // next line
        }
        super.onDraw(canvas);
    }
}

Upvotes: 2

Related Questions