I want to implement a function,
EditText user when entering text, you can make changes in accordance with the set font size,
Such as Google Docs of Office,
Now I found a way to SpannableString
, but read some examples seem unable to reach my needs
int index = input.getSelectionEnd();
SpannableString spann = new SpannableString(show_input);
AbsoluteSizeSpan word_size = new AbsoluteSizeSpan(18,true);
spann.setSpan(word_size, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
input.getText().insert(index, spann);
So is there any way to provide it?
To make your font size smaller or larger: On your device, open the Settings app. Search and select Font size. To change your preferred font size, move the slider left or right.
When setting text sizes, you should normally use sp , or "scale-independent pixels". This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.
Looks like you want to change size of selected text: int start = editText. getSelectionStart(); int end = editText. getSelectionEnd(); Spannable text=(Spannable)editText.
Prevent-system-font-size-changing-effects-to-android cs, override the Resources and set the configuration to default to restrict the font size effect on application. Resources. UpdateConfiguration() has been deprecated in API 25. So CreateConfigurationContext is used for APl level greater than 25.
Looks like you want to change size of selected text:
int start = editText.getSelectionStart();
int end = editText.getSelectionEnd();
Spannable text=(Spannable)editText.getText();
text.setSpan(new AbsoluteSizeSpan(NEW_TEXT_SIZE, true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setText(text);
Use AbsoluteSizeSpan to change for actual size or RelativeSizeSpan for proportional size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With