I'm working on a project where I've defined several strings to use on my project.
I want to use a string to be displayed as my subtitle of the page on the toolbar. The reason I'm using strings is because I want my app to be translation supported.
Here is how I use subtitles on the toolbar of my activity:
android.support.v7.app.ActionBar ab = getSupportActionBar();
ab.setTitle("Title");
ab.setSubtitle("Subtitle");
I want to use a string on java (like #string/helloworld in xml) but I don't know how can I do that.
Can anyone help me?
In your "res" directory, there might be "strings.xml" file. (If you didn't remove it). Add string tags like bellow code snippets.
<string name="title">Title Message</string>
<string name="sub_title">Sub Title Message</string>
And in your java file.
String mStringTitle = getString(R.string.title);
String mStringSubTitle = getString(R.string.sub_title);
You can also use these string resources in your layout XML like follows.
<TextView
android:text="#string/title" />
For more information, please refer to the bellow URLs.
What is the string resource in Android? android_string_resources
How to support multiple locales?
support_different_language //
different-locales
In this case, use R.string.helloworld, because these methods require a resource ID.
This is the code -
R.string.message_failure
Implementation of the above code -
snackbar = Snackbar.make(constraintLayout,R.string.message_failure, Snackbar.LENGTH_SHORT)
//String_file.xml code -
<string name="message_failure">Failure</string>
Related
I have a data in Firebase like:
-Moon1
sm_1 : "Titan"
-Moon2
sm_2 : "Europa"
-Moon3
sm_3 : "Triton"
-Moon4
sm_4 : "Io"
My strings.xml file is like below
<string name="sm_1">/*Data from firebase*/</string>
<string name="sm_2">/*Data from firebase*/</string>
<string name="sm_3">/*Data from firebase*/</string>
<string name="sm_4">/*Data from firebase*/</string>
I want the string sm_1 to have the data Titan, sm_2 = Europa and likewise. I am able to retrieve the data from Firebase and assign to a Listview.
But how to assign to a string in strings.xml?
I am fairly sure you cannot create string resources on runtime as they are compiled into your apk when you are bundling your project. What you are thinking is persisting your data from Firebase, which can be done in many ways.
You could save your instance state, create a Room database, a SQL database, use shared preferences even. However since you already seem to have a Firebase database set up maybe use that always.
It is actually possible by following codes but however, this ain't a good practice at run time. Instead, try saving strings in a variable then setting your data to adapter or etc.
In your Strings.xml:
<string name="sm_1">%1$s</string>
In Java-Kotlin side:
val yourString = "Titan" // Or you can set this from FireBase output
val formatted = getString(R.string.my_xml_string, yourString)
I am working with an Android app, which shows some text in Bengali.
This sentence is in the app resources string file:
<resources>
<string name="sentence">১৯৭৫ সালে বিল গেটস(Bill Gates) এবং পল এলেন(Paul Allen) একসাথে "মাইক্রোসফট"("MicroSoft") নামক কোম্পানি প্রতিষ্ঠা করেন, যেটা পরবর্তীতে পৃথিবীর সবচেয়ে বড় পিসি কোম্পানির মর্যাদা পায়।</string>"
</resources>
I want to show this whole sentence in Bengali, so I set typeface to my textview and I can use different Bengali fonts, but the problem is I need to show names (Bill Gates, Paul Allen and Microsoft) in English instead of Bengali font. How can I do it?
You can use HTML commands in your text view and then apply different typefaces to different parts of this TextView.
You can do this:
textView.setText(Html.fromHtml("<font size=... color=... face=...>" + getString(R.string.sentence) + "</font>"))
This would set the typeface of the whole sentence to the defined one. To do this only on parts, save the String resource to a local variable, and then apply the different Html commands to the desired parts.
Also see here and for the allowed Html tags here
I've read others posts about this link warning but they didn't solve my problem and I still don't get what I've doing wrong.
In default file strings.xml, I have:
<string name="rating_dialog">You\'re about to rate this app with %s stars.</string>
Later, I call it inside an override onClick method:
v.getContext().getString(R.string.rating_dialog, String.valueOf(rating))
It's appearing like:
You're about to rate this app with {String.valueOf(rating)} stars.
Link Warning:
Format string is not a valid format string so it should not be passed to string.format
Note:
v is a View and rating is an int value.
I've checked other strings.xml files for other languages, and all translations seems alright.
Solved: for hindi string.xml, instead of %s, there was only %.
You need to assign a position to each placeholder you define in your XML.
Your string then becomes:
<string name="rating_dialog">You\'re about to rate this app with %1$s stars.</string>
You can do it like below:
In string.xml:
If you want to assign integer value then use "%1$d" and if String then use "%1$s"
1 is use for defining position. If you want to use multiple values then you can use "%1$d","%2$d",....etc
<string name="rating_dialog">You\'re about to rate this app with %1$d stars.</string>
Then in code do it like :
v.getContext().getString(R.string.rating_dialog, rating);
Hope it helps you.
I'm working an Android app and want to translate it to Persian language.
All String values in the project are saved into an resource XML file.
Some String values are used in layout files and some in classes (R.String). When using Persian text in Android, it has to be reshaped to be displayed correctly.
I want to reshape all resource String values without calling reshape method for every String value.
Can anyone explain me how to do this?
Some idea:
override String class
override getResource class
Android provides already functionality (well documented) to have a multilanguage app. Instead of replacing your text and having severall versions of your app for each language, its better to use the frameworks function. In that case the app chooses a suitable language from the available languages you provided depending on the device locale settings.
So basically you would start by creating the required directory structure (link above):
MyProject/
res/
values/
strings.xml
values-fa/
strings.xml
and then you fill in String values into the files, e.g.:
English (default locale), /values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">My Application</string>
<string name="hello_world">Hello World!</string>
</resources>
Persian, /values-fa/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">...</string>
<string name="hello_world">...</string>
</resources>
(quoted and adapted from the link above)
Solution for reshape farsi language
The reshape function has to be used for every Farsi String value, so setting string in the XML layout isn't possible (AFAIK), so the following proposals assume, that all String values are set programatically.
Using a global static wrapper function #1
public static final String getLocalizedString(int resId) {
if(Locale.getDefault().toString().equals("fa_IR")) {
return PersianReshape.reshape(getResources().getString(resId));
}else{
return getResources().getString(resId);
}
You can now use this function to load the String (you have to change each occurence) or you override e.g. the getRessource method. I personally would prefer using a static function instead of overriding because of possible other problems regarding loading resources of other type than String, side effects etc.
Creating a custom class with overriding setText() for each used ui widgets #2
Another possibility is to create custom ui widgets that do a call to PersianReshape.reshape() when display. E.g. for EditText:
class CustomTextField extends EditText {
#Override
public void setText(String text) {
super.setText(PersianReshape.reshape(text));
}
}
[...]
CustomTextField myTextBox = new CustomTextField();
myTextBox.setText("....");
I've got few questions about Android and SCORM. In both areas I'm pretty new and I only spent one evening digging the web in search of some answers.
Topics I found were about synchronizing SCORM package with LMS but I do not need that. I'm just wondering how to PLAY (and just play, no need for any syncing or tracking) SCORM package on android device (Lenovo tablet with Android 4+ OS). If I try to make my own application which allows to browse local SCORM packages, will I be able to launch SCORM by using WebView component?
I found this tutorial:
http://support.scorm.com/entries/21826060-RSOfflinePlayer-Developer-Tutorial
which has section:
Playing Content and Syncing Results
where I found some interesting source code about configuring this WebView component in order to play SCORM content, but I'm not really sure if I need RSOfflinePlayer.jar for this.
I've also heard, that if device supports Flash, I will be able to launch SCORMs with Browser - is it true?
Maybe you know some application which can do that? Or library which could help?
Is there anyone with experience in:
1) Java SCORM API:
would paste URL, but I need more reputation
2) Celine
https://code.google.com/p/celine-scorm/
Any help will be appreacieted, not only by me but also by children with different kinds of diseases (we are just students trying to help them).
Javier is almost right. I will nonetheless try to explain this again. Maybe you will gather more information from this.
Every SCO is basically a zipped webpage. You have to unzip it and look for imsmanifest.xml, find the initial file in there (index.html, player.html, something like this). It will NOT be located under resources. You first have to look at Organizations > Organization > Item > Identifierref, which will give you an ID. Then you have to look at Resources > Resource with the above ID > href value. This is the file you're looking for.
Example (index.html is the file you need):
<organizations default="someorg">
<organization identifier="someorg">
<title>Some Title</title>
<item identifier="CourseItem01" identifierref="SCO_Resource_01" isvisible="true">
<title>SCO Title Here</title>
</item>
</organization>
</organizations>
...
...
<resources>
<resource identifier="SCO_Resource_01" type="webcontent" adlcp:scormtype="sco" href="index.html">
<file href="index.html"/>
<file href="SCORM_API_wrapper.js"/>
...
Once you found it, just open it in WebView and it'll try to connect to SCORM API in the parent window. You'll have to provide some dummy functions to fool it into thinking that it did connect to LMS and carry on as usual. Otherwise it will either fail or throw alerts at you.
I don't have any Android experience, but I have some experience working with SCORM.
To play a SCORM object, you need to open the right file inside the right environment, the right file is stated in the imsmanifest.xml file, that will be always in the top level of the zip package, you have to look for something like this:
<resources>
<resource identifier="546468" type="webcontent" href="index.htm" adlcp:scormtype="sco">
<file href="index.htm" />
</resource>
</resources>
This means that you have to open index.htm in the top level, in general you have to look for the first resource with adlcp:scormtype="sco" (if you need more details, read the SCORM spec).
When this page loads, it will look for the API object, it must be in the parent window, or parent frame, you will need a dummy SCORM API, something like:
function ScormAPIClass()
{
this.GetLastError = function (){return 0};
this.GetErrorString = function (param){return ""};
this.GetDiagnostic = function (param){return ""};
this.SetValue = function (element, value){
//you need something else here
return true};
this.GetValue = this.SetValue = function (element){
//you need something else here
return true};
this.Initialize = function (param){return true};;
this.Terminate = function (param){return true};
this.Commit = function (param){return true};;
this.version = "1.0";
}
window.API_1484_11 = new ScormAPIClass();
The SCORM objects will assume that you API works, so, if the set and get functions are not real this can generates errores depending on the object logic.
Also, I did not tested the code, is only to give you an idea of what you need.
I hope this help you.
First you have to understand structure of Scorm.
You can see Scorm package is a zip file containing several folders right and a manifest file.
First you have to unzip that zip package of Scorm and then you have to parse that imsmanifest.xml file and maintain two lists one containing titles and other addresses of html files corresponding to that title.
I have used sax2r2 parser to parse that manifest file and got that two array lists one containing title and other addresses of html files.
Later you just have to fill up you IOS list with titles array, and when user click on any title of that list get the position of list and retrieve the address of html files corresponding to that title from addresses array list.
finally you can open html file in webview of your IOS, make sure have enabled parameters required for open scorm html5 file.
In android I have enabled and set these values this is java code but it may help you.
WebViewClient webViewClient = new WebViewClient();
webView.setWebViewClient(webViewClient);
webView.clearCache(true);
webView.getSettings().setUseWideViewPort(true);
webView.setInitialScale(1);
webView.getSettings().setBuiltInZoomControls(true);
webView.clearHistory();
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.loadUrl("file://" + open_scorm.scorm_path
+ open_scorm.scorm_name + "/" + open_scorm.href.get(0));
webView is used to open html/html5 files in android and i have enabled above settings in android, these settings are by default in android, may be in ios you just have to load that html file and dnt have to enable all these values.
In above you can see I am retrieving href.get(0) which is first html5 file of scorm.
In simple words you just have to unzip scorm , parse imsmanifest.xml file and get data of it and use it to open/parse scorm.