HTML Styles in TextView

Olivertech2009Olivertech2009 Marcelo de OliveiraBRMember
edited November 2012 in Android

Hi There,

I am using VS 2010 with Mono For Android, and I have this situation in my strings.xml.

<string name="mensagem">Olá <b><i> {0} </i></b>,</string>

And in my code I do that:

string msg = String.Format(Resources.GetString(Resource.String.mensagem).ToString(), cliente.Nome.ToUpper());
lblNome.Text = Html.FromHtml(msg).ToString();

cliente is a class that I have properties to get from.

It is a simple example, but the style tags (b,i) do not work. I need to concatenate words with and without syte, so I need to do it in this way. I cant use setTypeface because I need to style individual words.

What is the way ?

Best Regards, Marcelo.


  • CheesebaronCheesebaron Tomasz Cielecki DKMember, Insider, University ✭✭✭✭✭

    You need to set the TextFormatted property and not the Text property. The ToString() method in the returned ISpannable you get from Html.FromHtml() just returns a normal string, which does not contain formatting.

    So try something like:

    var msg = string.Format(Resources.GetString(Resource.String.mensagem).ToString(), cliente.Nome.ToUpper());
    lblNome.TextFormatted = Html.FromHtml(msg);
  • Olivertech2009Olivertech2009 Marcelo de Oliveira BRMember


  • Olivertech2009Olivertech2009 Marcelo de Oliveira BRMember

    Hi Tomasz,

    I´ve tried your suggestion, but nothing happened. The text is set without the html tags.

    Do you know what may I have forgotten ?

    Thanks, Marcelo.

  • Olivertech2009Olivertech2009 Marcelo de Oliveira BRMember

    Hi Thomasz,

    I´ve found the answer !!! Using your suggestion, and changing the resource file, like below, I have finally make the style works...

    The line in strings.xml now, is now like that:

    <string name="mensagem">Olá &lt;b>&lt;i> {0} &lt;/i>&lt;/b>,</string>

    Thanks again. Marcelo.

  • CheesebaronCheesebaron Tomasz Cielecki DKMember, Insider, University ✭✭✭✭✭

    Cool! This is also documented here: under Styling with HTML markup :)

  • Olivertech2009Olivertech2009 Marcelo de Oliveira BRMember

    Hi Tomasz,

    Apologizes me to "explore" you here... :)

    May I ask you another thing ?

    My app will be running on APIs 7 to 10 (2.1 to 2.3.3). I have already tested it in the emulator with api 2.2 and when I press "CTRL + F11" the emulator changes from portrait to landscape and my app goes too.

    But, when I do the same thing with emulator running 2.3.3, the emulator changes but my app didnt change, remaining in portrait format, while emulator is in landscape mode. Do you know how to configure this feature to run in any api version ?

    Best Regards, Marcelo.

  • CheesebaronCheesebaron Tomasz Cielecki DKMember, Insider, University ✭✭✭✭✭

    I think it should just rotate the view, probably also restarting the activity if I recall correctly.

    If you want to detect rotations and set the view accordingly, i.e. if you want to have different views for landscape and portrait mode. You can in your activity where you set the Activity attributes add: ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation

    Such that you Activity attributes could look like this:

    [Activity (Label = "My Awesome Activity", ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation)]

    In the OnCreate method you should now be able to get the orientation with: var surfaceOrientation = this.WindowManager.DefaultDisplay.Rotation;

    And you possibly want to override the OnConfigurationChanged method as well to detect them when the screen actually rotates:

    public override void OnConfigurationChanged (Android.Content.Res.Configuration newConfig)
        base.OnConfigurationChanged (newConfig);
        Console.WriteLine ("config changed!");
        if (newConfig.Orientation == Android.Content.Res.Orientation.Portrait)
            // Do something when in portrait mode
        else if (newConfig.Orientation == Android.Content.Res.Orientation.Landscape)
            // Do something else when in landscape mode

    As to why you view does not rotate I do not know, if you can recreate it with a sample project it can maybe help me or others seeing this thread finding out why.

  • IliassIliass Iliass Yousfi USMember
    edited June 2013

    I had used the same but the OnConfigurationChanged doesn't fire

    public class NetworkView : BaseView
        protected override void OnCreate(Bundle bundle)
            this.Title = Resources.GetString(Resource.String.network_view_name);
        protected override void OnViewModelSet()
        public override void OnBackPressed()
        public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
            // Checks the orientation of the screen
            if (newConfig.Orientation == Android.Content.Res.Orientation.Landscape)
                Toast.MakeText(this, "landscape", ToastLength.Short).Show();
            else if (newConfig.Orientation == Android.Content.Res.Orientation.Portrait)
                Toast.MakeText(this, "portrait", ToastLength.Short).Show();
  • jeff.albanojeff.albano Jeff Albano USMember

    Hi Guys,

    Any of you have tried using it on the cross platform? I cannot achieve what i wanted. Somehow it goes like this. I wanted it to look like a clickable link like we do in web.

    Label _HtmlTagLabel = new Label() { FormattedText = "Forgot password? Click here." }

    Thanks for any help.

  • xceedxceed Iain Collins GBMember

    @jeff.albano I too am trying to achieve something similar using Xamarin.Forms, did you get it working?

Sign In or Register to comment.