Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show degree symbol in a TextView

I want to show 50 degrees in my activity using TextView as shown below

enter image description here

I really don't know how to do this and by googling I found this XML code

android:text="50°"

And I have no idea what the above code is.
Can anyone please explain to me what exactly that does and how it works.

like image 526
Akash Preet Avatar asked Apr 18 '16 16:04

Akash Preet


People also ask

How do you get the degrees symbol on text?

Press and hold the ALT key and type 0 1 7 6 on the numeric keypad of your keyboard. Make sure the NumLock is on and type 0176 with the leading zero. If there is no numeric keypad, press and hold the Fn before typing the 0176 numbers of degree symbol.

How do I add a bullet symbol in TextView?

The two Button s at the bottom have android:text="◄" and "►" .

How do you get the degree symbol on flutter?

Using Numeric Keypad You may also hold the Alt key and type 0176. Note that you have to turn on the num lock key to be able to use the numeric keypad. To easily remember, here are the keyboard shortcuts: Alt + 248.


2 Answers

  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 
like image 138
Udi Reshef Avatar answered Sep 19 '22 03:09

Udi Reshef


&#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.

like image 25
Allen G Avatar answered Sep 21 '22 03:09

Allen G