I'm trying to add a string to the following alert box's positive button, but it keeps spamming out error messages.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.checkNetwork).setTitle(
R.string.checkNetworkTitle);
builder.setIcon(R.drawable.ic_warning);
builder.setPositiveButton(android.R.string.continue, new warningContinue());
AlertDialog dialog = builder.create();
dialog.show();
Any help? It does seem to work for the title and the message, however not for the button...
Thanks in advance.
EDIT: The error says:
The method setPositiveButton(int, DialogInterface.OnClickListener) in
the type AlertDialog.Builder is not applicable for the arguments
(Class, warningContinue)
So it seems to expect an integer, but I was wondering if there's any way to use strings for localization-purposes?
EDIT2:
Ok what the hell. Problem lied in the string name; it won't let me name my string continue for some reason. o_O
You don't want to use "android.R.string.continue". "package name".R.string.continue is what you want. Thats the usual way you're doing localization in android. The id provides multilanguage support automatically based on the users system language. You just have to create multiple "values" directories. https://developer.android.com/guide/topics/resources/localization.html provides you more information about localization.
Also you can't use "continue" as a name, use "str_continue" or something like that (it's the same for "return" or "break" ect.). They are reserved in java. For more information you could read the link: http://java.about.com/od/javasyntax/a/reservedwords.htm
Because you don't post the code of "warningContinue()", i think you know that it has to extend "DialogInterface.OnClickListener()". Also you should consider to write your classes first character uppercase for convention reasons, for more information read this answer on stackoverflow: https://stackoverflow.com/a/414029/2238341
R.string.warningButton is just the int in your R.java file. To get the associated string use the following line:
builder.setPositiveButton(getString(R.string.warningButton), new warningContinue() );
As other mentioned, don't use android.R.string.continue as it doesn't exist in system.
try, first go to res / values / strings.xml and declare your string as follows
<string name="checkNetwork">your String...</string>
Then you can take it as follows
R.string.checkNetwork
Just like this one and be happy ....
Related
My app uses a lot of static text and I'm trying to find an optimal way to persist and display that text. For now, I don't need to focus on localizing the text so, all the text goes into the strings.xml and that presents a lot of formatting nightmares.
Of course, it is not 100% static content, I sometimes have dynamic values in there which in my case can stay within strings.xml so, what is the right way for persisting this static text?
Static text content is exactly what you want to use strings.xml for, and you automatically get the added bonus of easier localization as you can have different strings.xml for the different languages. No code changes required, just different XML files.
Dynamic content is going to be content which changes based on user input. You can still use strings.xml to store the static portion (if any) of that dynamic content. Like the "format" string you may pass to String.format() or something similar.
Use the resources support for this, it is exactly what it was intended to do (and do it efficiently.)
Your comment is contradictory; you say:
For now, I don't need to focus on localizing the text so, all the text goes into the strings.xml
if you don't need Localizing, then why are you using strings.xml?
Of course, the answer is because regardless of localizing, strings.xml is the perfect place for this.
I don't know what kind of nightmares you have with it, but it's not different from any other string:
E.g. of a strings.xml:
<string name="refresh">Refresh</string>
<string name="order_placed" formatted="false">Order Placed: %s</string>
You can later use the same formatter for them:
getString(R.string.order_placed, "3pm")
will output:
Order Placed: 3pm
If you need new lines…
<string name="error">Something bad happened.\nPlease try again.</string>
will output:
Something bad happened.
Please try again.
And so forth.
Additionally, if you have trouble naming your resources, I've been following more or less this idea and despite the shortcomings described at the bottom, they haven't been a big deal with Android Studio fancy refactoring tools.
You could create a class called StaticBuffer or something with a static String array as a data member.
class StaticBuffer
{
static String array[];
}
Then you could initialize it in your onCreate() or any other function and use it.As it is static it's values will reflect the changes that you make everywhere.
Eg :
//Initialization
StaticBuffer.array=new String[10];
//Usage
StaticBuffer.array[0]="Item1";
PS: I got this idea from a friend of mine. :)
Here is my question and its solution regarding this problem..
Em trying to pass textview value between different activities, and em getting a problem. when i execute the code, the app crashes on opening StudentActivity, but then it shows the correct result..here is the code
LoginActivity.java
int a = Integer.parseInt(textView.getText().toString());
Intent i = new Intent(LoginActivity.this, StudentActivity.class);
i.putExtra("level", a);
startActivity(i);
StudentActivity.java
textView.setText(Integer.toString(getIntent().getExtras().getInt("level")));
IN studentActivity, Integer.toString(getIntent().getExtras().getInt("level")) => this line says Number formatting does not take into account locale settings. Consider using String.format instead.Please suggest some code..
Any help would be truely appreciated!!
Regarding the warning :
"Number formatting does not take into account locale settings. Consider using String.format instead android studio",
This is a Lint warning called "TextView Internationalization" which says :
When calling TextView#setText * Never call Number#toString() to format
numbers; it will not handle fraction separators and locale-specific
digits properly. Consider using String#format with proper format
specifications (%d or %f) instead.
So you should have written :
textView.setText(String.format("%d", getIntent().getExtras().getInt("level"))));
Use
textView.setText(String.format(Locale.getDefault(), "%d", your_int));
The correct locale will automatically be used
In you LoginActivity, you insert String! named "a" into Intent.
In your StudentActivity, you try to grab Integer!! thanks for call getInt().
Simply change whole line to
textView.setText(getIntent().getExtras().getString("level"));
I used:
textView.setText(getIntent().getExtras().getString("level"));
but it still has some warning;
when I use this:
textView.setText(Locale.getDefault(), getIntent().getExtras().getString("level"));
it is Ok;
I am trying to create an EditText with auto-capitalization and auto-correction implemented. I have manually figured out how to add InputFilters to allow auto-capitalization, though this only works after the first letter is typed, and I have had no luck with auto correction (I tried to create an InputFilter that used AutoText, but I'm not sure how all that works). Ideally, I could just use EditText.setInputType(...) to handle everything, but so far this has not worked. Is there a way to achieve this? My failed attempt is shown below (I just get normal input).
EditText mEditText = new EditText(this);
int inputType = InputType.TYPE_CLASS_TEXT;
if (auto_capitalize) {
inputType = mEditText.getInputType() | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
}
if (auto_correct) {
inputType = mEditText.getInputType() | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT;
}
mEditText.setInputType(inputType);
Please note, I am only interested in solutions for creating this EditText in code - not via XML.
Edit
I found sound new documentation describing TextKeyListener, however after trying to use this:
mEditText.setKeyListener(new TextKeyListener(TextKeyListener.Capitalize.CHARACTERS, true));
and using #farble1670's idea of using setRawInputType, so as not to affect the KeyListeners, there is still no change to the text.
Through XML it would be setup like so.
android:inputType="textMultiLine|textNoSuggestions"
You simply add a pipe (|) between variables. I see you were doing it through code but I was just throwing this out there for reference.
I hope you've found an answer to the question. The answer might help those those come to the thread later. So, you can set multiple tags in similar manner as you do in XML using a | (pipe).
Something like:
EditText mEditText = new EditText(this);
mEditText.setInputType(InputTpe.TYPE_TEXT_FLAG_CAP_CHARACTERS|InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
Also, depending on your situation you might want to use setInputTypeor setRawInputype.
Yes, it seems like that should work. However, looking at the docs,
The type of data being placed in a text field, used to help an input
method decide how to let the user enter text. The constants here
correspond to those defined by InputType. Generally you can select a
single value, though some can be combined together as indicated.
Setting this attribute to anything besides none also implies that the
text is editable.
http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType
So it looks like in general, you can't expect to set two values. The above link shows which flags can be combined together.
Also, if you look at android:setInputType, it says this maps to the setRawInputType() method, not setInputType(). You might try calling setRawInputType() in stead of setInputType().
http://developer.android.com/reference/android/widget/TextView.html#setRawInputType(int)
i found this problem some time ago, but i solve it using this: getString(), or this: getResources().getString()
but now, for this case, it doesn't works, i think it's because i need to get the string values on a NON ANDROID ACTIVITY CLASS. I need the resource values on a remote connection class, that doesn't extends any kind of activity or service.
how i can acces to the variables from my strings.xml on this normal class?
this is the code where i get the error (it gets an integer, and not the string value)
String a =R.string.totalpermission;
Take a look at these two answers (are the same XD):
How to obtain AssetManager without reference to Context?
How can I get a resource content from a static context?
Just an advice: try to read some basic concepts... it seems you don't understand what the R class is and how to use it. Trust me, you waste less time studying than trying to figure out how things work.
I'll add something to existing answers since I found it very useful.
To get your strings you have to use a Context. Your activity will work just great.
String string = getString(R.string.myString);
But if you have something more complex... for exemple
R.string.result -> "You %1$s %2$d cats"
String result = getString(R.string.result, killed ? "killed": "saved", count);
That would give you a result like that:
You saved 10 cats or You killed 2 cats... and so on. You can pass parameters and positional arguments in strings will get replaced by your arguments in getString.
All Android resources are referenced via a resource ID, like R.string.totalpermission. You can see those numbers in R.java (although there's no reason to ever do that).
In cases of strings, you can easily get those using Context.getString. Bonus: You can even pass optional arguments and add dynamic strings that way. You always have a context - how are you getting called? If you really don't have a context, you can create one for the package your resources are in.
I hate the OK/Cancel dialogs, because if my application ask somebody if (s)he really want to do something you should never answer with "Cancel".
A little example:
final AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setIcon(android.R.drawable.ic_dialog_alert);
b.setTitle("Hello World");
b.setMessage("Did you do your homework?");
b.setPositiveButton(android.R.string.yes, null);
b.setNegativeButton(android.R.string.no, null);
b.show();
Is it possible that the constants "yes" and "no" really means "yes" and "no" with localization? Or have I do this explicit in my string resource and can't use global constants. So I replace the two lines with:
b.setPositiveButton("Yes", null);
b.setNegativeButton("No", null);
(or the resources instead of constants here)
Sincerely
xZise
Be aware that the contents of these text resources in English are actually "OK" and "Cancel", not Yes and No. So if you need Yes and No, you must use your own string resources. See for example http://code.google.com/p/android/issues/detail?id=3713 and http://groups.google.com/group/android-developers/browse_thread/thread/30b589fa9aca185a
A quick google search reveals that there are several apps that do exactly that, including Google's own My Tracks app, so I'd say it's safe to use android.R.string.yes.
Example: http://mytracks.googlecode.com/hg/MyTracks/src/com/google/android/apps/mytracks/io/backup/ExternalFileBackup.java?r=5ebff81c1c25d9600efb5d88eecc3e068ec22ae9
What you have provided should work. (An aside, in proper English your question should say "Have you done your homework? Or Did you do your homework?)
I don't think there is any global english standard regarding ok/cancel and yes/no. It really depends on the context. There are certain contexts where one or the other would make more sense. I things it's perfectly OK to use yes/no if that makes more sense for the kinds of things you are asking.