Android not properly displaying > on TextView - android

I've tried searching for it, but I can't seem to find anything, since searching with either > or &ampgt; doesn't really give you the intended search result.
The problem I'm facing is, that whenever I put a string into strings.xml, for example I typed in,
1 > 2 > 3
It changes it to 1 &ampgt; 2 &ampgt; 3
I've tried hardcoding the string in the layout file itself, but it seems to automatically change to &ampgt;, and I've tried running it on the emulator, as well as it showing on the preview, it seems to only show the html number of it.
Any solution would be appreciated. Of course, I would like to avoid typing all of the text manually by using Html.fromHtml

I don't understand, because I've made a test project, and it's working as intended :
in my res/values/string.xml file :
<string name="test_string">This is > a string <</string>
in my res/layout/main.xml file :
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/test_string" />
And on my Android device, it's showing :
This is > a string <
Can you post an extract of your own string.xml and layout file, and give a screenshot of your app ?

Related

How can I write "ó" in Android Studio on layout?

I am intented to write Iniciar sesión, but the ó not working. For this reason I changed to ó but is another error.
<TextView
android:id="#+id/textView"
android:layout_width="330dp"
android:layout_height="24dp"
android:text="Inicio de sesión"
tools:layout_editor_absoluteX="27dp"
tools:layout_editor_absoluteY="28dp" />
¿I must configure Android Studio?
I added compileOptions.encoding "ISO-8859-1" in build.gradle but the error persist.
Help me, please.
Thank you!
To fix this issue, you have to create a Strings.xml file and add to it and qualifier (Spanish language) then you can add this letter with accent without any problem
<resources> <!-- spanish language qualifier -->
<string name="test_text">Iniciar sesión </string>
</resources>
You can read more about qualifiers in the oficial documentation here:
Qualifiers
You should avoid to use text like this, directly on your layout file. Instead of it, you should relay on the file string.xml where you define all the texts your xml files will need to display. By doing this, you will be doing nothing less than the correct specification from Android makers in order to localize your app (more info on this: https://developer.android.com/guide/topics/resources/localization.html ).
So, proceed this way:
1) on your res folder there is another one called values. There you will find your strings.xml file. Open it and create a new key, let's call it txtarriba, like this:
<string name="txtarriba">Begin of session</string>
2) with android studio or your regular file manager, create another folder below your res folder, call it values-ES.
3) inside values-ES create a new file named strings.xml, there you will insert the translation of that key txtarriba. So do this:
<string name="txtarriba">Inicio de sesión</string>
4) Then, on your primitive layout xml file you define your textview like this:
<TextView
android:id="#+id/textView"
android:layout_width="330dp"
android:layout_height="24dp"
android:text="#string/txtarriba"
tools:layout_editor_absoluteX="27dp"
tools:layout_editor_absoluteY="28dp" />
DONE.
With this you can assure you app is, from now on, correctly showed according to your device idiom. If your device is in english, Begin of session will be showed. If device is set with spanish language, you will read Inicio de sesión. With this you can forget all the troubles about special chars, Android manages this for you ;)

How to solve this issue of Hardcoded string?

[I18N] Hardcoded string "Happy Birthday Debashish", should use #string resource less... (Ctrl+F1)
Hardcoding text attributes directly in layout files is bad for several
reasons: * When creating configuration variations (for example for landscape or
portrait)you have to repeat the actual text (and keep it up to date when
making changes) * The application cannot be translated to other languages by
just adding new translations for existing string resources. In Android Studio
and Eclipse there are quickfixes to automatically extract this hardcoded string
into a resource lookup.
Ths is not an error but a warning. As a general rule, you should never use hard-coded strings in your layout but always use string resources instead (which means that all the strings are stored in one separate file where they are easily changeable for different languages and so on).
To convert a hard-coded string into a string resource:
Put the curser on the hard coded string;
Press ALT + Enter;
Enter a name for your ressource;
Click OK.
After doing this the warning will be gone.
This is just a warning.
Define your string in string.xml file
Happy Birthday Debashish
and in textView use this string as
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/string_name"
/>
This is only a warning. The function will still work as intended. It is just recommended to place your text in the strings.xml file. This makes future changes much simpler and is easier to reference across multiple pages.
First, place the <string> element in values/strings.xml like this:
<string name="your_string_name">Happy Birthday Debashish</string>
Then, you can reference the string in the .xml file as follows:
<TextView
android:text="#strings/your_string_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Again, it is not required to do it in this method. It just makes things simpler to manage and change in the future if needed.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title"
tools:text="Happy Birthday Debashish" />
When you are in the 2019 version. Go to the strings.xml and Add this in to it
<string name="Your text">Your Text</string>
Or
In the warning it has the Fix button you can click it for fix

