How can I change some of statements' text style in Firebase? - android

I'm trying to show some statements to users from Firebase. There are titles and contents in these statements. Trying to write titles with a boldly styled post. However, I cannot change the code from the android studio because I've assigned the whole description child to a single TextView. instead, I used expressions such as "\ n", which is also used in string expressions. Is it possible to write bold headlines? if so how?
For example ; here is my
Can I write "ilk olarak bulunduğunuz federal devletteki bir..." here by a bold style?

yes you can display, the approach i use is setting html from firebase
<p><b>This text is bold</b></p>
and in android side
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
tvDesc.setText(Html.fromHtml(strfromfirebase, Html.FROM_HTML_MODE_COMPACT));
} else {
tvDesc.setText(Html.fromHtml(strfromfirebase));
}

You need to use a SpannableString in this case.
For example if I want to bold the word "Hello" in "Hello World", I would do something like:
String word = "Hello World";
SpannableString spannableString = new SpannableString(word);
spannableString.setSpan(new StyleSpan(Typeface.BOLD),0,5,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
0 is the starting index and 5 is the ending index

Related

How could I interchange SpannableString to String without losing it's marked up properties?

If i have the following string:
String string = “My string”
Then i could change it’s color background using SpanString:
SpannableString spannableString = new SpannableString(string);
BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.RED);
spannableString.setSpan(backgroundSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Then if i make
Toast(context , spannableString , Toast.LENGHT_SHORT).show();
It will display a toast “My string” with changed background to red.
However , how can I show the same string without using the type SpannableString??
I mean Toast(context , [toString] spannableString , Toast.LENGHT_SHORT).show();
[toString]: my note
PS:I've tried
Toast.makeText(context, Html.fromHtml(spannableString.toString()), Toast.LENGTH_SHORT).show();
and
Toast.makeText(context, spannableString.toString(), Toast.LENGTH_SHORT).show();
because i need to attribute this to a kind of:
entity.setAttribute([toString]Spannable);
but not showing commom text.
There is no way without using Spannables. Spannables are how you add formatting data to text in Android. Even your fromHTML hack turns html into a spannable.
The correct thing to do is probably take whatever API you're using that requires a string and make it take a CharSequence instead.

How to get this special space character ␣ into an Android Button?

I want to symbolize a space in an Android Button so:
in the strings.xml file I define <string name="SpaceSymbol">␣</string>
in the layout XML file I set android:text to "#string/SpaceSymbol"
When I run the application the button shows nothing.
Any ideas?
If any other character that symbolizes a space works other than ␣ I'll be glad to use it
Use the Html code corresponding to ␣ character, HTML Codes Table or HTML Special Characters
the define into the strings.xml
<string name="SpaceSymbol">␣</string>
This is an example with & character:
How can I write character & in android strings.xml
Update:
Answering your question, ␣ is non-printable character, it is just a "Space", if you want show this symbol into the button, try loading an image of that symbol into an ImageButton.
You can wrap the space char in
![CDATA[ ... ]] in the string resource..that may help you bring it in. You should check that the character is in the roboto font also
Given that the character is not represented in the Roboto font, you can use a SpannableString to replace the special character with your own image:
you should create a Drawable (or Bitmap) with the space image in it that you want to use. in this example it would be assigned to the variable 'drawable':
SpannableString textspan = new SpannableString(sb);
Pattern pattern = Pattern.compile("#_", Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(textspan);
while(matcher.find()) {
textspan.setSpan(new ImageSpan(this, drawable, ImageSpan.ALIGN_BASELINE), matcher.start(), matcher.end(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
In your string (in this code example) you would have replaced your space symbol with the two characters "#_". The pattern will find each occurence of "#_" and replace it with the drawable.
Then you can use the SpannableString as a CharSequence argument anywhere it's defined (Toast.makeText, TextView.setText, etc)
like this :
TextView tv = (TextView) findViewById(..);
tv.setText(textspan);

How can I capture the formatting of my EditText text, so that bold words show as bold next time I display them in a TextView

Edit:
my app allows users to post comments for all other users to see. I want to allow users to make certain words in their comment bold or italic etc, so that when they post a comment other people will also be able to see the bold words etc.
At the moment i am able to make the words bold but when the text is saved to my remote database server it is saved as normal text and i am unable to find out which words were bold and which arn't.
How can I preserve the bold/italic/underlined... formatting when saving the text from an EditText?
There is one way to achieve this.
You can store HTML content and display HTML content in TextView.
Just look at this Answer.
Here is Sample link.
Quite easily.
Use EditText.getText to get the text, which implements Spanned interface.
Then you need to somehow mark bold parts within text, for this you may use good old HTML format, which means that you'll end up with text (storable in database) with some formatting.
Use Html.toHtml to turn Spanned to (HTML) String. Save result to DB or where you need.
Reverse process is to get your HTML String and convert it to Spanned, then set it bact to EditText. Use Html.fromHtml to accomplish this.
final SpannableStringBuilder sb = new SpannableStringBuilder("Text from the edit Text");
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
final StyleSpan iss = new StyleSpan(android.graphics.Typeface.ITALIC);//Span to make text italic
sb.setSpan(bss, 0, 10, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 10 characters Bold
sb.setSpan(iss, 10, 20, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make last 10 characters Italic
//Set text or save it in db
etx.setText(sb);

how to add text with format into edit text?

The image is from an app called kakao story.
Suppose there's a post with a list of comments like any sns apps.
When you click a comment, it inserts the user name of the commenter in the edit-text to indicate my new comment is a reply to the user.
(You can't add the same name more than once.)
When you hit backspace to delete the name, the entire characters that make up the name(e.g., chabeau in the example) will be deleted by 1-backspace.
I'm trying to mimic the behavior and want some pointers how to implement it or what to search for.
If you are in search of bubble view. You can achieve it by creating a subclass of android.text.style.DynamicDrawableSpan.ImageSpan which will convert a portion of EditText string into formatted span.
This SO Question will give you some basic idea about creating formatted span.
This is a good tutorial for customizing editext with spans.
And for deleting whole word at once, you can use SPAN_EXCLUSIVE_EXCLUSIVE property.
Below code will format the first four character of the string, Hope this will give you some hint.
final SpannableStringBuilder sb = new SpannableStringBuilder("your text here");
final ForegroundColorSpan fcs
= new ForegroundColorSpan(Color.rgb(158, 158, 158));
// Span to set text color to some RGB value
sb.setSpan(fcs, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(sb);
EditText et = (EditText) findViewById(R.id.edit1);
et.setTextColor(Color.parseColor("yourColorCodeHere"));

Specifying "strikethrough" on a section of TextView text

I have a block of text coming from a webservice, and depending on some tags which I have predefined, I want to style the text before setting it to my TextView. For bold, italics, and underline, I was able to do this easily with the replaceAll command:
PageText = PageText.replaceAll("\\*([a-zA-Z0-9]+)\\*", "<b>$1</b>");
PageText = PageText.replaceAll("=([a-zA-Z0-9]+)=", "<i>$1</i>");
PageText = PageText.replaceAll("_([a-zA-Z0-9]+)_", "<u>$1</u>");
txtPage.setText(Html.fromHtml(PageText), TextView.BufferType.SPANNABLE);
So, to bold a word, surround it with *'s, for italics, surround with _.
But, for strikethrough, Html.fromHtml does not support the "strike" tag, so it can't be done this same way. I've seen examples of using Spannable to set the styling on one section of text, but it requires positional numbers. So, I guess I could loop through the text, searching for - (the tag to represent the strike), then searching for the next one, spanning the text in between, and repeating for all such strings. It will end up being 10 lines of looping code as opposed to 1 for the others, so I'm wondering if there is a more elegant solution out there.
If it is just TextView you can strike through using paint flags
TextView tv=(TextView) v.findViewById(android.R.id.text1);
tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
#Suresh solution works if you want to strikethrough the entire TextView but if you want to strikethrough only some portions of the text then use the code below.
tvMRP.setText(text, TextView.BufferType.SPANNABLE);
Spannable spannable = (Spannable) tvMRP.getText();
spannable.setSpan(new StrikethroughSpan(), 3, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Here text is the text which we want out TextView to display, 3 is the no. of characters (starting from 0) from where the strikethrough will start.
You can do it with a custom TagHandler such as the one on this SO question:
Spanned parsed = Html.fromHtml(PageText, null, new MyHtmlTagHandler());
And the TagHandler implements the methods:
public void handleTag(boolean opening, String tag, Editable output,
XMLReader xmlReader) {
if(tag.equalsIgnoreCase("strike") || tag.equals("s")) {
processStrike(opening, output);
}
}
....
Are you sure Html.fromHtml doesn't support <strike>? It's listed in this Commonsware blog post
It looks like is not really supported, at least it does not work on Android 3.1.
#RMS2 if text is small you can split it into two or three separate text views and apply flag only to the one which you want, not perfect for long texts ;(
Most of the applications we work in are going to use text somewhere throughout the project and thankfully, KTX provides some extension functions when it comes to these parts. For text, we essentially have some functions available for the SpannableStringBuilder class.
For example, after instantiating a Builder instance we can use the build methods to append some bold text:
textView.text =buildSpannedString {
strikeThrough {
append(
value ?: ""
)
}
}

Categories

Resources