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.
Related
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.
Say I have a list of items, and one of the views in each item is a message, in a simple TextView, that is populated in code (dynamic data from a backend service).
<TextView
android:id="#+id/message"
style="#style/DefaultText"
/>
Visually it doesn't need a label; the list item layout is pretty clear. But listening to how TalkBack reads the screen, I think it would be beneficial to have a "label" or description for it. So that TalkBack would read something like:
Message: [the actual dynamic message content]
My question is: is it possible to add a label/description for a TextView that, firstly, does not replace the TextView content, but is read along with it, and secondly, only affects TalkBack (not the visual presentation)?
What I tried:
contentDescription for the TextView. Doesn't work: if set, the actual content is not announced, only the contentDescription. Hmm, maybe if I'd set this in code with the description prepended to the actual content... but is there no easier way?
Separate TextView with labelFor pointing to #+id/message. The problem is that it's also shown visually and screws up the design. If I make it not visible, one way or other, it seems TalkBack won't read it either.
Alright, I found two solutions! As mentioned in the question, I'd rather have avoided doing it in code, but it seems for optimal results that's the way to go.
Use android:hint in XML
The most simple way is to set android:hint="Message" for the TextView. TalkBack reads it after the actual text:
[the actual dynamic message content] Message
For my needs, with the message being optional, this has some drawbacks: the hint is announced even if the TextView has no actual content (I'd prefer to omit it then), and in that case it's also shown visually (not what I want). Also, I'd prefer TalkBack to read the hint before the actual text.
Set contentDescription programmatically
Not as simple, but not complicated either, and you have complete control over what TalkBack says.
In my case, preferring to omit the description if message is missing and having it read before the message content, I'd do something like this in the code that populates the list items:
textView.setText(message);
if (!TextUtils.isEmpty(message)) {
textView.setContentDescription(
context.getString(R.string.message_desc, message));
}
With resource:
<string name="message_desc">Message: %s</string>
You can use this namespace it not visually show on real screen
xmlns:tools="http://schemas.android.com/tools"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="your text"/>
In your XML file, set the importance to "no". You can also do this at run time using the View.setImportantForAccessibility() API.
android:importantForAccessibility="no"
Programmatically you can say:
view.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
Example click here
click here for more info
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.
Background
Sometimes I see some weird attributes on the "strings.xml" file made by Google's samples, for example, on the chips example (code available here), I can find this strings file of "res/values-en-rGB" (for English-Britain) :
<resources xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="more_string" msgid="8495478259330621990">"+<xliff:g id="COUNT">%1$s</xliff:g>"</string>
<string name="copy_email" msgid="7869435992461603532">"Copy email address"</string>
<string name="copy_number" msgid="530057841276106843">"Copy phone number"</string>
<string name="done" msgid="2356320650733788862">"Return"</string>
</resources>
I think both are used only for localized strings, as I never saw them inside "res/values" folder.
The question
What do those attributes mean?
What does the value of "xliff" mean?
When should you use them and what should you put there?
Are they even needed?
Is there any documentation about those things?
On Android Developers Localise your app page, search for xliff in the section named "Mark message parts that should not be translated".
The explanation is as follows:
Often strings contain contain text that should not be translated into
other languages. Common examples might be a piece of code, a
placeholder for a value, a special symbol, or a name. As you prepare
your strings for translation, look for and mark text that should
remain as-is, without translation, so that the translator doesn't
change it.
To mark text that should not be translated, use an
placeholder tag.
The suggestion is that text within the <xliff:g></xliff:g> tags should not be translated. These tags can also provide metadata about the non-translated text.
When you declare a placeholder tag, always add an id attribute that
explains what the placeholder is for. If your apps later replace the
placeholder value, be sure to provide an example attribute to clarify
the expected use.
For more information on the actual xliff tool, rather than how it relates to Android strings, check out the related question:
What does this mean "xmlns:xliff"? XML.
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