I have used one textview and value set for it is like ex:11,234.45 and after decimal values like 45 should be shown with reduced front like 12sp and different colour like grey and 11,234 with black colour with front 16sp. how to achieve this using single textview in android?
You can achieve that if you follow this steps
First you need to split you string into 2 strings with this code lines
String s ="11,234.45";
String[] split = s.split(".");
String firstSubString = split[0];
String secondSubString = split[1];
Then you change the font size and color of secondSubString like this
SpannableString ss= new SpannableString(secondSubString);
ss.setSpan(new RelativeSizeSpan(2f), 0, 5, 0); // set size
ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);// set color
You can change firstSubString size and color like this
SpannableString ss1= new SpannableString(firstSubString);
ss1.setSpan(new RelativeSizeSpan(2f), 0, 5, 0); // set size
ss1.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 5, 0);// set color
Finally you set your string to the TextView
TextView tv= (TextView) findViewById(R.id.textview);
tv.setText(ss1 + "." + ss);
Related
I am trying to print a decimal number inside a textview with a special format: the part on the left of the comma in one particular size and the part on the right in a different size.
Any idea about how can I make this work?
you can make use of SpannableString
TextView tv = findViewById(R.id.textview);
String value = "10.3";
final SpannableString text = new SpannableString(value);
text.setSpan(new RelativeSizeSpan(1.5f), value.indexOf(".")+1, value.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ForegroundColorSpan(Color.RED), value.indexOf(".")+1, value.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(text);
for more refer these
http://alexandroid.net/spannablestring-example/
https://developer.android.com/reference/android/text/SpannableString.html
https://takeoffandroid.com/android-spannable-text-view-to-change-color-size-style-and-adding-click-event-for-particular-word-8acd8a05ec61
Use SpannableString..
Example
String yourText = "20.00";
SpannableString ss = new SpannableString(yourText);
ss.setSpan(new RelativeSizeSpan(1.2f), youString.indexOf(".") + 1, youString.length(), 0);
yourTextview.setText(ss);
Here is an example doing this
double number = 422.343;
String completeNumber = String.valueOf(number);
int decimalPointIndex = completeNumber.indexOf('.');
SpannableString ss= new SpannableString(completeNumber);
ss.setSpan(new RelativeSizeSpan(2f), 0,decimalPointIndex, 0);
textView.setText(ss);
Convert the decimal number to String
Get index value of decimal point
Then use it and display the first part before decimal as bigger
I have a string with some points which i get from storage model , I want to bold and color the point string only which i get from data storage model. How do i do that i have tried below mentioned code.
//Making a part of text from terms and condition spannable
SpannableStringBuilder sb = new SpannableStringBuilder("App " + CWalletDataModel.getInstance().getS_szWalletBalance() + getString(R.string.points_text));
// Span to set text color to some RGB value
final ForegroundColorSpan fcs = new ForegroundColorSpan(RewardUtil.getColor(mContext,R.color.colorAccent));
// create a bold StyleSpan to be used on the SpannableStringBuilder
StyleSpan b = new StyleSpan(Typeface.BOLD); // Span to make text bold
// Set the text color for first 4 characters
sb.setSpan(fcs, 8, 8 + CWalletDataModel.getInstance().getS_szWalletBalance().length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// set only the name part of the SpannableStringBuilder to be bold --> 16, 16 + name.length()
sb.setSpan(b, 8, 8 + CWalletDataModel.getInstance().getS_szWalletBalance().length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold
HomeScreenActivity.m_toolbarTitle.setText(sb);
Here i want only to bold "CWalletDataModel.getInstance().getS_szWalletBalance()".
That's because App + blank space is 4 characters("App "), not 8. Set the start for those 5000 at 4th position like you wrote in the comment and then spannable options would start after "App "
sb.setSpan(fcs, 4, 4 + test.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.setSpan(b, 4, 4 + test.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
I have what I think is a simple spanned string question. I have a string like this:
SpannableString styledString = new SpannableString("10:50 PM");//PM is 6-8
styledString.setSpan(new RelativeSizeSpan(.66f), 6, 8, 0);
styledString.setSpan(new UnderlineSpan(), 0, 8, 0);
((ProgressTextButton)findViewById(R.id.time_button_12)).setText(styledString);
It looks like this:
Is there any way to make it so that the underline is uniform?
Is it possible to have a TextView with different background colors. Eg. If the text is "This is a test", is it possible to set different background color for the four words?
Yes.
SpannableString spannableString = new SpannableString(getString(R.string.hello_world));
Object greenSpan = new BackgroundColorSpan(Color.GREEN);
Object redSpan = new BackgroundColorSpan(Color.RED);
spannableString.setSpan(greenSpan, 0, 6, 0);
spannableString.setSpan(redSpan, 6, spannableString.length(), 0);
TextView textView = (TextView) findViewById(R.id.text);
textView.setText(spannableString);
Produces:
EDIT: There are a lot of different spannable types, you can do much nicer looking things than my basic example. Check out this article.
I'm pretty new to android, Java and sqlite. For my first program I'm creating a program that will take user input and place in predefined text.
Example: "text" string1 "more text" string2 "even more text" etc
My text will be one color and strings will display in another color.
I'm using sqlite to seperate my data and code and this is where I hit a wall. Trying to find help on how I will be able to combine my above text into one row/column in my database table.
Using only one above example i could get this up and running. But there will be 50+ of above example for release making a database a must especially when I want to add more after release.
Most probably you've read up on SpannableStringBuilder, which allows you to add color to the text in your TextView's content. Check out the code below:
SpannableStringBuilder ssb = new SpannableStringBuilder(<your text>);
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0));
ssb.setSpan(fcs, 0, ssb.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ssb);
The code above will work in most cases, however what you want is to have different alternating colors on a single TextView. Then you should do the following:
String text = your_text + text_from_database;
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0));
ForegroundColorSpan fcs2 = new ForegroundColorSpan(Color.rgb(0, 255 0));
ssb.setSpan(fcs, 0, your_text, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.setSpan(fcs2, your_text.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ssb);
The above code will now work, but you'll notice that if you add another text your_another_text and want to use the previous fcs instance for a second time, the previously colored your_text will now lose its formatting (color). This time you'll need to create another ForegroundColorSpan fcs3 to format the third part of the SpannableStringBuilder. The key here is to use a character style in a setSpan method only once. See below:
String testTest = "abcdefgh";
String testTest2 = "ijklmnop";
String testTest3 = "qrstuvwxyz";
SpannableStringBuilder ssb = new SpannableStringBuilder(testTest+testTest2+testTest3);
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0));
ForegroundColorSpan fcs2 = new ForegroundColorSpan(Color.rgb(0, 255, 0));
ForegroundColorSpan fcs3 = new ForegroundColorSpan(Color.rgb(255, 0, 0));
ssb.setSpan(fcs, 0, testTest.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
ssb.setSpan(fcs2, testTest.length(), (testTest+testTest2).length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
ssb.setSpan(fcs3, (testTest+testTest2).length(), ssb.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
test.setText(ssb);
This method is good if you know you have a fixed number of String elements in your SpannableStringBuilder. If you have wish to have a TextView of dynamic length and number of elements, you need to do this in a different approach. What worked for me was to convert each string element into a SpannableString, use setSpan, and append it to the TextView. This is useful if you're using a loop to build your TextView.
TextView test = (TextView)findViewById(R.id.test);
String testTest = "abcdefgh";
String testTest2 = "ijklmnop";
String testTest3 = "qrstuvwxyz";
SpannableString ssb = new SpannableString(testTest);
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0));
ForegroundColorSpan fcs2 = new ForegroundColorSpan(Color.rgb(0, 255, 0));
ForegroundColorSpan fcs3 = new ForegroundColorSpan(Color.rgb(255, 0, 0));
ssb.setSpan(fcs, 0, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
test.setText(ssb);
SpannableString ssb2 = new SpannableString(testTest2);
ssb2.setSpan(fcs2, 0, ssb2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
test.append(ssb2);
SpannableString ssb3 = new SpannableString(testTest3);
ssb3.setSpan(fcs3, 0, ssb3.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
test.append(ssb3);