edit text forecolor

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

edit text forecolor

Postby m.allam » Thu Dec 17, 2009 5:11 pm

how i can change fore color for part text in edit text

thanks
m.allam
Junior Developer
Junior Developer
 
Posts: 15
Joined: Thu Dec 17, 2009 5:02 pm

Top

Postby SteffenL » Thu Dec 17, 2009 7:56 pm

Hello,

Please have a look here. :)

Made an example for you as well:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <EditText
  2.         android:id="@+id/editText"
  3.         android:layout_width="wrap_content"
  4.         android:layout_height="wrap_content"></EditText>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. EditText editText = (EditText)findViewById(R.id.editText);
  2. editText.setText(Html.fromHtml("look <sup><i>at</i></sup> <u>this</u> <sub><b>text</b></sub>"));
  3. editText.setTextColor(0xFFFF0000); // red text by default
  4. Spannable sp = editText.getText();
  5. sp.setSpan(new BackgroundColorSpan(0xFF0000FF), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // blue background color for the word "look"
  6. sp.setSpan(new ForegroundColorSpan(0xFF0000FF), 8, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // blue foreground color for the word "this"
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
Regards,
Steffen
SteffenL
Junior Developer
Junior Developer
 
Posts: 23
Joined: Wed Dec 16, 2009 9:52 am

Why doesn't setTextColor() work?

Postby schiavonir » Thu Dec 17, 2009 10:06 pm

I have the following code: (sorry the formatting is messed up)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.        
  2.  
  3.  
  4.  
  5.                                         final TextView myTextView = (TextView)findViewById(R.id.TextView01);
  6.  
  7.  
  8.  
  9.                                         //rotate color
  10.  
  11.                                         intTextColor = myTextView.getCurrentTextColor();
  12.  
  13.                                         if(intTextColor == R.color.Red)
  14.  
  15.                                                 myTextView.setTextColor(R.color.Green);
  16.  
  17.                                         else if(intTextColor == R.color.Green)
  18.  
  19.                                                         myTextView.setTextColor(R.color.Blue);
  20.  
  21.                                                 else
  22.  
  23.                                                         myTextView.setTextColor(R.color.Red);
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


TextView01 is originally set to color R.color.Red (and shows red onscreen) in layout.xml, but it doesn't even match up on the first "if" statement. What am I missing?
schiavonir
Freshman
Freshman
 
Posts: 3
Joined: Thu Dec 17, 2009 10:00 pm

Re: Why doesn't setTextColor() work?

Postby SteffenL » Thu Dec 17, 2009 11:27 pm

schiavonir wrote:...

R.color.* contains the resource ID for the color(s), not the actual value.
To get the value, use Resources.getColor().
To get an instance of Resources, use Context.getResources().
Example:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Resources res = getResources();
  2. int redColor = res.getColor(R.color.Red);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

Also, you may want to make a new thread/topic next time. :)
Last edited by SteffenL on Thu Dec 17, 2009 11:50 pm, edited 1 time in total.
Regards,
Steffen
SteffenL
Junior Developer
Junior Developer
 
Posts: 23
Joined: Wed Dec 16, 2009 9:52 am

Postby schiavonir » Thu Dec 17, 2009 11:43 pm

Thanks for your response. That makes sense.

I wasn't trying to hijack this thread, but since it seemed to be in the same spirit as my question, about changing the color of a text element, I just added it onto this one.
schiavonir
Freshman
Freshman
 
Posts: 3
Joined: Thu Dec 17, 2009 10:00 pm

Postby SteffenL » Thu Dec 17, 2009 11:54 pm

schiavonir wrote:I wasn't trying to hijack this thread, but since it seemed to be in the same spirit as my question, about changing the color of a text element, I just added it onto this one.

It was just a little heads-up. I'm sure it's alright (this time). :)
Regards,
Steffen
SteffenL
Junior Developer
Junior Developer
 
Posts: 23
Joined: Wed Dec 16, 2009 9:52 am

Top

Postby schiavonir » Fri Dec 18, 2009 12:00 am

Yeah, re-reading my post, I guess the least I could have done was some kind of a segue: "As long as we're talking about changing the colors of a TextView, I have a similar question..."

Feedback noted. I'm sure this won't be the last question you'll see from me. Not new to development, but new to Android.
schiavonir
Freshman
Freshman
 
Posts: 3
Joined: Thu Dec 17, 2009 10:00 pm

Postby SteffenL » Fri Dec 18, 2009 12:06 am

schiavonir wrote:...

I wasn't bothered at all. :)
I'm in the same boat as you but I'll be happy to help when I can. :)
Regards,
Steffen
SteffenL
Junior Developer
Junior Developer
 
Posts: 23
Joined: Wed Dec 16, 2009 9:52 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot], Majestic-12 [Bot], Yahoo [Bot] and 5 guests