Taking a char symbol by code in Android - android

I want to view all codes and symbols in custom font which I have loaded in assets of Android JDK. How do I do this? Thank you for helping.

You should create a TextView, add the characters that you want, then set the font of the TextView.
See here and here for changing fonts in a TextView.
Something like this should work to print out the characters:
StringBuilder sb = new StringBuilder();
// Loop over readable ASCII characters 32 to 127 as per http://www.asciitable.com/
for (int i = 32; i < 127; i++) {
sb.append((char) i);
}
TextView tv = (TextView) findViewByID(yourID);
tv.setText(sb.toString());

Related

Android edit/insert into string

I was wondering how I could programmatically edit strings in android. I am displaying strings from my device to my website, and the apostrophes ruin the PHP output. so in order to fix this, I needed to add character breaks, ie: the backslash '\'.
For example, if I have this string: I love filiberto's!
I need android to edit it to: I love filiberto\'s!
However, each string is going to be different, and there will also be other characters that I have to escape from . How can I do this?
I was wondering how I could programmatically edit strings in android. I am displaying strings from my device to my website, and the apostrophes ruin the PHP output. so in order to fix this, I needed to add character breaks, ie: the backslash '\'.
This is what I have so far, thanks to ANJ for base code...:
if(title.contains("'")) {
int i;
int len = title.length();
char[] temp = new char[len + 1]; //plus one because gotta add new
int k = title.indexOf("'"); //location of apostrophe
for (i = 0; i < k; i++) { //all the letters before the apostrophe
temp[i] = title.charAt(i); //assign letters to array based on index
}
temp[k] = 'L'; // the L is for testing purposes
for (i = k+1; i == len; i++) { //all the letters after apostrophe, to end
temp[i] = title.charAt(i); //finish the original string, same array
}
title = temp.toString(); //output array to string (?)
Log.d("this is", title); //outputs gibberish
}
Which outputs random characters.. not even similar to my starting string. Does anyone know what could be causing this? For example, the string "Lol'ok" turns into >> "%5BC%4042ed0380"
I am assuming you are storing the string somewhere. Lets say the string is: str.
You can use a temporary array to add the '/'. For a single string:
int len = str.length();
char [] temp = new char[len+1]; //Temporary Array
int k = str.indexOf("'"), i; //Finding index of "'"
for(i=0; i<k-1; i++)
{
temp[i] = str.charAt(i); //Copying the string before '
}
temp[k] = '/'; //Placing "/" before '
for(i=k; j<len; j++)
{
temp[i+1] = str.charAt(i); //Copying rest of the string
}
String newstr = temp.toString(); //Converting array to string
You can use the same for multiple strings. Just make it as a function and call it whenever you want.
The String API has a number of API calls that could help, for example String.replaceAll. But...
apostrophes ruin the PHP output
Then fix the PHP code rather than require "clean" input. Best option would be to select a well supported transport format (say JSON or XML) and let the Json API on each end handle escape code.

How to display Html table in textview?

I want to display text inside textview using this code:
Html.fromHtml("<html><body><table style=width:100%><tr><td><B>No</td><td><B>Product Name</td><td><B>Qty</td><td><B>Amount</td></tr></body></html>");
But result is not in correct format result look like this:
NoPRoductNameQtyAmount
please suggest what i am doing wrong in this code.
fromHtml() does not support <table> and related tags. Your choices are:
Reformat your text to avoid tables
Use WebView to render your HTML table
Use native widgets and containers (e.g., TableLayout) for your table
Instead of using html table inside a TextView I've solved formatting normal text into a table like text, adding white spaces into the text to have a tidy structure.
This is the code:
String lines[] = getItem(position).toString().split("\n");
String print = "";
String parts[] = null;
String tmp = "";
int weight = 0;
for (int i=0; i < lines.length; i++) {
if (!lines[i].equalsIgnoreCase("")) {
parts = lines[i].split(":", 2);
tmp = "";
Paint textPaint = text.getPaint();
float width = textPaint.measureText(parts[0]);
float wslength = textPaint.measureText(" ");
for (int j = 0; j < (220 - Math.round(width))/Math.round(wslength); j++) {
tmp = tmp + " ";
}
if (print.equalsIgnoreCase("")) {
print = print + parts[0] + tmp + Html.fromHtml(""+parts[1]+"");
} else {
print = print + "\n" + parts[0] + tmp + parts[1];
}
}
}
You can find more explanations here:
http://blog.blupixelit.eu/convert-text-to-table-in-android-sdk/
Hope this helps.
How about change the way to just render table by webview, the other tags render by textview ? Recently i finish a demo to overcome this. So we first need separate<table> with others supported tags, which will like change "<p>**<p> blabala <table>balabla </table> blabla <p>**<p>" to three separated strings
<p>**<p> blabala
<table>balabla </table>
blabla <p>**<p>
Then only <table> included tags render by webview, the others by textview
And the result in android will be like:
<ScrollView>
<LinearLayout>
<TextView>
<WebView> --- let the webview ATMOST measured
<TextView>
So every thing goes fine as i think. check this commit for detail
HtmlTextView recently added basic support for HTML tables. It's limited, but will do the trick if all you have to worry about is <table>, <td>, <tr>, and <th>.

