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.
Related
I am writing a message dialog within an if statement. When I finish the message there are no errors. When I add an error message or any other message an error says "no suitable method" and "not applicable".
The code looks like this:
OptionPane.showMessageDialog(null, "Wrong! Try again", JOptionPane.ERROR_MESSAGE);
Thats because there is no suitable method. You need to specify title and message of the dialog. Look here
For example:
JOptionPane.showMessageDialog(null, "Wrong! Try again", "Title", JOptionPane.ERROR_MESSAGE);
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
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();
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.
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.