Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using a string resource in a Toast

My code is:

public static void ToastMemoryShort (Context context) {
    CharSequence text = getString(R.string.toast_memoryshort); //error here
    Toast.makeText(context, text, Toast.LENGTH_LONG).show();
    return;
    }

but I'm getting "Cannot make a static reference to the non-static method getString(int) from the type Context" in Eclipse. I'm trying to get ready for localising my app (getting all the hard coded strings into resources), so where I have:

getString(R.string.toast_memoryshort)

I previously had a hard coded string which was fine.

I'm not sure what's going on here (Java noob). Can anyone enlighten me please?

Many thanks

Baz

like image 481
Barry Avatar asked Sep 07 '11 09:09

Barry


People also ask

How do you use string on toast?

Text : It is the string message that you want the toast to display on the Android Activity screen. String toastTextMsg = "Hello, welcome to Code2care!"; Toast toast = Toast. makeText(MainActivity.

How do you add string resources?

android:text="@string/hello" />String string = getString (R. string. hello);

How do you show variable values in toast?

On Button click call displayValue() to display the toast message with the value set to cText. Show activity on this post. cText sounds like a character sequence or at the least some kind of text. Assuming that its a character sequence or String: yes you can use it.

What is the syntax for toast?

Therefore the code to make a Toast message is: Toast. makeText(getApplicationContext(), "This a toast message", Toast. LENGTH_LONG);


2 Answers

Change to

 public static void ToastMemoryShort (Context context) {

        Toast.makeText(context, context.getString(R.string.toast_memoryshort), Toast.LENGTH_LONG).show();
        return;
        }
like image 102
Rasel Avatar answered Sep 22 '22 22:09

Rasel


Just use this instead:

makeText(Context context, int resId, int duration) Make a standard toast that just contains a text view with the text from a resource.

From http://developer.android.com/reference/android/widget/Toast.html

like image 32
Stefan H Singer Avatar answered Sep 18 '22 22:09

Stefan H Singer