Show degree symbol in a TextView

  1. For Celsius ℃ use :

         "\u2103" 
    

for Fahrenheit use:

      "\u2109"

for only degree symbol without c or f use:

     "\u00B0"

For example:

      String TemperatureMeasurementStr = String.valueOf(measurement.getTemperature()) + "\u2103";

Or simple example:

      String TemperatureMeasurementStr = "37"+ "\u2103";

And set the string in your textView

      public TextView temperatureTV;                     
      temperatureTV.setText(TemperatureMeasurementStr);
  1. if you want just to add the symbol to the xml layout file - just use:

       android:text="37\u2103"
    

for example:

<TextView
     android:id="@+id/temperature_measure"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignTop="@+id/temperature_icon"
     android:layout_marginBottom="-5dp"
     android:layout_marginLeft="10dp"
     android:layout_toRightOf="@+id/temperature_icon"
     android:text="37\u2103"
     android:textAlignment="center"
     android:textSize="50sp"
  />
  1. if you want this symbol to be smaller or in different color or style then the number (lets say "normal" while the number itself is "bold") then you can use SpannableString:

Like this:

String TemperatureMeasurementStr = String.valueOf(measurement.getTemperature()) + "\u2103";
SpannableString tempSpan=  new SpannableString(TemperatureMeasurementStr);
if (TemperatureMeasurement.length() >0){
   
    //the symbol will be smaller then the number
    tempSpan.setSpan(new RelativeSizeSpan(0.7f),TemperatureMeasurementStr.length() - 1, TemperatureMeasurementStr.length(), 0);

    //the number style will be bold and the symbol normal
   tempSpan.setSpan(new android.text.style.StyleSpan(Typeface.BOLD), 0,   TemperatureMeasurementStr.length()-1, 0);
  
    //the symbol color will be yellow
   tempSpan.setSpan(new ForegroundColorSpan(Color.YELLOW), TemperatureMeasurementStr.length() - 1, TemperatureMeasurementStr.length(), 0);
 }
 temperatureTV.setText(tempSpan);

Finally, if you want just to type the degree symbol "°" in any editor in windows (include android studio) just use:

                                    Alt+0176  

or

                                    Alt+248 

There're a lot of ways to do it :

1.- You can create a char as follows :

char degreesymbol = '\u00B0';

Then you can add it on your TextView

2.- You can put it on your XML as follows :

android:text="50&#x2103;"

3.- And if you want to make it programmatically do it as follows :

YOURTEXTVIEW.setText((Your_Temperature) + " \u2109");

&#xb0; is just the standard way of inserting special characters into XML. You can refer to https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references for a more full description, but to break it down:

  • & indicates it's the start of a special character.
  • # means the character is indicated with a number (versus a special string code, e.g. &amp; for ampersands).
  • x means the numeric code is a hexadecimal value.
  • b0 the hex value for the degree symbol (176 in decimal).
  • ; ends the sequence.

An alternative representation would be &#176; which uses the decimal value instead of hex.

Hence, you can insert any special character if you know its ASCII decimal/hex value.