Skip to content

Android.widget.TextView

TextView에서 한줄에 모든 텍스트를 삽입하고싶을 경우

아래와 같은 속성을 layout.xml에 사용하면 된다.

android:singleLine="true"

TextView 생략 효과

Android:ellipsize 속성을 사용하면 된다.

  • none: 앞부분 부터 표시. 그냥 자름.
  • start: 앞부분을 '...'으로 표시
  • middle: 중간 부분을 '...'으로 표시
  • end: 뒷부분을 '...'으로 표시
  • marquee: 글자 이동 애니메이션.
    • 애니메이션이 무한반복으로 재생되게 하고 싶을 경우 android:marqueeRepeatLimit="marquee_forever"속성을 추가하면 된다.

자동 링크걸기 (인터넷, 이메일, 지도, 전화번호)

  • NONE: android:autoLink="none"
  • E-Mail: android:autoLink="email"
  • Address: android:autoLink="map"
  • URL: android:autoLink="web"
  • Phone: android:autoLink="phone"
  • ALL: android:autoLink="all"

TextView에서 HTML TAG를 적용하는 방법 (웹주소와 이메일 주소를 하이퍼링크하는 방법)

layout.xml:

<TextView
    android:text="http://www.naver.com"
    android:id="@+id/TextView01"
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:autoLink="web">
</TextView>

Java source code:

TextView TextView01 = (TextView) findViewById(R.id.TextView01);
TextView01.setText(Html.fromHtml("<a href=\"http://www.naver.com\">네이버으로 이동</a>"));
textView.setMovementMethod(LinkMovementMethod.getInstance());

TextView에서 클릭이벤트 발생 시키는 법

TextView 속성중 Clickable 속성을 True로 주면 된다.

android:clickable="true"
android:onClick="onClickPlayButton"

How to Show Circular Text using TextView in Android

Text크기를 자동으로 조절해주는 TextView:

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new GraphicsView(this));
    }

    static public class GraphicsView extends View {
        private static final String QUOTE = "This is a curved text";
        private Path circle;
        private Paint cPaint;
        private Paint tPaint;

        public GraphicsView(Context context) {
            super(context);

            int color = Color.argb(127, 255, 0, 255);

            circle = new Path();
            circle.addCircle(230, 350, 150, Direction.CW);

            cPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            cPaint.setStyle(Paint.Style.STROKE);
            cPaint.setColor(Color.LTGRAY);
            cPaint.setStrokeWidth(3);

            setBackgroundResource(R.drawable.heart);

            tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
            tPaint.setColor(Color.BLACK);
            tPaint.setTextSize(50);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawTextOnPath(QUOTE, circle, 485, 20, tPaint);
        }
    }
}

문자열 마퀴효과

우선, Layout XML에서 아래와 같이 적용한다.

android:ellipsize="marquee"
android:singleLine="true" 

그 이후, Java Source부분(주로 onCreate함수 )에서 아래와 같이 입력하면 된다.

TextView tv = (TextView)findViewById(해당 TextView 아이디);
tv.setSelected(true);  // 포커스 없어도 자동으로 흐르게...

안드로이드 TextView 에 Font 바꾸기

Android_Typeface.png

TextView의 속성에서 바꾸는 방법은 아래와 같다.

android:typeface= "normal"

만약 asset에서 바꿀 경우 아래와 같이 적용하면 된다.

TextView v = (TextView) findViewById(R.id.textview);
v.setTypeface(Typeface.createFromAsset(getAssets(), "Font.ttf"));

How to Change DrawableLeft

TextView textView = (TextView) findViewById(R.id.myTxtView);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);

Stroke TextView 표현방법

res/values/attr.xml에 아래 속성을 추가한다.

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <!-- TextView의 아웃라인을 위한 XML Layout 속성. -->
    <declare-styleable name="StrokeTextView">
        <attr name="textStroke" format="boolean" />
        <attr name="textStrokeWidth" format="float" /> <!-- OR format="dimension" -->
        <attr name="textStrokeColor" format="color" />
    </declare-styleable>
</resources>

이 후, TextView를 재정의 한다.

package ~;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.widget.TextView;

import com.typhoon.androidclient.R;

/**
 * 아웃라인(Stroke)적용을 위한 Custom {@link TextView}.
 * 
 * @author cwlee
 * @since 140108
 */
public class StrokeTextView extends TextView {

    private boolean _stroke = false;
    private float _strokeWidth = 0.0f;
    private int _strokeColor = Color.WHITE;

    public StrokeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context, attrs);
    }

    public StrokeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public StrokeTextView(Context context) {
        super(context);
    }

    private void initView(Context context, AttributeSet attrs) {
        TypedArray type = context.obtainStyledAttributes(attrs, R.styleable.StrokeTextView);
        _stroke = type.getBoolean(R.styleable.StrokeTextView_textStroke, false);
        _strokeWidth = type.getFloat(R.styleable.StrokeTextView_textStrokeWidth, 0.0f);
        _strokeColor = type.getColor(R.styleable.StrokeTextView_textStrokeColor, Color.WHITE);
        type.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (_stroke == true) {
            // DRAW STROKE!
            ColorStateList states = getTextColors();
            getPaint().setStyle(Style.STROKE);
            getPaint().setStrokeWidth(_strokeWidth);
            setTextColor(_strokeColor);
            super.onDraw(canvas);

            // DRAW INNER TEXT!
            getPaint().setStyle(Style.FILL);
            setTextColor(states);
        }
        super.onDraw(canvas);
    }
}

마지막으로 Layout에 해당 Widget을 적용하면 된다.

<~.widget.StrokeTextView
    xmlns:app="http://schemas.android.com/apk/res/~"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="HAHAHA"
    android:textColor="#FFFFFF"
    android:textSize="12sp"
    android:textStyle="bold"
    android:typeface="sans"
    app:textStroke="true"
    app:textStrokeColor="#00FF00"
    app:textStrokeWidth="4.0" />

중요한 것 중 하나는, XML Namesapce를 아래와 같이 적용해야 한다.

xmlns:app="http://schemas.android.com/apk/res/[APP_PACKAGENAME]"

Favorite site