Set style for TextView programmatically

I do not believe you can set the style programatically. To get around this you can create a template layout xml file with the style assigned, for example in res/layout create tvtemplate.xml as with the following content: <?xml version=”1.0″ encoding=”utf-8″?> <TextView xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”This is a template” style=”@style/my_style” /> then inflate this to instantiate … Read more

How to change the font on the TextView?

First, the default is not Arial. The default is Droid Sans. Second, to change to a different built-in font, use android:typeface in layout XML or setTypeface() in Java. Third, there is no Helvetica font in Android. The built-in choices are Droid Sans (sans), Droid Sans Mono (monospace), and Droid Serif (serif). While you can bundle … Read more

Vertical (rotated) label in Android

Here is my elegant and simple vertical text implementation, extending TextView. This means that all standard styles of TextView may be used, because it is extended TextView. public class VerticalTextView extends TextView{ final boolean topDown; public VerticalTextView(Context context, AttributeSet attrs){ super(context, attrs); final int gravity = getGravity(); if(Gravity.isVertical(gravity) && (gravity&Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { setGravity((gravity&Gravity.HORIZONTAL_GRAVITY_MASK) | … Read more

How to set the text color of TextView in code?

You should use: holder.text.setTextColor(Color.RED); You can use various functions from the Color class to get the same effect of course. Color.parseColor (Manual) (like LEX uses) text.setTextColor(Color.parseColor(“#FFFFFF”)); Color.rgb and Color.argb (Manual rgb) (Manual argb) (like Ganapathy uses) holder.text.setTextColor(Color.rgb(200,0,0)); holder.text.setTextColor(Color.argb(0,200,0,0)); And of course, if you want to define your color in an XML file, you can do … Read more

Set color of TextView span in Android

Another answer would be very similar, but wouldn’t need to set the text of the TextView twice TextView TV = (TextView)findViewById(R.id.mytextview01); Spannable wordtoSpan = new SpannableString(“I know just how to whisper, And I know just how to cry,I know just where to find the answers”); wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); TV.setText(wordtoSpan);

How to adjust text font size to fit textview

The solution below incorporates all of the suggestions here. It starts with what was originally posted by Dunni. It uses a binary search like gjpc’s, but it is a bit more readable. It also include’s gregm’s bug fixes and a bug-fix of my own. import android.content.Context; import android.graphics.Paint; import android.util.AttributeSet; import android.util.TypedValue; import android.widget.TextView; public … Read more

How to set the part of the text view is clickable

android.text.style.ClickableSpan can solve your problem. SpannableString ss = new SpannableString(“Android is a Software stack”); ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View textView) { startActivity(new Intent(MyActivity.this, NextActivity.class)); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setUnderlineText(false); } }; ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); TextView textView = (TextView) findViewById(R.id.hello); textView.setText(ss); textView.setMovementMethod(LinkMovementMethod.getInstance()); textView.setHighlightColor(Color.TRANSPARENT); In XML: <TextView … Read more