i want to put large text inside textView. The text has multiple section with multiple colours. i have seen Html.fromHtml() function and also know how to use setSpan(). But any of these not work for me. In my case i dont know the id of text View at runtime. As i am inflating different Views at runtime each View has many textViews. It will be better if i can find a way to set styles in strings.
Please some one help me out i have spend so much time here.
you can set id of text view weather the view is find by inflater
when you get text view using inflater then you can set id of that particular text view
and you can set textSize() and other attributes
This method creating spannables directly with the attributes you need will work.
Basically you can set colors like this, given a TextView called text:
String greeting = "Hello World!"
SpannableString str = SpannableString.valueOf(greeting);
str.setSpan(new ForegroundColorSpan(0xffffffff), 0, greeting.length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
str.setSpan(new BackgroundColorSpan(0xff0099ff), 0, greeting.length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
text.append(str);
Related
I have some intent with set of TextViews and a Button. If user clicks the button and there is some error I want to change look of one TextView. E.g. change the border to red and make font bold. I wrote a style for it but I have not found method setStyle on the TextView. After some self study I realized that Android does not support setting the style programmatically. There are some workarounds, when you create the intent source. But my intent already exists, it seems odd to recreate it.
Could you tell me the proper way?
use the workaround and create the TextView again
forget the styles and use java methods to decorate existing TextView
something else
Changing the style of the textview directly does not work as you know. But you can create a second textview with other styles in your layout, which you can show up if needed.
Just add this xml attribute android:visibility="gone" to the second textview, so this second textview is not displayed at first, but available.
When you now want to change the style of your textview, you simple need to swap the two textviews by hidding the first one and showing the second one
textView1.setVisibility(View.GONE);
textView2.setVisibility(View.VISIBLE);
I used these two answers to make it work:
https://stackoverflow.com/a/5488652/1639556
https://stackoverflow.com/a/14195090/1639556
and the code is:
ViewManager parent = (ViewManager) unknown.getParent();
parent.removeView(unknown);
TextView newUnknown = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
newUnknown.setId(unknown.getId());
parent.addView(newUnknown, unknown.getLayoutParams());
unknown = newUnknown;
You can try using setTextAppearance() on the textview. The link is: setTextAppearance
Your style will need TextAppearance.SomeThing.SomeOtherThing as the parent.
Use R.style.YourStyleName as the integer argument.
I've searched something around here but nothing came up.
I'd like to have an EditText in which I can change attributes like color or dimension of a selected part.
I've already tried with the spannable thing, from another question:
TextView myTV = (TextView)findViewById(R.id.test);
String textString = "StackOverFlow Rocks!!!";
Spannable spanText = Spannable.Factory.getInstance().newSpannable(textString);
spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), 14, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTV.setText(spanText);
I assume that I have to assign it to some onClick method..Also my problems are those two number, I should put some "selectedText" there instead I think.
Is this even possible?
I'd like to have an EditText in which I can change attributes like color or dimension of a selected part
If you want the user to "change attributes... of a selected part", my recently-updated RichEditText offers that. I do not have color or text size going yet, though, as those will need a toolbar (rather than my current action mode support).
Also my problems are those two number, I should put some "selectedText" there instead I think
You are probably looking for getSelectionStart() and getSelectionEnd().
I want to add small upper text in android textView
how would you implement this?
two textViews both layout_below the same element and then
make the second smaller?
or is there a way in one textView to put style to one word
(can be longer or shorter in other languages)?
You can achieve that by following code
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("2580<sup>TH</sup>"));
Solution from Android Layout framework, would put two textViews in side a Linear layout and that is it.
Another way, is making the small text as an image. Than you can add it to text view directly.
you can do it like this:
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("YourRank:250<sup>TH</sup>"));
also you can do it like this:
String str=getString(R.string.rank); //YourRank:250
String str2=getString(R.string.superScript); //TH
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("+str+"<sup>"+str2+"</sup>"));
it worked like charm!
You can use SpannableStringBuilder as fallows:
String text = "YourRank:250TH";
SpannableStringBuilder spannableString = new SpannableStringBuilder(text);
spannableString.setSpan(new SuperscriptSpan(), text.length() - 1, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
YourTextView.setText(spannableString);
I'm trying to have the layout of the comments on a progam in the following form:
I want the USER_NAME in Bold, and the DATE in Enphasis (The red part should be normal, I don't want it red). My problem comes with the red part. I cant get the cooment to be displayed like this using layouts, I've been able to get:
and
Is there any way to get the comment working like the one I'm showing. I've thought about a multi formatted textView, is it posible??
If getting the comment like this is not possible, just say it's imposible.
One possible solution is to put two TextViews into a Linear layout one after another and then use SpannableStringBuilder to assign the formatted text to the first view.
SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(text);
sb.setSpan(createBoldSpan(), 0, lengthOf(userName),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
firstTextView.setText(sb, TextView.BufferType.NORMAL);
createBoldSpan should return TextAppearanceSpan
private TextAppearanceSpan userNameSpanInBold(String userName) {
return new TextAppearanceSpan(...);
}
please follow API docs for TextAppearanceSpan - you can create one using custom style.
You could use a WebView with loadData if HTML markup would be good, or alternatively delve into the realm of Spannable text.
I've not worked with Spannable text myself, but essentially it allows you to attach attributes (e.g. styles & colors) to parts of text in a TextView.
The relevant method is TextView.setText(CharSequence text, TextView.BufferType type) with BufferType of SPANNABLE. The documentation I've seen is.... confusing.
Hello I am trying to change the text color of the items in a list on a ListPreference pop up window. I have spent over an hour looking through all of the various style names but I can't find TextAppearance or anything that goes to this particular text. Thanks for your help!
You can't and you shouldn't. *Preference uses styles from com.android.internal.R.styleable which might be changed by manufactures. The idea of using the default ones is that every preference screen in your device look alike.
On the other hand you can try doing an Activity with android:theme="#android:style/Theme.Dialog" in your app's AndroidManifest and place a ListView styled as you want.
I don't really know which kind of View use ListPreference, probably it's something like TextView. If so than you could make smth like:
TextView textView;
String myString;
//....
SpannableString spanString=new SpannableString(myString);
spanString.setSpan(new ForegroundColorSpan(Color.RED), 0, myString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spanString);