Custom fonts for TextView based on languages inside String

I have two font ttf files that must be applied on a TextView based on languages inside String. So e.g. consider this sample text:
hey what's up ضعيف
I can just apply a typeface span based on language but it requires custom markup in every string that is fetched from our server e.g.
<ttf1>hey what's up <ttf1><ttf2>ضعيف</ttf2>
And parsing every String at run time will give a performance hit. Is there any other approach to achieve this?
For start lets say I need to do this just for direction of text i.e. RTL and LTR so in above example English is LTR and Arabic is RTL. Will this be any different?
I have tried merging those two font files but there are line height issues and if I fix it for one font file it gets broken for other file.
I found a more elegant solution than manual markup with help of someone:
String paragraph = "hey what's up ضعيف";
int NO_FLAG = 0;
Bidi bidi = new Bidi(paragraph, NO_FLAG);
int runCount = bidi.getRunCount();
for (int i = 0; i < runCount; i++) {
String ltrtl = bidi.getRunLevel(i) % 2 == 0 ? "ltr" : "rtl";
String subString = paragraph.substring(bidi.getRunStart(i), bidi.getRunLimit(i));
Log.d(">>bidi:" + i, subString+" is "+ltrtl);
}
prints:
hey what's up is ltr
ضعيف is rtl
So now one can easily build TypefaceSpan or MetricAffectingSpan based on language direction like this:
SpannableString spanString = new SpannableString(paragraph);
for (int i = 0; i < runCount; i++) {
Object span = bidi.getRunLevel(i) % 2 == 0 ? ltrFontSpan : rtlFontSpan;
spanString.setSpan(span, bidi.getRunStart(i), bidi.getRunLimit(i), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
textView.setText(spanString);

Small Caps on TextViews, EditTexts, and Buttons in Android

Is there something I can do to make the text look in small caps/capital? As described here: http://en.wikipedia.org/wiki/Small_caps. I used a converter but some characters are missing.
EDIT 2015-08-02: As of API 21 (Lollipop) you can simply add:
android:fontFeatureSettings="smcp"
to your TextView declaration in XML, or at runtime, invoke:
textView.setFontFeatureSettings("smcp");
Of course, this only works for API 21 and up, so you'd still have to handle the old solution manually until you are only supporting Lollipop and above.
Being a bit of a typography geek at heart, this seemed like a really good question. I got to learn some more about Unicode today, as well as an answer for your question. :)
First, you'll need to have a font that includes "actual" small-caps characters. I'm assuming you know that since you're asking, but typically most professional fonts include these. Unfortunately most professional fonts are not licensed for distribution, so you may not be able to use them in your application. Anyway, in the event that you do find one (I used Chaparral Pro as an example here), this is how you can get small caps.
From this answer I found that the small caps characters (for A-Z) are located starting at Unicode-UF761. So I built a mapping of these characters:
private static char[] smallCaps = new char[]
{
'\uf761', //A
'\uf762',
'\uf763',
'\uf764',
'\uf765',
'\uf766',
'\uf767',
'\uf768',
'\uf769',
'\uf76A',
'\uf76B',
'\uf76C',
'\uf76D',
'\uf76E',
'\uf76F',
'\uf770',
'\uf771',
'\uf772',
'\uf773',
'\uf774',
'\uf775',
'\uf776',
'\uf777',
'\uf778',
'\uf779',
'\uf77A' //Z
};
Then added a helper method to convert an input string to one whose lowercase letters have been replaced by their Small Caps equivalents:
private static String getSmallCapsString (String input) {
char[] chars = input.toCharArray();
for(int i = 0; i < chars.length; i++) {
if(chars[i] >= 'a' && chars[i] <= 'z') {
chars[i] = smallCaps[chars[i] - 'a'];
}
}
return String.valueOf(chars);
}
Then just use that anywhere:
String regularCase = "The quick brown fox jumps over the lazy dog.";
textView.setText(getSmallCapsString(regularCase));
For which I got the following result:
Apologies for dragging up a very old question.
I liked #kcoppock's approach to this, but unfortunately the font I'm using is missing the small-cap characters. I suspect many others will find themselves in this situation.
That inspired me to write a little util method that will take a mixed-case string (e.g. Small Caps) and create a formatted spannable string that looks like Sᴍᴀʟʟ Cᴀᴘs but only uses the standard A-Z characters.
It works with any font that has the A-Z characters - nothing special required
It is easily useable in a TextView (or any other text-based view, for that matter)
It doesn't require any HTML
It doesn't require any editing of your original strings
I've posed the code here: https://gist.github.com/markormesher/3e912622d339af01d24e
Found an alternative here Is it possible to have multiple styles inside a TextView?
Basically you can use html tags formatting the size of the characters and give a small caps effect....
Just call this getSmallCaps(text) function:
public SpannableStringBuilder getSmallCaps(String text) {
text = text.toUpperCase();
text = text.trim();
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
if (text.contains(" ")) {
String[] arr = text.split(" ");
for (int i = 0; i < arr.length; i++) {
spannableStringBuilder.append(getSpannableStringSmallCaps(arr[i]));
spannableStringBuilder.append(" ");
}
} else {
spannableStringBuilder=getSpannableStringSmallCaps(text);
}
return spannableStringBuilder;
}
public SpannableStringBuilder getSpannableStringSmallCaps(String text) {
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(
text);
spannableStringBuilder.setSpan(new AbsoluteSizeSpan(36), 0, 1, 0);
spannableStringBuilder.setSpan(new StyleSpan(Typeface.BOLD), 0, 1, 0);
spannableStringBuilder.setSpan(new StyleSpan(Typeface.BOLD), 1,
text.length(), 0);
return spannableStringBuilder;
}
This is not my code but its works perfectly.
public SpannableString getSmallCapsString(String input) {
// values needed to record start/end points of blocks of lowercase letters
char[] chars = input.toCharArray();
int currentBlock = 0;
int[] blockStarts = new int[chars.length];
int[] blockEnds = new int[chars.length];
boolean blockOpen = false;
// record where blocks of lowercase letters start/end
for (int i = 0; i < chars.length; ++i) {
char c = chars[i];
if (c >= 'a' && c <= 'z') {
if (!blockOpen) {
blockOpen = true;
blockStarts[currentBlock] = i;
}
// replace with uppercase letters
chars[i] = (char) (c - 'a' + '\u0041');
} else {
if (blockOpen) {
blockOpen = false;
blockEnds[currentBlock] = i;
++currentBlock;
}
}
}
// add the string end, in case the last character is a lowercase letter
blockEnds[currentBlock] = chars.length;
// shrink the blocks found above
SpannableString output = new SpannableString(String.valueOf(chars));
for (int i = 0; i < Math.min(blockStarts.length, blockEnds.length); ++i) {
output.setSpan(new RelativeSizeSpan(0.8f), blockStarts[i], blockEnds[i], Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
return output;
}
Example:
SpannableString setStringObj = getSmallCapsStringTwo("Object"); tvObj.setText(setStringObj);
in XML
edit text has property :android:capitalize=""

In Android, how do I get the text on a button to descend vertically?

I am trying to get the text within my buttons to descend vertically like below. I'd prefer to have this happen within the main.xml file.
M
Y
T
E
X
T
I don't want the text to go sideways.
Use "\n" between the letters, that tells the compiler to add a return character between the characters.
A little helper function for this (i have no better idea, then adding the newlines):
public static final String rotateString(String str)
{
final StringBuilder sb = new StringBuilder();
for(int i=0; i<str.length(); i++)
sb.append(str.charAt(i)).append(i==str.length()-1? "" :'\n');
return sb.toString();
}
And then if you want to use it:
yourTextView.setText(rotateString("Hello Text"));
At the moment i could only test it on an emulator with 4.0.3, but it seems to work nicely.

Categories

Resources