Android find all hardcoded strings in code using Android Studio

I want to find all hard-coded strings in my code to move them into strings.xml file for future localization. Such as :
Toast.makeText(context,"Hardcoded text",LENGTH_SHORT).show();
I using Android Studio.
Go to Analyze > Run Inspection By Name... (Ctrl+Alt+Shift+I)
and type:
Hardcoded Text to find the hardcoded strings in
the .xml files;
Hardcoded Strings to find the hardcoded strings in the .java files.
Run it against the whole project, and you should get an inspection results panel that will show the hardcoded text instances.
This answer hasn't getting to me any result.
Nonetheless, for future searches :
In Android Studio 1.2.2 added new Option Hardcoded strings(not a Hardcoded text) and this getting to me perfect searches result.
After android studio 1.2.2 It seems pretty easy way to do it,
Go to Analyze
Run Inspection by name
Type : HardCoded Text
And then by selecting appropriate module's option you can get all hard coded Strings in your whole project.
Tip : Short key : Ctrl + Alt + Shift + I
It seems it has been already answered here, isn't it relevant for your problem ?
Edit : just don't forget to check "File mask(s)" box in the window after having typed "Hardcoded text", and select *.java, if you want to search in Java files.
And after you found all your hardcoded strings, this may help you to transfer them to XML.

Android: resource String automatic generation

I'm new to Android. When I add a button/views in Graphical layout it adds the label text this way- android:text="Button" . Why doesnt it add "android:text="#string/my_label" and add a string resource in string.xml file. Can't it be done automatically in eclipse?
I have searched a lot but I have not get any automated way to add a string to the resource file But This will save your time a lot IMHO.
Select a String, click Refactor --> Android --> Extract Android String.
Thanks to Brent Hronik. CTRL-1 on Windows works fine.
Because you don't have to use the #string resource. The purpose of the #strings resource is to make it easier to change elements about your code. For example, if you are using your application title in mutliple places, let's say in every dialog box, then if you change the title you would have to change it in all the instances that the app title is being display. So in this instance the #string/App_Title could be set to "My Program" and all of the dialog boxes can reference that. If you change the title to "Hello World" then all of these are changed. The #strings resource, while eclipse tries, doesn't have to be used for every string. Not using it is the equivalent to hard coding the value. There are plenty of reasons for and against using #string for everything.
I am not sure if there is a setting in eclipse that will automatically add a string to the resource file when the control is added.
(EDIT: Based on other users CTRL+1 is the short cut to do this.)
You can add the string to the strings.xml by clicking command and 1(on a mac, assume it would be control 1 on a Windows or Linux box) simultaneously. This will add the resource to strings.xml and then open that up in the editor.
Thanks Siddiq Abu Bakkar! I didn't think it would be there.
On Eclipse (and Windows) the shortcut is:
Alt+Shift+A (release all and then press) S
When you use Eclipse for first time it's not easy understand how to use these kind of "complex" shortcuts.
I can't vote and i can't comment answers yet (missing reputation as i'm a new user)
But i confirm :
1) hard type the string in your code like
mydlg.setTitle("hello guys");
2) select your string (e.g : "hello guys")
3) press Alt + Shift + A then press S
a dialog will appear to let you add a new string into resources. Everything should be already filled into that dialog box.

error in android:text

i am writing a piece of code where it creates a button and put a text on it. here is the piece of code
<Button android:id="#+id/start_client"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Client" />
but the part "android:text="Start Client" />" is being highlighted (yellow), not allowing me to build the project.
Can anybody help me out??
Thanks
//that yellow line is indicating as warning
you need to use res/string file to store text values.
[I18N] Hardcoded string "Start Client", should use #string resource
this is because of Lint tool.
your project has some other error.
open your problems tab and check whats wrong there.
//clean and build again will also work fine.
The yellow part is a warning to you to not use a hard coded string for text. means you cant give just any string directly to any content like button.
whatever string you want to use, first you will add that in string.xml (check in "values" folder in left side).
after adding any string, you can use in you project anywhere without getting any warning.

Categories

Resources