So I have a challenge similar to this post, to change the OK and Cancel button texts on Android and have been reading about Android resources, therefore decided I shall have my \Resources\values\Strings.xml edited in my Android project and containing two dedicated ok/cancel key-values.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="library_name">My App</string>
<string name="ok">Fine</string>
<string name="cancel">Nope</string>
</resources>
File build action for my XML is set to AndroidResource, although this does not impact the Picker ok/cancel button texts. What have I missed? It has to be working according to docs.
Thanks in advance
this does not impact the Picker ok/cancel button texts.
Your Application string resources:
Resource.String. --> Resources\values\Strings.xml
System string resources:
Android.Resource.String.
For more Android.Resource information, you could read this.
You could find it in Source code that it use the android string resource :
builder.SetNegativeButton(global::Android.Resource.String.Cancel, (s, a) => ...
builder.SetPositiveButton(global::Android.Resource.String.Ok, (s, a) => ...
So you cant change the Ok and Cancel text. As mister_giga said, if you still want change the text, you have to reinvent whole bicycle.
Update :
I have write a custom PickerRenderer to implement your feature, and I have post my code here . Hope this can help you.
Effect.
Related
My problem is very simple: I've started an app for playing Darts. The app will have several activities ('pages').
One page will be about the rules of the game. I'll be using a scroll layout because it's quite some text. But how to get the text there?!
I assume working with strings is not the best way? Do I use the XML file to get the text on screen then or does it work via Java (Assetmanager)?
Maybe there are sample apps in which large chunks of text are used?
I know this really might seem like a trivial question but I haven't a clue where to begin.
Thanks in advance!
You should put your string in your strings.xml in your res\values folder.
You can define strings by ID which allows easier internationalization (i18n), so that you can easily adjust the strings used in your app to locale (which is done automatically using resource identifiers, and it falls back to strings.xml if it can't find a strings-hu.xml in case you have Hungarian locale set as system language).
You can also define string-array and the like in XMLs. Then all you need is create a layout XML with a ScrollView in it that has a TextView in it and then you set android:text="#string/rules" for that TextView and you're done.
It is so simple my friend.
You can simply use TextView and in "android:text" you refer to the string that you delared in strings.xml file (by its name)
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="#string/text_name"
/>
If your text is dynamic, you can modify it in Java code!
Make a String Resource like this.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string1"> your text </string>
<string name="string2"> your text </string>
</resources>
and access like this if you are in Activity.
getResources().getString(R.string.string1);
If you are a begginer you should read some tutorials after post a question...
I give you a three nice tutorials below :
Want to Learn How to Program for Android? Start Here
Android Programming Tutorial
Android Development with Android Studio or Eclipse ADT
About your question, if you don't know how to use the string.xml resource just read the string-resource guide
Hope it helps.
I used eclipse for training in android but now using Intellij IDEA 13. I am used to the graphical string editor of eclipse which can alphabetically sort the string.xml file for me.
I want to do the same in intelliJ but couldn't find any solution.
This is how code look like
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ActionBar App</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>
and I want to sort it alphabetically like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="action_settings">Settings</string>
<string name="app_name">ActionBar App</string>
<string name="hello_world">Hello world!</string>
</resources>
How to do this ? Please help!
The arrangement feature allows you to setup rules to arrange the order this appear in a file. Arrangement of attributes, particularly for Android files, was add via IDEA-72907. You can get a predefined arrangement style for android by going to Settings > [Project Settings] > Code Style > XML {Arrangement Tab} Click on the "Set from..." link near the top right and select Preferred Style > Android. That will populate the XML style (and arrangement) settings with a predefined style that you can tweak.
That said, I am unsure if the arrangement feature has the ability to sort by the attribute's value. I know it can sort the attributes in a particular order. If I get a chance, I'll see if I can find the syntax to sort by the attribute's value. In the meantime, you can take a look.
Also, you can try either the Lines Sorter or SortSelection third party plug-ins. A bit more basic, but one might work for you.
I found this link Rearrange Attributes in Android XML Files with IntelliJ IDEA 13
I chose the second option.
As mentioned go to the settings/Editor/Code Style/XML/Arrangement
Reordering the arrangement and changing to be reorder by name:
This won't sort the strings.xml by name, so
copy and paste into an excel worksheet and sort by A-Z
I am posting this two years after the fact, but there's another way to sort lines in IntelliJ IDEA that I'd like to mention here -- i.e. by using the IDE's Vim features.
If you want to sort a range of lines alphabetically -- say, lines 3 through 6 -- just do the following:
Open the Tools menu and make sure that 'Vim Emulator' is checked.
Type the following command in your editor:
:3,6sort
That's it -- your lines will now be sorted. All you need to do now is disable Vim if you don't use it. You can do this by Opening the Tools menu and unchecking 'Vim Emulator'.
Note that this isn't an XML sort -- it's an alphabetic line sort so spaces and so on could get in the way of a successful sort. But for simple cases like the one you mention above, this should work.
Darryl
You can't sort XML elements by its attribute value, e.g. 'name' via File → Settings → Editor → CodeStyle → XML → Arrangement. You only can sort the attribute order inside a tag or can sort the elements by its tag name.
For Android there is a plugin called AndroidXMLSorter (can only sort by attribute 'name', only in Android Projects)
If the entries are written in a single line you can select the text and use: Edit → Sort Lines (no need for a plugin)
http://docs.fusioncharts.com/charts/contents/Styles/Font.html
I tried this, along with a lot of things but failed to do so.
Here's what I want.
<string name="ss">Bold. Underlined. Italic. Big. Small</string>
I want to format a little bit of the string.
Where it's written bold, I want it to be bold...and same for others.
I tried a lot of tags ...but well nothing worked, and I couldn't find anything on Google or SO.
I know how to do it in a textview, but that's not what I want...
I'm sending some text resource to an activity that shows it...
If I did it with different text views, I'd have to create several of them, a new one for whenever I want bold text, and that's not very elegant.
Is there a way to simple do this in the XML file ? or some other way ?
Try wrapping your marked up text in CDATA tags. For example:
<string name="ss"><![CDATA[<b>Bold.</b> <u>Underlined.</u> <i>Italic.</i> <big>Big.</big> <small>Small</small>]]></string>
And then use Html.fromHtml wherever you're wanting to display it:
Html.fromHtml(getString(R.string.ss))
This problem has been driving me crazy for ages. It's something sooo simple that you just want it to work!!!
Anyway I've found an answer here at http://www.coderzheaven.com/2011/06/19/styling-text-in-android-through-xml/
The key is to load the resource as a CharSequence using getResources().getText(R.string.xxxx) this will retain all the style information and allow you to use inline styling tags.
My mistake was using getString() because when loading your resource getString() will cause the string to lose all its style information.
exemple:
<string name="ss"><font size="15"><b>Parrainage</b></font><u>subscribe</u></string>
b = bold et u = underline .....etc
This is working for me.
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
txt.setText(Html.fromHtml(getString(R.string.welcome_messages)));
more details check Official site:
https://developer.android.com/guide/topics/resources/string-resource.html#StylingWithSpannables
in dimens file write:
<dimen name="size_edittext">180dp</dimen>
and in your xml layout or activity call it:
android:#dimen/ size_edittext
I'm looking for a way to provide an additional line or two of text (in a smaller than usual font), in some of settings form components in my app. Such an additional text is referred to as 'secondary text' in many places in Android design guide (e.g. here: http://developer.android.com/design/patterns/settings.html).
However, I cannot find any way to specify it in components, such as TextView or Checkbox.
I'm sorry, but as a new user, I'm not allowed to post pictures here. The examples can be found in the Design Guide linked above :)
Thanks :)
The 'secondary text' is known as the summary.
So in xml you could have
<CheckBoxPreference
android:key="#string/keep_screen_on_KEY"
android:summary="#string/general_battery_warning_one"
android:title="#string/keep_screen_on_title" />
where
<string name="keep_screen_on_KEY">keep_screen_on_key</string>
<string name="general_battery_warning_one">Increases battery drain</string>
<string name="keep_screen_on_title">Keep display on</string>
This would give you a checkbox in preferences
You can change this in code with the .setSummary(CharSequence summary) method, which is available for ListPreference and CheckBoxPreference.
It doesn't seem like it's possible to add a new line /n to an XML resource string. Is there another way of doing this?
use a blackslash not a forwardslash. \n
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Hello\nWorld!</string>
</resources>
Also, if you plan on using the string as HTML, you can use <br /> for a line break(<br />)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Hello<br />World!</string>
</resources>
I know this is pretty old question but it topped the list when I searched. So I wanted to update with another method.
In the strings.xml file you can do the \n or you can simply press enter:
<string name="Your string name" > This is your string.
This is the second line of your string.\n\n Third line of your string.</string>
This will result in the following on your TextView:
This is your string.
This is the second line of your string.
Third line of your string.
This is because there were two returns between the beginning declaration of the string and the new line. I also added the \n to it for clarity, as either can be used. I like to use the carriage returns in the xml to be able to see a list or whatever multiline string I have. My two cents.
After I tried next solution
add \n
add \n without spaces after it
use "" and press Enter inside text
text with press Enter
use lines=2
What solves me is br tag
<string name="successfullyfeatured">successfully<br/> You are now a member</string>
Update
the most reliable solution is to use Translation editor and edit text and it will handle new lines for you
This is an old question, but I found that when you create a string like this:
<string name="newline_test">My
New line test</string>
The output in your app will be like this (no newline)
My New line test
When you put the string in quotation marks
<string name="newline_test">"My
New line test"</string>
the newline will appear:
My
New line test
When using the translations editor in Android Studio just click the icon to the right (or use Shift-Enter on Windows, Alt/Option-Enter on MacOS) and then add line breaks using return.
This will insert \n correctly in the localized strings.xml.
In the latest version of Android studio, "\n" is going to be printed like it was meant to be there unless the whole string it's in apostrophes
For Example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">"Hello\nWorld!"</string>
</resources>
I have tried two options, both are efficient and easy.
I used <br/> HTML tag
<string name="name_detail">
Hey user. <br/>
Enjoy a new shopping deal.!!
</string>
Add an encoding line at the top of strings.xml
<?xml version="1.0" encoding="utf-8"?>
and use "\n" to break line
Example
<string name="name_detail">
Hey user.\n
Enjoy a new shopping deal.!!
</string>
If you put "\n" in a string in the xml file, it's taken as "\\n"
So , I did :
text = text.Replace("\\\n", "\n"); ( text is taken from resX file)
And then I get a line jump on the screen
don't put space after \n, otherwise, it won't work. I don't know the reason, but this trick worked for me pretty well.
You can use this format
<string name="test">
"This is
a new line"
</string>
I just faced this issue.
didn't work on TextView with constraint parameters. Adding android:lines="2" seems to fix this.
I have tried
1-Top word \n Bottom word
2-Top word ENTER Bottom word
Didn't work
And finally <br/> worked for me in android studio
In fact the correct way to use it is \n although it is possible to see it done after Build the application. The only has an effect on the design and not on the application
<string name="available_balance">Available\nBalance</string> //Look good on the app and Available\nBalance in android studio
<string name="available_balance">Available<br/>Balance</string> //Look good in android studio and Available Balance on the App
finally, I found the solution.
the <br/> is working on design only and it's not working in running app. the \n is working on running app only and not working on design
so the solution is to use both if you want to have both results.
something like this
\n<br/>
Example:
Hello \n<br/>World.
Result in design mode:
Hello\n
World.
Result in running app:
Hello
World.
Very simple you have to just put
\n
where ever you want to break line in your string resource.
For example
String s = my string resource have \n line break here;
I want to expand on this answer.
What they meant is this icon:
It opens a "real editor window" instead of the limited-feature text box in the big overview. In that editor window, special chars, linebreaks etc. are allowed and converted to the correct xml "code" when saved
Just use "\n" in your strings.xml file as below
<string name="relaxing_sounds">RELAXING\nSOUNDS</string>
Even if it doesn't looks 2 lines on layout actually it is 2 lines. Firstly you can check it on Translation Editor
Click the down button and you will see this image
Moreover if you run the app you will see that it is written in two lines.
\n tag worked in the strings.xml file
Eg:
<string name="hello">Hello World &\nWelcome</string>
Output:
Hello World &
Welcome
Note:
Make sure that you dont add space before and after of \n"
Even if it doesnt looks on new line in the preview window of UI when you run the app it will be displayed in the next line.
To see in the design tab the line breaks and in the device i use:
<br/><br/>\n\n
(to 2 break lines)
Although the actual problem was with the forward slash still for those using backslash but still saw \n in their layout.
Well i was also puzzled at first by why \n is not working in the layout but it seems that when you actually see that layout in an actual device it creates a line break so no need to worry what it shows on layout display screen your line breaks would be working perfectly fine on devices.
Late answer but i think might help someone.
You will some times see \n in the textview in android studio's layout preview. (But it lies then). Please refresh layout.
Or deploy your app to a real Android Device to see the \n actually working in android string resource.
Have you tried turning it off and on again ?!
This is a very old question but no other answer seem to suggest this.
It could be that the layout preview on android studio is not properly displaying the textView.
So you could try exiting android studio and launching it again.
I guess this can happen if android studio was open while the computer was in sleep/hibernate etc.
Anyway, worth a shot.