I want to show 50 degrees in my activity using TextView
as shown below
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.
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.
The two Button s at the bottom have android:text="◄" and "►" .
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.
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);
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"
/>
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
°
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. &
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 °
which uses the decimal value instead of hex.
Hence, you can insert any special character if you know its ASCII decimal/hex value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With