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

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
//...

Related

How to create a Lottie Alert Dialog in Java

I've tried searching for the Java tutorial regarding creating my LottieAlertDialog, but I can't find any. Everywhere it's in Kotlin, but I need the Java code as my project is in Java.
I've tried creating my LottieAlertDialog in this way:
LottieAlertDialog.Builder alert=new LottieAlertDialog.Builder(context,DialogTypes.TYPE_CUSTOM,
"social.json") //Here social.json is inside assets folder
.setTitle("Social")
.setDescription("social");
alert.build();
But the dialogbox doesn't show, when I run the app. To check whether my alert dialogbox was being created or not I tried testing it by printing the description set in the dialog in a Toast:
Toast.makeText(context,alert.getDescription(),Toast.LENGTH_SHORT).show();
The toast works and its showing "social"! That means the dialog is being created. But unfortunately it doesn't show in my app. What do I do? I've implemented all the dependencies as shown in the below link:
lottiealertdialog
Ok, after much hankering, I finally came to the solution. It's
alert.build().show();
The thing is not related to Kotlin or Java as such, you need to show the dialog once you have built it. So far your code is correct. You just need to show it further like this
LottieAlertDialog.Builder alert = new LottieAlertDialog.Builder(context, DialogTypes.TYPE_CUSTOM,
"social.json")
.setTitle("Social")
.setDescription("Social")
.build()
.show();

Android-Stringbuilder Returns Numeric, Not Alpa

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.

How I can make links clickable in an AlertDialog without using a TextView?

I have this line in the strings.xml file:
<string name="backup_help_body">Please watch this video if you did not understand the above steps: www.youtube.com</string>
and the above text in an AlertDialog:
AlertDialog.Builder backupHelpDialog = new AlertDialog.Builder(this);
backupHelpDialog.setTitle(getString(R.string.backup_help_title));
backupHelpDialog.setMessage(R.string.backup_help_body);
backupHelpDialog.setIcon(R.drawable.my_notes);
backupHelpDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface backupHelpDialog, int witch) {
// Do Not Do Anything.
}
});
backupHelpDialog.show();
return true;
How can I make the link clickable in the AlertDialog without using a TextView?!
I'm not sure you can do without a TextView, I would use a Custom AlertDialog, however you can create a "temporary" TextView within the code to perform what you need, here I leave the url where you have several examples
How can I get clickable hyperlinks in AlertDialog from a string resource?
Use HTML formatting :
Example :
<string name="my_link">Click me!</string>
For reference :
Android-Is it possible to add a clickable link into a string resource
Now for making the link as clickable, you need to add the LinkMovementMethod to the corresponding Textview.
To get the related Textview, you need to fetch the mAlert field from AlertDialog using reflection and then findviewbyId(android.R.id.alertTitle)
Reference :
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.0_r1/android/app/AlertDialog.java#AlertDialog.0mAlert
I think the best and fastest way (what I do) would be to add a custom layout with a textview inside, then add an onClickListener on that textview.
#SuperEasyToDo

Make a call via my app programatically [duplicate]

This question already has answers here:
How to make a phone call using intent in Android?
(21 answers)
Closed 7 years ago.
I'm building an app that lets you do various tasks and for this particular task. I want the user to be able to call someone. I've taken a look at many questions but all of them tell you to use intents and direct you to the phone app or instead they give you an answer where the users clicks a button and it calls the number assigned in the code.
I want the user to be able to type in a number in and EditText field and be able to hit the call button to call someone, how can I do something similar to this?
I'm not sure if this website is good for stuff like this as answers are hard to come by so I will keep looking around for answers, hopefully someone can answer my q and help me out a bit.
EDIT -
Super disappointed with all the notices and the downvote, really sucks however I appreciate the answers provided. But none helped lol.
So here is how I managed to do what I had asked. I create a onClick event and defined the two main objects with the objects and IDS above it (The EditText and the Button). In the onClick method I got the code to check and see if anything has been entered in the textfield (EditText), it detects with the following code -
(!TextUtils.isEmpty(txtPhone)) {
Uri uri = Uri.parse("tel:" + txtPhone);
If numbers have been typed in, it will run this intent and pull the string from the EditText object.
Intent intent = new Intent(Intent.ACTION_CALL, uri);
startActivity(intent);
if nothing is in field it will show a toast.
Check below for my method. Hopefully someone in my position will benefit from this and wont have to get downvoted for no particular reason.
// Call button
ImageButton call = (ImageButton) findViewById(R.id.call_button);
final EditText txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
call.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String txtPhone = txtPhoneNo.getText().toString();
if (!TextUtils.isEmpty(txtPhone)) {
Uri uri = Uri.parse("tel:" + txtPhone);
Intent intent = new Intent(Intent.ACTION_CALL, uri);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(),
"Please enter recipients number", Toast.LENGTH_LONG)
.show();
}
}
});
Looks like I will be using YouTube for stuff like this now, StackOverFlow is supposed to be about helping new users. Not downvoting their questions and repeatedly labelling their posts as duplicates and already answered.
You should check this : how to make phone call using intent in android? and the accepted answer.
I don't know whether you have searched or not, I found this solution as first link in my search and its pretty much what you want to do (i.e. Call Directly from your app).
Try this
handle this on your call button click
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+ editText.getText.toString()));
startActivity(callIntent);
add <uses-permission android:name="android.permission.CALL_PHONE" /> in Manifest

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

Categories