Android-Stringbuilder Returns Numeric, Not Alpa - java

Really new to Java and Android but trying to learn. In process of creating a dialog alert I am using the stringbuilder to put together a message to the user. I am taking several lines of text from my string.xml file that looks like
<string name="lbl_deactivated_error">YOU HAVE BEEN DEACTIVATED</string>
<string name="lbl_notification_line2">Call Customer Service 1–800-xxx-xxxx-/string>
<string name="lbl_notification_line3">Email support#xxxx.com</string>
<string name="lbl_notification_line6">Please Exit By Pressing Home.</string>
My stringbuilder looks like
AlertDialog.Builder dialog = new AlertDialog.Builder(ErrorActivity.this);
dialog.setTitle(lbl_deactivated_error);
alertmessage.append(lbl_notification_line2);
alertmessage.append("\n");
alertmessage.append(lbl_notification_line3);
alertmessage.append("\n");
alertmessage.append(lbl_notification_line6);
dialog.setMessage(alertmessage.toString());
dialog.show();
When executed, the alert shows with the title but the message shows a set of numbers, not the text as shown in the strings.xml. It prints each line of the text with the returns, but I get something like
YOU HAVE BEEN DEACTIVATED
2131099688
2131099688
2131099688
Is there an obvious mistake I am making? Does it have something to do with the hyphens in the phone number maybe?

You need to get the String not the resource ID.
Whenever you build it creates an R file that holds resource IDs (ints) for your resource pointers. You want to use the getString(R.string.lbl_notification_line3) instead.

Related

How to retrieve data from Firebase and assign to a string in strings.xml in Android Studio

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)

Invalid format String containing %

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.

How to access string resource in android of text in AlertDialog [duplicate]

This question already has answers here:
Android: How do I get string from resources using its name?
(17 answers)
Closed 6 years ago.
It is possible duplicate as bottom link. For specific question I want to ask or to get a faster answer, see the answer in my question. Otherwise, go to the following link.
Android: How do I get string from resources using its name?
===========================================================================
My question :
I am asking this question even though there are possible duplicates but those didn't worked for me.
I am trying to access to
strings.xml
in AlertDialog but I can't seem to make it. I have said
.setTitle(#string/AlertTitle)
in the following code below but it didn't worked and gave out an error.
Is there any possible ways to get strings from AlertDialog or is it impossible?
Code below.
This is AlertMainActivity.java
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add a New Task") //AlertTitle
.setMessage("What do you want to do next?") //AlertMessage
.setView(taskEditText)
.setPositiveButton("Add", new DialogInterface.OnClickListener() //Add {
...
.setNegativeButton("Cancel", null) //Cancel
...
}
and strings.xml
<string name="AlertTitle">Add a New Task</string>
<string name="AlertMessage">What do you want to do next?</string>
<string name="Add">Add</string>
<string name="Cancel">Cancel</string>
Thanks!
When using String resources programmatically you should use
setTitle(getString(R.string.AlertTitle));
instead of
.setTitle(#string/AlertTitle);
Also for this particular case there are two methods for setting the Title. Either trough the resource id, which would be R.string.AlertTitle or by setting a String, which can get from getString()
.setTitle(R.string.AlertTitle);
see Android: How do I get string from resources using its name? for more information. You can do getString instead of getResources().getString(...) when using it from an Activity or Fragment
When you are referencing string resources in Java code, the correct way to do it is: R.string.string_name.
So your code should work like this:
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle(R.string.AlertTitle) //AlertTitle
//...

How do I change the text shown in GooglePlayServicesUtil.getErrorDialog()?

I am trying to follow the gcm tutorial from Googles docs. They say to call this method if Play Services are out of date:
GooglePlayServicesUtil.getErrorDialog(resultCode, activity, 9000).show();
That's fine, but it puts a dialog that says "This app won't run unless you update Google Play Services" with an "Update" button. I want to change the title and the message text. My users CAN skip the update, and the app will still run. They just won't get push notifications. How do I change the message of the dialog?
I would like to do something like:
Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(resultCode, activity, 9000);
errorDialog.setTitle("out of date");
errorDialog.setMessage("Please update or push notifications won't work. Thank you");
errorDialog.show();
You can override desired string value in your application's strings.xml like this. Simply add these lines in your strings.xml
For message on dialog
<string name="common_google_play_services_update_text" msgid="448354684997260580">This app won\'t run unless you update Google Play services.</string>
For title on dialog
<string name="common_google_play_services_update_title" msgid="6006316683626838685">out of date</string>
EDIT
You could find more information here
http://blog.elsdoerfer.name/2010/04/08/android2po-managing-android-translations/
and What's the meaning of attribute 'msgid' in strings.xml?
msgid is used in android internal strings for localization but I never found any documentation about it. Two reference I have as above. I believe if you remove the msgid still it would work, although I never tried it.
The source of this code is
android_sdk\extras\google\google_play_services\libproject\google-play-services_lib\res\values\common_strings.xml

Load a string resource based on selected MenuItem resource ID - Android JAVA

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.

Categories