I am writing my first Android app. It will have hundreds of menu links that will change with each version released. I am looking for a way to process the onclicks based on the Resource ID.
Menus are defined in XML like so:
<item android:id="#+id/Spring_2013" android:title="Spring 2013" android:showAsAction="ifRoom|withText" />
The onclicks will load a URL stored in strings.xml:
<string name="Summer_2013">file:///android_asset/catalog/current/genadmissionreq.html</string>
So, seems like all I need is a simple, single onclick method that loads whatever string is needed based on the selected Resource ID. The MenuItem ID is ALWAYS equal to the URL string ID in my code. So, without using hundreds of if() or switch/case statments, cant I just say:
String url = getString("R.string." + menuItem.getItemId());
Obviously that assignment wont work for a number of reasons. But you get the idea right? So, how CAN I make this work?
Im sorry for the newb question. I know it should be simple but I've been reading for days and cannot find any way to do this without an if() statement for EVERY menu item. That method would make the weekly updates a nightmare!
Try using getIdentifier()
String title = menuItem.getTitle().toString().replace(' ', '_');
id = getResources().getIdentifier(title, "string", getPackageName());
String string = getString(id);
The first line fetches Spring 2013 and converts it to Spring_2013.
The second line builds "R.string." with whatever the title is, in this case "R.string.Spring_2013".
The third line simply fetches the String resource.
Related
I am porting my Android app to its cross-platform version (to have the iOS version) by means of Codename One.
I would like to use string resources like in the Android version.
I created a localization bundle (named "Localization (L10N) 1") only with english words for now.
In the main form I put this:
theme = UIManager.initFirstTheme("/theme");
String lang= L10NManager.getInstance().getLanguage();
UIManager.getInstance().setBundle(theme.getL10N("Localization (L10N) 1",lang));
In another container class I have:
String StringRes(String id)
{
String result;
result=UIManager.getInstance().getBundle().get(id);
return result;
}
when I need a string, for example:
add(new Label(StringRes("title_string")));
I get null pointer error in StringRes method.
I know that it is just an attempt to manage string resources.
What is the right way?
You are over thinking this... Codename One is localizable by default unlike any other framework I'm personally aware of.
Once you set the bundle value all you need to do is use the label with the right key e.g.:
add(new Label("title_string"));
It will "just work". Also you can use:
String value = getUIManager().localize("title_string", "This will show if title_string is missing from the bundle");
So StringRes isn't the right direction.
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.
Recently I want to get specific line text in a textview and I want to use a webview to load the text,I ve found some code references which I think is useful but not work for me well.
Textview:
A
Http://ABC.com.jp
C
I want to load line 2 URL.
Sorry for my low responsibility, now I still updating and modifying my source code,I am new in java and this is not my main profit,besides I will keep learning.
Target =(TextView)findViewbyid(R.id.abc);
myTextView = (TextView)findViewbyid(R.id.id1);
int startL= myTextView.getLayout().getLineStart(2);
//^start line
int endL = myTextView.getLayout().getLineEnd(3);
//^end line
String getResults = myTextView.getText().substring(startL, endL);
Target.setText(getResults);
//benefits of using get line end & start is that it can select multiple line content from a large content,also it can be used in selecting single line or specific line.
So I'm pretty new to the whole Android Studio thing and I've been using the internet to help me with a lot of the things I am doing and needed help on something.
I'm not sure if it's possible to connect this to either a string or an SQL database but I have a Main Layouts with a bunch of buttons that allow me to click on them and choose what external player I would like to use to watch the video. In my MainActivity java class, this is how it finds the button.
case R.id.button3:
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("http://videoname.mp4"), "video/*");
startActivity(Intent.createChooser(intent, "Choose an External Player"));
break;"
I wanted to know if "http://videoname.mp4" url can be connected to like a string where I can always update or change the URL instead of manually going to find the URL in the MainActivity java and changing it. As of now I have to manually do it but a different way to do it would be helpful.
I'm sorry if it's all confusing, but if you know, please let me know as soon as.
Thank you.
you just need write your string URL :
open your directory folder /res/values/strings.xml -> write your string : <string name="yourStringName>yourStringURL</string>. to use your string do this
getContext().getString(R.string.yourStringName) in fragment
getString(R.string.yourStringName) in Activity
or you can write directly on your code, put your cursor on your string, then press key alt + enter choose extract string resource fill the resource name with your string name
wherever you need to use it, just do point 1 or 2. also in your Intent
hope this help you, never stop learning!
Add it to strings.xml in your project. That is the recommended way of using strings anyway.
I I18N an application.
Part of it consists in I18N-ing the menus. That is OK.
With GWT I can use the Constants interface.
Now I have to I18N the help for the app, which includes some text referring to Menus.
So in these bigger constants, I need to use the menu constants.
Exemple :
I have a resource in the property file for I18N :
menuPlay = Play ...
Now I want to define another resource = some help text:
howToPlay = In order to start the game, go to the menu ??<resource menuPlay>??
In question marks above, I want to use the resource menuPlay.
When I want to translate eg in French, the two resources would be :
menuPlay = Jouer...
howToPlay = Pour démarrer le jeu, aller au menu ??<resource menuPlay>??
How can I do that (in Java / GWT) ?
I mean is there an off-the-shelf solution (which I could not find or think of). I don't want to code some specific solution that would combine both --in real time-- based on localized info.
This would help prevent inconsistencies when changing the menuPlay resource : no need to bother on changing the other resource howToPlay (high risks to forget).
Then you property is like
menuPlay = Play ...
howToPlay = In order to start the game, go to the menu {0}
while using
inorder to get menuPlay then in java you may using like resource.menuPlay();
now inorder to get howtoPlay
resource.howToPlay(resource.menuPlay()); //now {0} replaces with Play
you can do it for no.of arguments
howToPlay = In order to start the game, go to the menu {0} {1} ..etc
see message patterns