Textview does not change textcolor - android

I have an problem setting textcolor on some part of a Textview.
The text is defined to have the color white in the layout-xml.
When the user chooses the write answer in the game this method call is triggered:
this.Question = (TextView) findViewById(R.id.layout1Question);
this.Question.setText(Html.fromHtml("<font color = 'green'>CORRECT: </font>") + this.CurrentQuestion.getFillin());
Here I want the CORRECT -part to show green text while the rest should be white as default.
But the whole text is shown in white. What am I doing wrong?
Thanks for any help!
EDIT: It works if I remove the second part, hence:
this.Question.setText(Html.fromHtml("<font color = 'green'>CORRECT: </font>"));
works okay.

Try moving the geFillin() call to fromHtml parameter. May be the string concatenation is casting the Spanned string back to simple string.
this.Question.setText(
Html.fromHtml("<font color='green'>CORRECT: </font>" + this.CurrentQuestion.getFillin()));

Related

Change the color of a part of a TextView in strings.xml

In my android app with Kotlin, I created a layout in which there's a TextView that shows some text. For the text I have an item in strings.xml where I want to change the color of part of this Text, I tried the following code :
<string name="description">the product is <font fgcolor="green"> free </font></string>
But, The color didn't change.
I just want to change the color of "free" to green, can someone explain how I can achieve this?
Use <font color="#008000">free</font> instead. According to the documentation, the correct attribute name is color and it only supports hex codes.
Ben P.'s awesome answer should satisfy your use case. However, I want to present to you another way you can achieve this.
You can use SpannableString to achieve the same effect. With SpannableString, you can set several behaviours (color, font-weight, font-size, click-behaviour, etc) to any part of your String.
For the string in your question, you can do something like this:
// the textview you want to set your coloured text to
TextView textView = (TextView) findViewById(R.id.myTextView);
// declare the string you want to span as a Spannable
Spannable wordtoSpan = new SpannableString("the product is free");
// set the colour span
wordtoSpan.setSpan(new ForegroundColorSpan(Color.GREEN), 15, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// set the text to your TextView
textView.setText(wordtoSpan);

Change attributes to a selected part of text in EditText

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().

changing textcolor in android edittext programmatically

I have to create the background color for edittext means am getting the background correctly.
TestPost.this.findViewById(R.id.revieew)
.setBackgroundColor(color);
But how can i get the textcolor for these edittext .
TestPost.this.findViewById(R.id.revieew)
.setTextColor(color);
Please give me a solution ???
i have to pick the green color means have to enter the text is green....here i have done these for background color.i have to set the background color is green means have to pick the color green from color picker means the bexkground is successfully displaying...how can i set the textcolor from color picker ???
EDIT:
reviewEdit.setTextColor(color);
means am getting the color is successfully...But i didn't change the whole text.
I want to change the color for selected text alone...
For EG:
The text is : The customer is waiting for your reply means
Have to pick a color green, have to write the The customer is waiting have to display green color for these text alone.after that have to pick a color pink means have to display the for your reply as pink color.
This is exactly i need ...how can i implement these ???
((TextView)TestPost.this.findViewById(R.id.revieew)).setTextColor(color);
See the docs here
What you are doing is almost correct. The method findViewById() returns a View which you need to cast into a TextView/EditText (depending on how you have the view with that id defined in your xml) and then the method will be available for use.
EditText text = (EditText) findViewById(R.id.revieew);
text.setTextColor(color);
Try this TestPost.this.findViewById(R.id.revieew).getCurrentTextColor();
Found here: https://stackoverflow.com/a/6746131/2065418
Your method was correct for getting the color use below code
int color =editText.getCurrentTextColor();
Log.d("color", String.valueOf(color));

Is there a way to set the text in the text view to invisible?

I can't change its color or use alpha because the background may change and I won't know its color!
Is there any other way to set it to invisible ?
Also I don't want to set the text view invisible since I want the background to be visible!
Try:
textView.setTextColor(getResources().getColor(android.R.color.transparent));
You can set a textview to INVISIBLE:
myTextView.setVisibility(View.INVISIBLE);
That way the textview will still be present but not seen
Something like this..
String text = "Invisible Text";
TextView text1 = new TextView();
text1.setTag(text);
why not, set text value as empty string when you want to make it invisible. keep backup of the original value so that you can use it later when required.

Android: Set text color dynamically for string variable to be appended

I have TextView. Using java code I want to set color for string variable that I have to append to the text. Variable is generated at run time.
I explored Spannable but you have to give start and end which is not fixed.
Any other way to fix this. Please help.
Code:
String text; (Filled at runtime)
//but I want it to be different color
textview.append(text);
You can do something like this to set the text in your TextView:
tv1.setText(Html.fromHtml("<font color='red'>R</font><font color='green'>G</font><font color='blue'>B</font>"));
The issue with this is that when you have just one text object you can only have one colour for it. You will have to use more then one text object (each with different colours) and juxtapose them in you design.

Categories

Resources