I want to give style to text while typing in edittext, for example make text bold or italic. I know how do this after typing and selecting part of text with help of spannable, but I want to do this while typing. For example after clicking a bold button, everything I type be bold. Can anyone give a hint to me how do this?
you can try to do this in code like this:
TextView textView=(TextView)findViewById(R.id.your_textView_id);
SpannableString spanString = new SpannableString(tempString);
spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0, spanString.length(), 0);
textView.setText(spanString);
you can try these in on click listener of a button to change an edittext content and after that invalidate it
You can do:
((EditText) mView.findViewById(R.id.your_edittext)).addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
by this any time during the texting you can do it
I had the same requirement. I used addTextChangedListener and styled text using Html.fromHtml() in onTextChanged method, also removed textChangeListener and added it back once text was styled, just to prevent infinite loop
binding.descriptionEditText.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
Log.d(TAG, "beforeTextChanged: $s")
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
Log.d(TAG, "onTextChanged: called $s")
binding.descriptionEditText.removeTextChangedListener(this)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
binding.descriptionEditText.setText(
Html.fromHtml(
s.toString(),
HtmlCompat.FROM_HTML_MODE_LEGACY
)
)
} else {
binding.descriptionEditText.setText(
Html.fromHtml(
s.toString()
)
)
}
//to bring cursor back to the end
binding.descriptionEditText.setSelection(binding.descriptionEditText.text.length)
binding.descriptionEditText.addTextChangedListener(this)
}
override fun afterTextChanged(s: Editable?) {
}
})
It sort of works but if we type too fast or delete any content too fast, it lags. I found this blog it looks quite complex but seems promising. I haven't implemented it yet.
Just use android:textStyle="bold"
or "italic"
as per your requirements and its done. And if you want to make it bold or italic after pressing some button. Then make a method in which you mention something like edittext.setOnClickListener
and inside that use the id of the edit text and use the edittext.setTypeface(null, Typeface.BOLD);
attributes
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.