Add styles to a TextView
suggest changeIn the following example, we create an Activity to display a single TextView.
The TextView will use a SpannableString
as its content, which will illustrate some of the available styles.
Here’ what we’re gonna do with the text :
- Make it larger
- Bold
- Underline
- Italicize
- Strike-through
- Colored
- Highlighted
- Show as superscript
- Show as subscript
- Show as a link
- Make it clickable.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SpannableString styledString
= new SpannableString("Large\n\n" // index 0 - 5
+ "Bold\n\n" // index 7 - 11
+ "Underlined\n\n" // index 13 - 23
+ "Italic\n\n" // index 25 - 31
+ "Strikethrough\n\n" // index 33 - 46
+ "Colored\n\n" // index 48 - 55
+ "Highlighted\n\n" // index 57 - 68
+ "K Superscript\n\n" // "Superscript" index 72 - 83
+ "K Subscript\n\n" // "Subscript" index 87 - 96
+ "Url\n\n" // index 98 - 101
+ "Clickable\n\n"); // index 103 - 112
// make the text twice as large
styledString.setSpan(new RelativeSizeSpan(2f), 0, 5, 0);
// make text bold
styledString.setSpan(new StyleSpan(Typeface.BOLD), 7, 11, 0);
// underline text
styledString.setSpan(new UnderlineSpan(), 13, 23, 0);
// make text italic
styledString.setSpan(new StyleSpan(Typeface.ITALIC), 25, 31, 0);
styledString.setSpan(new StrikethroughSpan(), 33, 46, 0);
// change text color
styledString.setSpan(new ForegroundColorSpan(Color.GREEN), 48, 55, 0);
// highlight text
styledString.setSpan(new BackgroundColorSpan(Color.CYAN), 57, 68, 0);
// superscript
styledString.setSpan(new SuperscriptSpan(), 72, 83, 0);
// make the superscript text smaller
styledString.setSpan(new RelativeSizeSpan(0.5f), 72, 83, 0);
// subscript
styledString.setSpan(new SubscriptSpan(), 87, 96, 0);
// make the subscript text smaller
styledString.setSpan(new RelativeSizeSpan(0.5f), 87, 96, 0);
// url
styledString.setSpan(new URLSpan("http://www.google.com"), 98, 101, 0);
// clickable text
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
// We display a Toast. You could do anything you want here.
Toast.makeText(SpanExample.this, "Clicked", Toast.LENGTH_SHORT).show();
}
};
styledString.setSpan(clickableSpan, 103, 112, 0);
// Give the styled string to a TextView
TextView textView = new TextView(this);
// this step is mandated for the url and clickable styles.
textView.setMovementMethod(LinkMovementMethod.getInstance());
// make it neat
textView.setGravity(Gravity.CENTER);
textView.setBackgroundColor(Color.WHITE);
textView.setText(styledString);
setContentView(textView);
}
And the result will look like this:

Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents
2 Layouts
3 Gradle
6 Intent
11 Exceptions
13 AsyncTask
15 Emulator
18 Service
20 WebView
22 RecyclerView
23 Google Maps
26 Android NDK
28 Camera 2 API
29 ViewPager
30 CardView
32 SQLite
34 ButterKnife
36 Glide
37 Retrofit2
38 Dialog
39 ACRA
40 GreenDAO
43 AlarmManager
44 Fragments
45 Handler
48 Activity
49 Snackbar
54 Toast
55 Interfaces
56 Animators
57 Location
60 Parcelable
61 MediaPlayer
64 Menu
66 Picasso
68 RoboGuice
69 Memory Leaks
71 Volley
72 Widgets
78 Dagger 2
79 Realm
85 ProgressBar
86 Custom Fonts
87 Vibration
90 UI Lifecycle
91 Spinner
96 OkHttp
100 Firebase
103 Facebook SDK
104 Unzip File
109 TextView
110 ListView
112 Loader
117 MVP Architecture
119 Xposed
120 Security
121 PackageManager
122 ImageView
124 Doze Mode
126 SearchView
128 Callback URL
129 Twitter APIs
131 Drawables
132 Colors
133 ConstraintLayout
134 RenderScript
135 Fresco
136 Swipe to Refresh
139 IntentService
140 AdMob
141 Implicit Intents
145 Email Validation
146 Keyboard
147 Button
148 TextInputLayout
149 Bottom Sheets
151 EditText
156 Vk SDK
158 Count Down Timer
160 Otto Event Bus
164 ExoPlayer
166 MediaSession
168 FileProvider
170 XMPP
171 Authenticator
173 AudioManager
174 Job Scheduling
176 OpenCV
178 Threads
179 MediaStore
180 Time Utils
181 Touch Events
182 Fingerprint API
185 ORMLite
186 YouTube API
187 TabLayout
190 ShortcutManager
191 LruCache
192 Using Jenkins CI
193 Zip files
194 Vector Drawables
195 Fastlane
200 FileIO
202 Robolectric
203 Moshi
206 Retrolambda
207 SparseArray
210 Android Things
211 VideoView
212 ViewFlipper
217 Paint
218 AudioTrack
219 ProGuard
221 Java on Android
226 ConstraintSet
227 CleverTap
229 ADB shell
230 Ping ICMP
231 AIDL
232 Using Kotlin
235 Context
239 Bitmap Cache
241 JCodec
242 Design Patterns
243 Okio
245 TensorFlow
246 Game developmen
249 Leak Canary
250 FuseView
254 SpannableString
255 Looper
257 Google Drive API
262 Fastjson
264 Jackson
265 Play Store
268 Smartcard
270 Contributors