Android XML: L shape text view - android

I have 2 TextViews:
txtView1: User first name.
txtView2: User comment.
The design should be as shown in the picture below:
The problem is that i can't find a way to implement this design without adding a third TextView.
With 2 TextViews I got either overlapped text views or side by side text views.
Any ideas?

You can use even 1 TextView by using spans.
Here is what I got (I have only 1 text view):
And here is the code:
String firstName = "Pavel";
firstName = firstName.toUpperCase();
String firstPart = firstName + ": ";
String finalText = firstPart + "Text text text text text text text text text text text text text text text text text text text text text text text text text";
//0 - first line margin, 50 - other lines margin. Should be taken from resources.
LeadingMarginSpan paragraphSpan = new LeadingMarginSpan.Standard(0, 50);
//Bold span for the first name
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
//Color span for the notes text. Should be taken from resources
ForegroundColorSpan colorSpan = new ForegroundColorSpan(0x77000000);
Spannable spannableString = new SpannableString(finalText);
spannableString.setSpan(paragraphSpan, 0, finalText.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(boldSpan, 0, firstPart.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(colorSpan, firstPart.length(), finalText.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
Really recommend to get yourself familiar with this article to better understand spans

Related

How to make part of string bold and colored in android?

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);

In TextInputLayout change the color of hint for single letter or alphabet

I have defined TextInputLayout programmatically in my Activity. I have added EditText inside that TextInputLayout, that to programmatically.
Now, I have to set the color of hint of that edittext. Here I just have to change the color of single alphabet of the hint.
Example - Hint is like, Firstname *. Here i want to change the color of "*" to Red and remaining hint as black in color.
I have already tried Html and Spannable String, but both are not working for TextInputLayout.
For Spannable i did
SpannableString redSpannable = new SpannableString(red);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
Spanned hint = Html.fromHtml(string + redSpannable);
etDefault[j].setHint(hint);
For HTML,
I used "font-color"
If anyone knows this, then share your opinion.
As per the Android Issue Tracker,
Setting the hint to the editText respects the span, but the label does
not float on focus.
editText.setHint(hint);
Setting the hint to the textInputLayout ignores the span
textInputLayout.setHint(hint);
TextInputLayout uses internal class CollapsingTextHelper to draw the
hint, which does not support spans.
And as they said,
They have passed this defect on to the development team and will
update this issue with more information as it becomes available.
In TextInputLayout change the color of hint for single letter or alphabet
String firstNameText ="firstName *";
Spanned redStar;
String html = "<string style=\"color:firstNameText;\">firstName<span style=\"color:red;\">*</span></string>";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.L) {
redStar = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
} else {
redStar = Html.fromHtml(html);
}
I suggest you using just SpannableStringBuilder without Html.fromHtml:
SpannableStringBuilder sb = new SpannableStringBuilder("Hint with first letter black");
CharacterStyle cs= new ForegroundColorSpan(ContextCompat.getColor(this, android.R.color.black));
sb.setSpan(cs, 0, 1, 0);
((TextView) findViewById(R.id.description_messages)).setHint(sb);
In this piece of code the first letter becomes black. If you would like it to be bold too, use this:
CharacterStyle cs = new StyleSpan(android.graphics.Typeface.BOLD);
That just puts hint to EditText. There will be no floating text effect.

How to set gravity center of SpannableStringBuilder?

I'm using two different fonts in one TextView: first is SVG font , second TTF font.
To set icon with text to TextView I'm using this:
String icon =getContext().getString(iconId);//""
String text = getResources().getString(labelId);//"AnyText"
int iconLength = icon.length();
String fulltext = icon + " " + text;
SpannableStringBuilder spLabelWithIcon = new SpannableStringBuilder(fulltext);
spLabelWithIcon.setSpan(new CustomTypefaceSpan(TypefaceManager.ICONS.getTypeface()),
0, iconLength, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
spLabelWithIcon.setSpan(new CustomTypefaceSpan(TypefaceManager.GOTHAM_BOOK.getTypeface()),
iconLength, fulltext.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
myTextView.setText(spLabelWithIcon);
Result: the icon is not on vertical center of the line(or maybe text is not on vertical center):

Smily above the text in spannable string

I used the below code to add image span.
Drawable happySmiley = ctx.getResources().getDrawable(
R.drawable.img_smile);
happySmiley.setBounds(0, 0, happySmiley.getIntrinsicWidth(),
happySmiley.getIntrinsicHeight());
Drawable sadSmiley = ctx.getResources()
.getDrawable(R.drawable.ic_launcher);
sadSmiley
.setBounds(0, 0, sadSmiley.getIntrinsicWidth(),
sadSmiley.getIntrinsicHeight());
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("Some text ");
builder.setSpan(new ImageSpan(happySmiley), builder.length()-1, builder.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(". More text [sad_smiley_anchor]");
builder.setSpan(new ImageSpan(sadSmiley), builder.length()
- "[sad_smiley_anchor]".length(), builder.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tvSS.setText(builder);
o/p Some text (smiley). More text (smiley).
But I want to display the smiley above the "Some" in "Some text". Like
(Smiley) (Smiley)
Some Text. More text.
If smiley can be set as background to "Some" in "Some text" like setForegroundSpan() etc. then also problem can be short out. Please comment on this.
Thanks

TextView with different sizes

In my layout, I have defined a TextView and have given it the id - textName.
Programmatically, I have set its text to My name is Blah. I was wondering if it is possible to programmatically increase the textsize of just Blah and not the whole TextView?
Or simply using the Spannable class:
String text = textView.getText();
Spannable span = new SpannableString(text);
span.setSpan(new RelativeSizeSpan(1.5f), text.indexOf("Blah"), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(span);
Yes!! you can do by formatting the string with HTML, refer https://stackoverflow.com/a/1533512/603233
like eg
String text = "<h5>My name is </h5><h1>Blah</h1>";
yourtextview.setText(Html.fromHtml(text));

Categories

Resources