Trying to use Toast message, but get error - android

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.

Related

Getting the value of the toast message and storing it in a string

I am new to Android. So, after clicking a button a toast message appears. I would like to store the toast message in a variable as a string after the message appears.
I have viewed this: Is it possible to get the value of string of Toast.makeText()?
In the solution provided there, he has created a variable name myToast. I have more than one toast but only one toast will appear when the button is clicked. I would like to store that particular toast as a string.
Thanks in advance!!
I think you can previously create one general string variable which will be changed when some button will be clicked and then you can show this variable in the toast which will appear. For example:
var selectedString:String = ""
then assign another value:
selectedString = "some_data"
and then show it in the toast:
Toast.makeText(this, selectedString , Toast.LENGTH_SHORT).show()
UPDATE
In general after clicking on button you will assign the latest string which will be represented in toast. So after that, you only need to analyse stored variable:
when(selectedString){
"test_1" -> moveFun(1)
"test_2" -> moveFun(1)
}
and then we have to create function for moving to next screen:
fun moveFun(toWhich:Int)
{
var intent:Intent?=null
when(toWhich){
1-> intent = Intent(this, FirstActivity::java.class)
2-> intent = Intent(this, SecondActivity::java.class)
}
if(intent!=null){
startActivity(intent)
}
}
UPDATE 2.0
Example of assigning value to selectedString:
selectedString = if(success_condition){
"good"
}else{
"bad"
}
then show it in Toast:
Toast.makeText(this, selectedString , Toast.LENGTH_SHORT).show()
and then analyse it :)

Android, using Toast inside onClickListener

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

Using a != statement with an edittext object in java?

Im trying to create a button which when pushed reads the edittext box to
Make sure its not blank
Make sure its not the default text in this case "First Name".
However when the button is pushed it still preforms the action even if the edittext text is First Name or blank. Is there an easier way to do this? Also the toast are not made when the text is First Name or blank.
createp.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (fname.getText().toString() != "")
if (fname.getText().toString() != "First Name"){
prefsEditor.putInt("user", 1);
prefsEditor.commit();
}
if (fname.getText().toString() == "")
{
Toast.makeText(createactivity.this, "You need a first name to create a profile!",
Toast.LENGTH_LONG).show();
}
if (fname.getText().toString() == "First Name") {
Toast.makeText(createactivity.this, "You need a first name to create a profile!",
Toast.LENGTH_LONG).show();
}
}});
}
Try this instead:
if (!fname.getText().toString().equals(""))
...
Another example:
if (fname.getText().toString().equals("First Name"))
....

Toast.makeText from resource string

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();

Change ImageView image inside onClick event

I have an ImageView working as a button in my program. I need it to be an ImageView since I want just the image to be shown, not the borders.
This button is a lamp, if the data in database says it's visible, the lamp must be "on", when you click the button, it will check the data from bank and change it to "off" if it's on and vice-versa.
So, the change of image will happen inside a onClick event.
Here is my code:
btnVisualizar.setClickable(true);
btnVisualizar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
if (visivelBanco.getText().equals("Y")) {
btnVisualizar.setImageResource(this.getResources().getIdentifier(
"drawable/visible_off", null, this.getPackageName()));
} else {
btnVisualizar.setImageResource(
this.getResources().getIdentifier("drawable/visible_on", null, this.getPackageName()));
}
} catch (Exception e) {
Toast.makeText(
getBaseContext(),
"Falha ao modificar visibilidade do item!",
Toast.LENGTH_SHORT).show();
}
}
});
However the compiler doesn't recognize the method getResources() and getPackageName().
What am I doing wrong?
Any help is appreciatted.
The this in there refers to the onClickListener.
Use [Activity].this instead of this, where [Activity] is the name of the Activity
try
String uri = "drawable/visible_off"
btnVisualizar.setImageResource(EdicaoActivity.this.getResources().getIdentifier(uri, "drawable", EdicaoActivity.this.getPackageName()));
I think you just need to use the R class to obtain the identifier of your drawable. For example:
btnVisualizar.setImageResource(R.drawable.visible_on);
You should not need to go messing about with getResources().

Categories

Resources