As described in Databinding document we can use default to check value in preview pane.
It is working perfectly if i have simple String like below :
android:text="#{place, default=Columbia}"
But i have combination of state and country and if i am using ,(comma) then it is creating problem.
android:text="#{place, default=Gujarat, IN}"
It is showing error , unexpected, How to resolve it?
I know still we can use tools:text to check output in preview pane, but how to overcome this issue in Databinding with default?
Changing the ticks will work, but it'll show the ticks:
android:text='#{place, default="Gujarat, IN"}'
What you can do, is to create a string resource:
<string name="placeholder">Placeholder, text</string>
And use it as default value:
android:text='#{viewModel.placeHolder, default=#string/placeholder}'
You need to quote the entire value.
For example:
android:text='#{place, default="Gujarat, IN"}'
will work
You can use double quotes with back quotes or single quotes with double quotes. Check this Android developer document reference.
1st way
android:text="#{place, default=`Gujarat, IN`}"
2nd way
android:text='#{place, default="Gujarat, IN"}'
Related
I have an Android application using default values from the Preferences Android framework.
It all works fine except for phone numbers (defined with android:inputType="phone" in preferences.xml).
The phone numbers get treated as numeric value so if I go to the preferences screen to see the default values I see
3.3631241E10
for the value defined in preferences.xml as
android:defaultValue="+33631241234"
To avoid this problem, I have used values from strings.xml defining the default values in preferences.xml like this :
android:defaultValue="+33631241234
It works ... but I don't like it: that's a source of problem as I need to redefine the same phone number for each language used ! !
I must be doing something wrong as I haven't found anyone else with the same problem on internet however I don't see what I am doing wrong ! !
Any help would be really appreciated.
that's a source of problem as I need to redefine the same phone number for each language used
you don't - when the text is absent for a language the default is used - docs :
Whenever the application runs in a locale for which you have not provided locale-specific text, Android will load the default strings from res/values/strings.xml.
<string name="message">هذا المجلد يحتويى على %1$s ملفات. الرجاء التأكد قبل الحذف. الملفات المحذوفة غير قابلة للإسترجاع.</string>
I wanna put "%1$s" in an arabic string, but as you can see here, word, notepad++, utraedit, all failed to get the right string. how you guys edit arabic string?
In android Studio 2, RTL support is not turned on by default,
Configure it Manually:
1. In your Computer, go to the [android-studio2.0]/bins/idea.properties
2. add editor.new.rendering=true to the end of idea.properties
3. restart your android studio.
This is a source of frustration when editing mixed-direction text. What counts is the logical order of the text, not how any of the editors display it. When you finally format the string at run time in the app, the %1$s will be replaced by whatever string you pass to the formatting method. The only thing that matters is how the string will be rendered after the substitution.
The easiest thing to do is to write the message without the %1$s, then position the insertion caret, paste in the format code, and simply ignore how the editors screw up the bidi analysis. (The screw-up is because the editors are using a left-to-right base level. In some editors, you can set the base flow to right-to-left, but then the xml markup ends up being unreadable.)
When I work with RTL text and I need to put a place holder(%1$s) or LTR words, I just write it in MS Word and copy to the IDE.
It works for me in Eclipse and Android Studio.
All you need to check is that the components that displays that string like TextView has the right gravity.
You can try in Activity..
String formatedString = String.format("%1$s", getString(R.string.your_string));
Everybody knows that if we have:
ekran3.setText("VAT Tax:");
We may (or even we SHOULD) convert it to:
ekran3.setText(getString(R.string.kwotaVat));
and add in strings.xml:
<string name="kwotaVat">VAT Tax:</string>
But is there some kind of trick to do it automatically? For example by clicking RMB on text and selecting some option? It would be nice to know it in fact it will save us a lot of time than while we're doing it manually.
If you are using Eclipse you may extract the string directly into the strings.xml file by placing the mouse within the string and hitting Ctrl + 1. It will bring up the dialog as followed and you may select "Extract String". You then give it a name (Ex: kwotaVat) and you're done.
hey you do not need to use getString() to convert it to string the values xml file is already having data in string form so you just need to use the following code to set the string
ekran3.setText(R.string.kwotaVat);
where ekran3 is the object of your text view
and kwotaVat is the id of your value string
for more detail od android codes have look here http://grabcodes.blogspot.com/
TextView.setText(String with ') , i wanna set a text to the text view but this text keep coming uncompleted in case it has Space or apostrophe
i have tried to use
String SomeString="MacDonald's";
or
SomeString="Fast Food";
and i tried the following
HTML.Fromhtml(SomeString).tostring()
and
SomeString.replace("'","\\\'")
but with no good result
the Result always
MacDon
Fast
any good ideas ?!
Make sure your apostrophe is ' and not special one like ‘
From android developer documentation
you can set apostrophe from XML
Single quote (')
Any of the following:
1. '
2. \'
3. Enclose the entire string in double quotes ("This'll work", for example)
Sample
1. <string name="travelers_details">Traveler's Information</string>
2. <string name="travelers_details">Traveler\'s Information</string>
Apastrophe cannot be directly displayed. Instead of directly placing Apastrophe use the below when you encounter the apastrophe as below:
if (YourString.contains("'")) {
YourString= YourString.replaceAll("'", "\'");
}
This will help you
I believe, calling yourTextView.setText("MacDonald's"); will simply set MacDonald's inside your TextView. You don't need anything fancy to get it printed.
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.