Frage

So I am trying to add a TextView widget to my app that reports the API level that my device is running to the user.

I found out this string reports the API Level:

String androidOS = Build.VERSION.RELEASE;

Unfortunately after some more research, it seems like most people are very passionate about not using hardcoded strings in Android and it appears that it is not allowed. However, I kind of need to because it is essential to display the API level of the device.

If someone could tell me how to display hardcoded strings or use string resources somehow to display the API level to the user, that would be very appreciated.

War es hilfreich?

Lösung

Create a String resource like this:

<string name="wine_detail_header">Wine Details</string>

inside string.xml in values folder. You can then use it like this:

String s = myContext.getResources().getString(R.string.wine_detail_header);

Andere Tipps

What you are doing is perfectly fine. Build.VERSION.RELEASE is what is known as a constant not a hard-coded string.

You can use the setText() method of TextView:

myTextView.setText(Build.VERSION.RELEASE);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top