Android.widget.TextView
lastmod 2018-08-17created 2016-06-103 edits
Fitting all text on a single line in a TextView
Use the following attribute in layout.xml.
TextView ellipsis effect
Use the Android:ellipsize attribute.
- none: Shows from the beginning. Simply truncates.
- start: Shows the beginning as '...'
- middle: Shows the middle part as '...'
- end: Shows the end as '...'
- marquee: Scrolling text animation.
- To make the animation repeat forever, add the
android:marqueeRepeatLimit="marquee_forever"attribute.
- To make the animation repeat forever, add the
LINK
Automatic linking (internet, email, map, phone number)
- 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"
Applying HTML TAGs in a TextView (hyperlinking web addresses and email addresses)
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());
Triggering click events in a TextView
Set the Clickable attribute of the TextView to True.
How to Show Circular Text using TextView in Android
A TextView that automatically adjusts the text size:
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);
}
}
}
String marquee effect
First, apply the following in the Layout XML.
Then, in the Java Source (usually the onCreate function), enter the following.
Changing the Font of an Android TextView
| |
To change it through the TextView attributes:
To change it from an asset, apply the following.
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);
How to draw a Stroke TextView
- Making a Stroke TextView
- Android textview outline text
- How do you draw text with a border on a MapView in Android?
Add the following attributes to 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>
Then override 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);
}
}
Finally, apply the Widget in the Layout.
<~.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" />
One important point: the XML Namesapce must be applied as follows.
Favorite site
- Shadow effect
- Handling TextView strings: how to use Spannable
- Setting font size according to TextView settings
- Automatically resizing the String (TEXT) of an Android TextView