About the Toast command [duplicate] - java

This question already has answers here:
what is the activity name in anonymous class
(4 answers)
Closed 4 years ago.
I want to know what 'this' means in the below Toast command:
Toast.makeText(MainActivity.this, "msg" ,Toast.Length_long ).show();
If possible could you please explain the whole command.

In general when you use construct SomeClass.this that means that you are referring to the specific (frequently 'outer' class). In example you can have a code like:
class Apple {
void outherMethod() {
}
class AppleType {
void innerMethod(){}
void method(){
Apple.this.outerMethod();
this.innerMethod();
}
}
}
Additionally, in this specific case on Android it means that you are using the activity's Context which is provided via MainActivity class.
So the whole command should be read as:
Create Toast widget inside context provided by MainActivity
It should display some text: "msg"
It should be visible for specific time defined by the constant: Toast.Length_long
finally, via show() method display it on device.

'this' means itself.
Toast.makeText(MainActivity.this, "msg" ,Toast.Length_long ).show();
Call the toast method, and the required parameters are 'context', 'toast message' and 'toast duration'.
Finally .show() means make toast to show.

its clear and you can use it like this
Toast toast =Toast.makeText(this, "msg", duration);
toast.show();
this: context
"msg": your message
duration: Toast.LENGTH_SHORT or Toast.LENGTH_LONG
and you can change position by setting gravity
toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0);
this will show toast center screen

Related

toast setGravity is not working it show normal default toast

i add the toast like this
Toast t = Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER,0,0);
t.show();
it is not working it shows normal default toast.

Show toast WHILE dialog is open?

This is an extremely simple issue I am facing. Basically, I am requesting run-time permissions—but I also want to show a toast at the same time as the permission request:
Relevent Code:
if ((ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{
Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 4);
Toast.makeText(MainActivity.this, "You must enable BOTH", Toast.LENGTH_LONG).show();
The problem is, the toast quickly disappears (within maybe less than 0.5 second), as soon as the permission dialog appears.
Is this a bug on Android? Or is there some work around that I'm missing?
Toasts don't display permanently. The entire concept of a Toast is that it pops up then fades away. If you want something more permanent, you'll have to implement it yourself.
It's default dialog for permission in android so no solution for this.ya but if you make your custom dialog then you can show it where you want.
Toast message is displayed for short duration of 2 sec or for long duration of 3.5 sec and it cannot be changed.
If you wish to display the toast message for longer time then you need to display it continuously.
for (int i=0; i < 5; i++){
Toast.makeText(this, "Your toast message", Toast.LENGTH_SHORT).show();
}
It will display your toast for 10 seconds.
Hope it helps :)
Try to make the context as
Toast.makeText(getApplicationContext(),"YOUR TEXT",Toast.LENGTH_LONG).show();

Issue implementing snackbar in android app

I'm working on an android app while learning to code in Java and I'm building a note taking app currently and am trying to convert what used to be a toast message to a snackbar message. Below is what I currently have along with the toast message that used to be there that is now commented out. I'm getting stuck on the method it says to call, any help would be greatly appreciated!
private void deleteNote() {
getContentResolver().delete(NotesProvider.CONTENT_URI,
noteFilter, null);
Snackbar.make(this, getString(R.string.note_deleted), Snackbar.LENGTH_LONG).show();
// Toast.makeText(this, getString(R.string.note_deleted),
// Toast.LENGTH_SHORT).show();
setResult(RESULT_OK);
finish();
}
The error android studio gives me is it cannot resolve the method "make".
The make function takes a view as the first parameter.
public static Snackbar make(View view, int resId, int duration)
If you need it in the bottom of your activity pass findViewById(android.R.id.content) as the first parameter.

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

Android Toast Messages not working

I'm developing a game via Andengine for Android. I have MainActivity class and GameScene class. I use Toast messages in GameActivity. And it is working.
Toast.makeText(this, " Hello World", Toast.LENGTH_SHORT).show();
So I wanna use Toast messages in GameScene class. But it doesn't work. Here is the code:
Toast.makeText(activity, " Hello World", Toast.LENGTH_SHORT).show();
I have to use "activity" instead of "this". But it doesn't work
why?
EDITED:
when I use second one, an error occurs.
LogCat:
http://s29.postimg.org/k8faj9mdj/Capture.png
You're trying to display a Toast in a background thread. You should do all your UI operations on the main UI thread.
The exception RuntimeException: Can't create handler inside thread that has not called Looper.prepare() can be a little cryptic for beginners but essentially it tells you that you're in a wrong thread.
To solve it, wrap the toast to e.g. runOnUiThread():
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(...).show();
}
});
There could be two reasons for your code to not work. It's ether your activity parameter is null or...
Short time after you showing the toast the activity is die, in that case it will kill the toast as well, to avoid this you can call activity.getApplicationContext() like in #Mehmet Seçkin answer.
use one of the following
Toast.makeText(getApplicationContext(), " Hello World", Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(),"please Create your Account First", Toast.LENGTH_SHORT).show();
Toast.makeText(GameActivity.this,"please Create your Account First", Toast.LENGTH_SHORT).show();
Use:
Toast.makeText(getApplicationContext(), " Hello World", Toast.LENGTH_SHORT).show();
or
Toast.makeText(activity.this, " Hello World", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();
try this.
Since you asked why; i think you're giving an activity reference as a context to the Toast message, this is why it isn't working.
If you're trying to show a Toast message from outside of an activity, you could try :
Toast.makeText(activity.getApplicationContext(), " Hello World", Toast.LENGTH_SHORT).show();
or from the GameActivity
Toast.makeText(GameActivity.this, " Hello World", Toast.LENGTH_SHORT).show();
or from the MainActivity
Toast.makeText(MainActivity.this, " Hello World", Toast.LENGTH_SHORT).show();
Since You are calling it from the class. you need to get the context from the activity through the class constructor or else you need to use GetApplicationcontext().
Make sure the app you are testing has notifications turned on. That was my story and why toasts weren't working either. I had gone looking for a straight answer and it just happens that toasts are considered part of the notifications. Interesting stuff, I had no clue.
If you think your code is correct, try to close your emulator tab then open AVD manager, next wipe data, then restart. Or you can delete the current AVD and add a new one.

Categories