Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically change the text size in Android

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,

enter image description here

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?

like image 906
Ban Lin Avatar asked Oct 30 '15 08:10

Ban Lin


People also ask

How do you change text size on android?

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.

Should I use SP or DP android?

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.

How do you change text to dynamic in Java?

Looks like you want to change size of selected text: int start = editText. getSelectionStart(); int end = editText. getSelectionEnd(); Spannable text=(Spannable)editText.

How do I stop system font size changing effects to android application?

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.


1 Answers

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.

like image 101
lewkka Avatar answered Oct 03 '22 07:10

lewkka