I am creating a Gallery application. Each image will show the title along with the name of the person who uploaded the image. Example of what I want is below:
Here the Night is dark, but this darkness has the silence is the title for the image, and the shaggy boy is the user who uploaded the image for example.
So, now I want Night is dark, but this darkness has the silence to be in the separate textview and the shaggy boy at the end of the Night is dark, but this darkness has the silence's textview even if Night is dark, but this darkness has the silence is a single line or multi-line.
I have used the Flexbox Layout but I am failed to achieve what I want.
How to append the textview to the end of another textview? Thanks!!!
You can use a single TextView and set its text String with a Spannable and match each portion of text with indices here I am separating both portions by a hyphen; but you can change that as you would like.
TextView textView = findViewById(...);
String tag = "Night is dark, but this darkness has the silence - shaggy boy";
SpannableString spannableString = new SpannableString(tag);
spannableString.setSpan(new ForegroundColorSpan(Color.BLACK), 0, tag.indexOf("-"), 0);
spannableString.setSpan(new ForegroundColorSpan(Color.GRAY), tag.indexOf("-"), tag.length(), 0);
textView.setText(spannableString);
Maybe you could just get the text of the author's TextView and add it to the String of the first TextView?
You can use a single TextView with String palceholders of the quote and the author, and add the colors using HTML tags.
strings.xml
<resources>
<string name="tag">
<![CDATA[
<font color="#00000">%1$s</font> -
<font color="#D3D3D3">%2$s</font>
]]>
</string>
</resources>
Behavior:
String message = "Night is dark, but this darkness has the silence";
String name = "shaggy boy";
String formattedTag = String.format(getString(R.string.tag), message, name);
textview.setText(Html.fromHtml(formattedTag));
Related
I am using SpannableString to insert emotion icons into EditText.
The following four cases are all OK:
(1) append text or emotion icon at the end of EditText,
(2) insert emotion icon between text,
(3) insert emotion icon between existed emotion icons.
(4) insert text between existed text.
But the problem is I cannot insert text between those icons. Here is my code and logcat info.
(1) Add icon to EditText:
Drawable d = UiUtil.getCachedDrawable(mActivity, EmResource.findResIdByTag(value));
if (d != null) {
d.setBounds(0, 0, w, w);
String str = "<img src='" + value + "'/>";
SpannableString ss = new SpannableString(str);
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);
ss.setSpan(span, 0, str.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
contentEt.getText().replace(contentEt.getSelectionStart(), contentEt.getSelectionEnd(), ss);
}
(2) After inputing some icons, it looks like this (I have moved the cursor manualy after typing):
I have added a TextWatcher for this EditText. Log info from TextWatcher:
<img src='115'/><img src='116'/><img src='117'/><img src='118'/>
(3) Then I press 'y' on the keyboard (in the position of previous screen shot). After that the cursor is moving but the text isn't shown. Like the following screen shot:
But the log info shows that the text is inserted to EditText successfully.
<img src='115'/><img src='116'/>y<img src='117'/><img src='118'/>
<img src='115'/><img src='116'/>yy<img src='117'/><img src='118'/>
<img src='115'/><img src='116'/>yyy<img src='117'/><img src='118'/>
Any suggestions? Thanks a lot.
Change Spannable.SPAN_INCLUSIVE_EXCLUSIVE to Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
Hope it helps.
As SpannableString content is immutable, it might not allow you to edit between two emojis, Instead of that try with SpannableStringBuilder whose content and markup can both be changed. Hope it will help
in Android I would like to concatenate number with Arabic text. but if i concatenate the number then the arabic text direction changes.
Example: My Application reads each line of arabic text from file and adds the line number to each line and appended to string builder , After completion of reading final string will be displayed on text view.
Here is my code
SpannableStringBuilder styledString = new SpannableStringBuilder(TextUtils.concat(l,"("+sn+")"));
styledString.setSpan (new CustomTypefaceSpan("", type), 0, l.length()-1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
// styledString.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE),l.length()-1,l.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannablestringbuilder.append(styledString);
Expected Output:
(3) ﺑِﺴْﻢِ ٱﻟﻠَّﻪِ ٱﻟﺮَّﺣْﻤَٰﻦِ ٱﻟﺮَّﺣِﻴِ(1)ٱﻟْﺤَﻤْﺪُ ﻟِﻠَّﻪِ ﺭَﺏِّ ٱﻟْﻌَٰﻠَﻤِﻴﻦَ (2) ٱﻟﺮَّﺣْﻤَٰﻦِ ٱﻟﺮَّﺣِﻴﻢِ
Actual Output
(1)ﺑِﺴْﻢِ ٱﻟﻠَّﻪِ ٱﻟﺮَّﺣْﻤَٰﻦِ ٱﻟﺮَّﺣِﻴﻢِ (2)ٱﻟْﺤَﻤْﺪُ ﻟِﻠَّﻪِ ﺭَﺏِّ ٱﻟْﻌَٰﻠَﻤِﻴﻦَ ٱﻟﺮَّﺣْﻤَٰﻦِ(3) ٱﻟﺮَّﺣِﻴﻢِ
Try
Removing spaces
Use hyphens - instead of spaces
Try finding a way to change the direction
By the way, this question is answerde here and might be a duplicate!
Thanks for posting!
I used to be able to do this:
<string name="foo">white <font fgcolor="#ff6890a5">blue</font></string>
But now it doesn't work any more. It seems to be a bug in the integer parsing code; see https://code.google.com/p/android/issues/detail?id=58192
Problem is, I'm getting customer complaints now; I can't wait for the bug to be fixed.
Does anybody know a work-around, such as using named resources from color.xml or something like that?
ETA: I've discovered fgcolor="blue" still works, but it's the wrong shade of blue. Is there a list of legal color names somewhere? Maybe I could find one that's close enough. It also works if the color is a number without the high bit set, like #7f6890a5, but of course that's too faint to be useful; I need a solid color, not semi-transparent.
ETA: browsing source code shows these colors:
aqua 0x00FFFF
black 0x000000
blue 0x0000FF
fuchsia 0xFF00FF
green 0x008000
grey 0x808080
lime 0x00FF00
maroon 0x800000
navy 0x000080
olive 0x808000
purple 0x800080
red 0xFF0000
silver 0xC0C0C0
teal 0x008080
white 0xFFFFFF
yellow 0xFFFF00
This doesn't fix my problem, but perhaps other people searching on this question could find this information useful.
How about we leverage the fact that colors without the high bit set still works and just replace it with the correct colors? So you can have a method like this:
private CharSequence fixSpanColor(CharSequence text) {
if (text instanceof Spanned) {
final SpannableString s = new SpannableString(text);
final ForegroundColorSpan[] spans = s.getSpans(0, s.length(), ForegroundColorSpan.class);
for (final ForegroundColorSpan oldSpan : spans) {
final ForegroundColorSpan newSpan = new ForegroundColorSpan(oldSpan.getForegroundColor() | 0xFF000000);
s.setSpan(newSpan, s.getSpanStart(oldSpan), s.getSpanEnd(oldSpan), s.getSpanFlags(oldSpan));
s.removeSpan(oldSpan);
}
return s;
} else {
return text;
}
}
You will then need to pass any text with the required color accent through this method, the simplest example would be modifying all calls like this:
tv.setText(getText(R.string.foo));
To:
tv.setText(fixSpanColor(getText(R.string.foo)));
Hopefully, depending on how your code is structured, there might already a central place where you can add this extra method call.
I have some awful workaround in my client code to manually reset the
ForegroundColorSpans to the proper color, but it would be great not to
have to do so.
I think the workaround that the issue reporter's talking about is the following:
Define the string as:
<string name="foo">white blue</string>
In your activity:
TextView tv = (TextView) findViewById(R.id.textView1);
SpannableString spannableString = new
SpannableString(getResources().getString(R.string.foo));
ForegroundColorSpan fcs = new
ForegroundColorSpan(getResources().getColor(R.color.bluish));
spannableString.setSpan(fcs, spannableString.toString().indexOf(" ") + 1,
spannableString.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
tv.setText(spannableString);
R.color.bluish is defined as <color name="bluish">#ff6890a5</color>.
But, using " " (space) to distinguish and apply the ForegroundColorSpan would only be practical if you have a small number of strings defined.
The following modification might actually be easier for you to carry out:
Define the string as:
<string name="foo_sep_1"><![CDATA[white <font color=\"#6890a5\">blue</font>]]></string>
Or:
<string name="foo_sep_1">white <font color="#6890a5">blue</font></string>
In your activity:
TextView tv2 = (TextView) findViewById(R.id.textView2);
tv2.setText(Html.fromHtml(getResources().getString(R.string.foo_sep_1)));
Be careful about the color code: HTML color codes do not have alpha values (RRGGBB will work, AARRGGBB will not)
Another workaround is using Html.fromHtml(String) directly:
TextView tv3 = (TextView) findViewById(R.id.textView3);
tv3.setText(Html.fromHtml("white <font color='#6890a5'>blue</font>"));
Kudos to TWiStErRob who found a solution that doesn't involve code in https://stackoverflow.com/a/11577658/338479. To quote:
for any color above 7fffffff apply the following: <font
color="#ff6890a5"> put ff6890a5 into a calculator (optionally convert
to decimal first) and flip the sign, then (optionally convert back to
hexa) take the last 8 hexadecimal digits and use <font
color="-#00976F5B">.
i have used many punjabi fonts as like bulara_f.ttf , anmollipi , Akaash and many more but every time prolem with one word of punjabi "sihari"
Sihari : Sihari is equivalent to i in pin or thin. Hence forth in this module we will denote sihari vowel with /i/.
in my application only this one word is not shown properly as like in following snapshot... please help...
EDIT
i am using this method
Typeface myfont;
myfont=Typeface.createFromAsset(getAssets(),"font.ttf");
TextView textview = (TextView)findViewById(R.id.TextView01);
//textview.setText("punjabi Text");
textview.setTypeface(myfont);
}
Use bulara_5.ttf and run the code on device it will show correct punjabi words
mTvAbout = (TextView) findViewById(R.id.tv_about);
font = Typeface.createFromAsset(this.getAssets(), "bulara_5.ttf");
mTvAbout.setTypeface(font);
mTvAbout.setText(" ਜਾਣਕਾਰੀ");
i am going to set mix text on edittext like:Xyz Pvt Ltd فاتورة المبيعات مبيعات التسليم <0x09> Choclate Cake 10 x30.000 = 300.000
Acualy i had set text like this but arbic text chage it Xyz Pvt Ltd ف اتورة المبيعات مبيعات التسليم -Choclate Cake 10 x30.000 = 300.000
As you can see in first line after arbick text i had append <0X09> but it will append before arbick text that's proble
You can try to convert the unicode to arabic type with a convertor online and then setText the view with arabic font. For tamil I had used TAB font so I downloaded the tab font and had put it in my assets folder and then in setTypeFace I used the font. You can try the arabic encoded string and the font.
String EncodedString = "Üî¢î¤ò£òñ¢ : "; // Your arab encoded string
Typeface tfTab = Typeface.createFromAsset(getAssets(),"fonts/tabkovai.ttf"); // Replace with arab font
tab.setTypeface(tfTab);
TextView.setText(EncodedString);