I'm builind an app to iOS and Android with AppCelerator.
So I have created Under i18n, two folder
i18n
--en
----string.xml
--it
----string.xml
In the string.xml I have insert some resources.
Now how can I change the language of application at runtime???
I'm try to do this but not works:
Ti.App.Properties.setString('SETTING_LANUAGE','it');
There are many ways to change the app language at runtime but it will only change your app's language, not the entire device language. So I hope this is what you want to achieve.
The simple solution I know is that:
e.g. Use L("appTitle") method to change the language.
Here is a short code snippet to use above method:
en -> strings.xml
<string name="en_home">Home</string>
<string name="en_cl">Change Language</string>
it -> strings.xml
<string name="it_home">Casa</string>
<string name="it_cl">Cambia lingua</string>
alloy.js
// you can set this to any language at default or you can save the last applied language in the app
Alloy.Globals.Lang = "en_";
index.js
var win = Ti.UI.createWindow({
backgroundColor : "#aaaaaa"
});
var lang = Alloy.Globals.Lang;
var label = Ti.UI.createLabel({
text : L(lang + "home")
});
var btn = Ti.UI.createButton({
title : L(lang + "cl"),
bottom : 50,
color : 'white',
backgroundColor : "#0aa"
});
btn.addEventListener('click', changeLanguageItalian);
win.add(label);
win.add(btn);
win.open();
function changeLanguageItalian() {
Alloy.Globals.Lang = "it_";
lang = Alloy.Globals.Lang;
btn.title = L(lang+"cl");
label.text = L(lang+"home");
}
Note that this is just a simple explanation of how you can change the language at run time. You can try anything you feel right according to your app. Good Luck!
Related
I am using excel dart package to create and edit excel files. I am trying to make my excel file look attractive by giving colors to excel cells.
I read the documentation here and tried to do like this :
CellStyle color1 = CellStyle(
fontColorHex: "#Ffffff",
backgroundColorHex: "#80ff00",
fontFamily: getFontFamily(FontFamily.Calibri),
);
CellStyle color2 = CellStyle(
fontColorHex: "#Ffffff",
backgroundColorHex: "#Ea4a73",
fontFamily: getFontFamily(FontFamily.Calibri),
);
var cell = sheetObject.cell(CellIndex.indexByString("A1"));
//changing cell style to color 1
cell.CellStyle = color1;
//now again changing cell style to color 2
cell.CellStyle = color2;
Problem with this:
When I try to change the cellStyle to color2 it persist the old values of color1. Why this is happening and how can I fix this, Is there any property to remove the cellStyle first and then assigning to new CellStyle. Thanks in advance
The issue I found was that cell.CellStyle should be cell.cellStyle. if that was a mistake while adding code here, Then for your information, the current code works for me without any issue. try to delete the excel file been exported and clean the project and run the code again.
I am using the "setMovementMethod" to make clickable an url on a TextView. Better look the follow code:
1. String value = "By signing up, you agree to our My App Term and confirm that you have read our Privacy Policy";
2. TextView text = (TextView) findViewById(R.id.text);
3. text.setText(Html.fromHtml(value));
4. text.setMovementMethod(LinkMovementMethod.getInstance());
The problem is about the slash just after ".com" of the url. If I remove that slash and I write the url like that https://app.mywebsite.com then it works perfectly but when I write the url like that https://app.mywebsite.com/terms then the link isn't clickable. I can see the link highlighted but when click on the link then it does not work
How I could resolve this? Thank you very much.
Firstly,
String value = "By signing up, you agree to our My App Term and confirm that you have read our Privacy Policy";
is illegal because - you are not allowed to use double-quote i.e. " inside another double-quote.
So, the correct form should be:
String value = "By signing up, you agree to our <a href='https://app.mywebsite.com/terms'>My App Term</a> and confirm that you have read our <a href='https://app.mywebsite.com/privacypolicy'>Privacy Policy</a>";
When I do this change, I am able to click the link, or else, the code will not even compile.
Secondly, have a check on the Build version and use the two parameter version of the Html.fromHtml(string, int)
More information on it is here:
Create a function look like:
fun applyHtmlToTextView(tv: TextView?, html: String) {
if (tv == null)
return
tv.movementMethod = LinkMovementMethod.getInstance()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
tv.text = Html.fromHtml(html, Html.FROM_HTML_MODE_COMPACT)
} else {
tv.text = Html.fromHtml(html)
}
}
Add your string to the string.xml file as below:
<string name="lb_your_string"><![CDATA[<a href="https://google.com”>Google.</a>]]></string>
And using it by adding the code:
applyHtmlToTextView(tv, getString(R.string.lb_your_string))
OR you can edit the value variable to:
String value = "By signing up, you agree to our <a href=https://app.mywebsite.com/terms>My App Term</a> and confirm that you have read our <a href=https://app.mywebsite.com/privacypolicy>Privacy Policy</a>"
Removed \"
My engironment is titanim 6.0.1.GA
It doesn't show the label on Android, while iOS show the label correctly.
var descriptionView = Ti.UI.createView({
height:'100%',width:'100%'
children:[Ti.UI.createLabel({
wordWrap :true,top:0,
color:'black',
text:"my label",
})]
});
It works well both on Android/iOS
var descriptionView = Ti.UI.createView({
height:'100%',width:'100%'
});
var label = Ti.UI.createLabel({
wordWrap :true,top:0,
color:'black',
text:"my label",
});
descriptionView.add(label)
I just wonder using children is bad behaivor for andorid?
However it sometimes very useful to simplify the code.
Is there anyone who uses children successfully for Android??
According to the titanium API 'Children' property is a read only property and it should not be used to set data. It's considered to be good luck as it's working with IOS but with Android we need to be specific with the code.
I would never suggest you to use this coding style to simplify the code, rather you could use the following to simplify and also memory effective way :
var descriptionView = Ti.UI.createView({
height:'100%',width:'100%'
});
descriptionView.add(Ti.UI.createLabel({
wordWrap :true,top:0,
color:'black',
text:"my label",
}));
Good luck,
Cheers
I am new in Titanium alloy and I would like to change my project from titanium default template to alloy. Below is the code for creating a text box in default template. I would like to change this to alloy template.
var checkbox = Ti.UI.createSwitch({
id:'checkbox',
style:Ti.UI.Android.SWITCH_STYLE_CHECKBOX,
});
Not hard at all! Try this inside your Alloy XML view markup:
checkbox.xml
<Alloy>
<Switch id="checkbox"/>
</Alloy>
Now we can use the style file to set attributes based on the id.
checkbox.tss
"#checkbox[platform=android]" : {
style:Ti.UI.Android.SWITCH_STYLE_CHECKBOX
}
This will set the style to checkbox, also note that I set this to only happen for android.
Alternatively, if we wanted every switch to be of the checkbox style we could set this inside app.tss:
"Switch" : {
style:Ti.UI.Android.SWITCH_STYLE_CHECKBOX
}
You can create the check box like this.
var checkbox = Ti.UI.createSwitch({
style: Ti.UI.Android.SWITCH_STYLE_CHECKBOX,
textAlign:Ti.UI.TEXT_ALIGNMENT_LEFT,
title:'Notice Me',
value:true,
width: 300,
left: 18
});
win.add(checkbox);
checkbox.addEventListener('change',function(e){
//function
Ti.API.info('Switch value: ' + checkbox.value);
});
I have an iPhone/ Android app built using Appaccelerator. I have translated all text by placing it into locales (i18n//strings.xml) ,and I have also implemented a switch :
<label for="flip-1" class="username">Language:</label>
<select name="flip-1" id="flip-1" data-role="slider">
<option value="off">EN</option>
<option value="on">NL</option>
</select>
which works reasonably well on Android and iPhone.
How can I enable this select element to update active language locale within the app, so the language of the application would change, and is this possible without restarting the app?
There is no way to change current locale in runtime. Locale depends on current language of a cell phone, but you can still do some work around.
Create strings.xml file in your i18n directory and put all your texts there like this:
<resources>
<string name="en_car">Car</string>
<string name="de_car">Auto</string>
</resources>
The you can implement your own L() function:
Ti.App.defaultLang = "en"; // global variable with default language (set it in app.js)
function myL(str, lang) {
if(lang && str) {
return L(lang + '_' + str);
} else {
return L(Ti.App.defaultLang + '_' + str);
}
}
I haven't test it, just want to give you some example to deal with it. You can maybe pass also hint option like in Ti docs: http://docs.appcelerator.com/titanium/2.1/index.html#!/api/Titanium.Locale-method-getString