I am trying to make a toast display some text given certain conditions inside an onClickListener. The app won´t run in the simulator, and I get the following error: "void cannot be converted to Toast"
I have searched around, and find several similar problems and solutions on this forum, but none of them applies completely to my problem. The others haven´t used the correct context in the statement, but I really mean that I do. (The name of the javafile (context) is: "Case1Activity") Can anyone help me with this?
I have simplified the code a bit:
public void onClick(View view) {
if (button1Pushed == false){
count++;
Toast toast = Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
}
}
});
do it without assignment statement
Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
apply it as.
Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
if you want to use assignment operator then you can use below code
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Dear Friend check below before eating Toast,
Your Toast (Incompatible types error) :
Toast toast = Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
Normal Case (Standard Use):
Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();`
Special Case (if you need reference of Toast):
View toast = Toast.makeText(MainActivity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
because here ".show()" method is -
public void show () which
shows the view for the specified duration.
Thanks
Related
I want to make the user to fill all the particular edittext and cannot leave it empty. I use .isEmpty code and the or symbol "|". This is what I make:
if(!e_name.getText().toString().isEmpty()|!e_numb.getText().toString().isEmpty()|!e_pangkat.getText().toString().isEmpty()
|!e_tarikh.getText().toString().isEmpty()|!e_masa.getText().toString().isEmpty())
{
Toast.makeText(getApplicationContext(), "Cannot empty", Toast.LENGTH_SHORT).show();
return;
}else {//another code}
when I run the code, even when the edit text is fill, it still toast "cannot empty" what did I do wrong?
Use double pipes for or ||, single pipes are for bitwise operations
Also your if-statement is probably wrong, currently you check if the strings are NOT empty and give an error.
You should remove the ! from the statements so you check if the strings ARE empty, then give the error.
if(e_name.getText().toString().isEmpty()||e_numb.getText().toString().isEmpty()||e_pangkat.getText().toString().isEmpty()||e_tarikh.getText().toString().isEmpty()||e_masa.getText().toString().isEmpty())
{
Toast.makeText(getApplicationContext(), "Cannot empty", Toast.LENGTH_SHORT).show();
return;
}
else {//another code}
Try like this
if(!e_name.getText().toString().isEmpty()||!e_numb.getText().toString().isEmpty()||!e_pangkat.getText().toString().isEmpty()
||!e_tarikh.getText().toString().isEmpty()||!e_masa.getText().toString().isEmpty())
{
Toast.makeText(getApplicationContext(), "Cannot empty", Toast.LENGTH_SHORT).show();
return;
}else {//another code}
Am trying to get the ImageView drawable and use it to create an if else statement, i have tried many solutions including the one below, all are not working they always give me the wrong Toast
if(holder.likeImage.getDrawable()==context.getResources().getDrawable(R.drawable.ic_action_like)){
Toast.makeText(context, "LIKED ALREADY", Toast.LENGTH_SHORT).show();
}
else if(holder.likeImage.getDrawable()!=context.getResources().getDrawable(R.drawable.ic_action_like)){
Toast.makeText(context, "YOU HAVNT LIKED", Toast.LENGTH_SHORT).show();
}
try this.
holder.likeImage.setImageResource(R.drawable.ic_action_like);
holder.likeImage.setTag(R.drawable.ic_action_like);
if(holder.likeImage.getTag().equals(R.drawable.ic_action_like)){
}else{
}
I am currently developing my first android application, which is actually my first contact with java ever. Therefore, I suppose this question has an obvious answer to experienced developers, but I couldn't find anything at SO or anywhere else. The following EditText has two toasts (I'm not sure if that sounded right). One is shown if the user types a specific word (in this case, Please is shown if the user types Magic word and presses the button). I would like the another toast to appear if the user types anything else but that word (anything but "Magic word") when clicking the button.
EditText editText1 = (EditText) findViewById(R.id.editText1);
{
if(editText1.getText().toString().trim().equals("Magic word"))
{
Toast.makeText(MainActivity.this,
"Please", Toast.LENGTH_LONG).show();
}
}
else if (editText1.getText().toString().trim().equals())
{
Toast.makeText(MainActivity.this,
"You didn't say the magic word", Toast.LENGTH_LONG).show();
}
}
}
});
I apologize for my poor english. Hopefully, I made myself understood.
Thank you very much.
Following code give you the solution:
final EditText editText1=(EditText)findViewById(R.id.editText1);
Button btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
final String edittext=editText1.getText().toString();
if(edittext.trim().equals("Magic word"))
{
Toast.makeText(MainActivity.this,
"Please", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this,
"You didn't say the magic word", Toast.LENGTH_LONG).show();
}
}
});
More simple way!!
String hello = editText1.getText().toString();
and then,
Toast.makeText(MainActivity.this, "The text is" +hello , Toast.LENGTH_SHORT).show();
More accurately "Magic word" isn't a word, its two with a space. The first if statement will require the text in the EditText to be exactly that for the statement to be true. If you actually intended for "magic word" to merely be somewhere in the text typed into the EditText to show a Toast that says please, look into using String.contains() or regex for the first comparison.
For all other conditions there's no need to check the value of the EditText. In other words remove the second if just keep else as Luksprog mentioned.
I have a class named MyPrimaryClass, this class has a button witch when pressed, creates an Intent with the class myClassForResult.
I use this to start it:
startActivityForResult(myIntentOfMyClassForResult, ACTIVITY_EDIT_BTEXT);
Both MyPrimaryClass, and myClassForResult extends Activity.
So, when I call Toast.makeText within the myClassForResult, with the text parameter of R.string.my_resource_string, it gives me Force Close!
I have tried this:
Context c = myClassForResult.this;
Toast toast = Toast.makeText(c,
c.getResources().getString(R.string.my_resource_string),
Toast.LENGTH_SHORT);
toast.show();
Also this: c = getApplicationContext()
Also this: c = getBaseContext()
Also this:
Context c = MyPrimaryClass.this;
Toast toast = Toast.makeText(c,
R.string.my_resource_string,
Toast.LENGTH_SHORT);
toast.show();
If I use an inline string, like "My toast Text!", it works. But i need to get a string from the resources.
-Problem solved:
To solve the problem I changed the duration of the Toast to Toast.LENGTH_LONG
The string R.string.my_resource_string value is "The title is empty"
When I change its value to "The title", it worked properly, so I guess the string was too long for the Toast.LENGTH_SHORT duration.
But when i change the duration to Toast.LENGTH_LONG, I could use the long string.
Context c = MyPrimaryClass.this;
Toast toast = Toast.makeText(c,
R.string.my_resource_string,
Toast.LENGTH_LONG);
toast.show();
One thing to note:
Toast toast = Toast.makeText(c,
c.getResources().getString(R.string.my_resource_string),
Toast.LENGTH_SHORT);
toast.show();
Can be simplified into:
Toast.makeText(c,
c.getResources().getString(R.string.my_resource_string),
Toast.LENGTH_SHORT).show();
This saves you an object reference that you do not need.
One thing you need to understand is that whenever you reference you R in your package (not android.R.) you will have access to your resources as long as you have Context.
Update
After realizing what you are using this for I would recommend that you
change your approach, while this is in fact possible, your approach isn't ideal for something so simple.
The method startActivityForResult(xx) is typically when you want to start an application that is outside of your package for a result.
For instance: if I wanted to retrieve a barcode from a product, then I'd start an Intent to that barcode class, indirectly through an action. Then I'd retrieve the data through using onActivityResult(xx).
it makes No Sense to do this for your own classes.
Try:
Toast.makeText(this, this.getString(R.string.my_resource_string), Toast.LENGTH_SHORT);
#dilipkaklotar Answered correctly but a few changes needs to be done:
this is how it worked for me
Toast.makeText(getApplicationContext(),
getApplicationContext().getResources().getString(R.string.message),
Toast.LENGTH_SHORT).show();
the getResources has no parenthesis ().
and at the end is .show(); not toShow().
but it's correct. so thank you very much.
Toast.makeText(getApplicationContext(), getApplicationContext().getResources.getString(R.string.imgval), Toast.LENGTH_SHORT).toShow();
I'm trying to use a Toast message inside a listener method, but I get an error saying something like: Toast is not applicable for the argument.. I don't understand this and can't solve this problem without some help? Thanks!
// Button 1
button_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//text_1.setText("New text for first row!"); // Change text
Toast.makeText(this, "You have clicked on number 1", Toast.LENGTH_LONG).show();
if(controlImage_1)
{
image_1.setImageResource(R.drawable.android_1b); // Change image
controlImage_1 = false;
}
else
{
image_1.setImageResource(R.drawable.android_1a); // Change image
controlImage_1 = true;
}
//Toast.makeText(this, "Your download has resumed.", Toast.LENGTH_LONG).show();
}
});
Try it:
Toast.makeText(**YourClassName.this**, "You have clicked on number 1", Toast.LENGTH_LONG).show();
Without * =)
Toast.makeText(this, "You have clicked on number 1", Toast.LENGTH_LONG).show();
Your this in this statement refers to the View.OnClickListener you created. Read more on anonymous inner classes.
Use MyActivity.this instead
You need to pass in the Context of the current Activity as the first argument. You can't just say this in this case because it is not referring to the application context. You can either create a variable in your on create and use that context or just do this...
Toast.makeText([CLASS_NAME].this, "You have clicked on number 1", Toast.LENGTH_LONG).show();
Replace [CLASS_NAME] with the class that is extending Activity
Try getApplicationContext() as first argument for Toast.makeText or MyActivity.this
here in your code , this refers to the View
Also, if button_1 needs to have onclick listener by default and you are using API > 7,its nice to define onclick="myclickfunction" in the layout itself. Cleans up your code and easy to modify...
Because you are using it in the onClick Listener method, you cannot use only this as the first parameter.
Toast.makeText(ClassName.this, "You have clicked on number 1", Toast.LENGTH_LONG).show();
Use your classname.this as the first parameter.