0

I have a strange problem that I'm struggling with.

It is very basic: trying to show a Toast message when clicking a imageView, the code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_password);

    lock = (ImageView) findViewById(R.id.lock);
    lock.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(getBaseContext(), "clicked", Toast.LENGTH_SHORT).show();
        }
    });
}

But for some reason the toast message comes out blank, as if i sent a "" message. if i move the toast line to "oncreate" it will be shown as normal, I have no idea what seems to be the problem

Lachlan Goodhew-Cook
  • 1,101
  • 17
  • 31
EranGL
  • 81
  • 1
  • 6
  • Are you saying that if you change the "clicked" string argument, to "oncreate", it will work properly ? – Evdzhan Mustafa May 04 '14 at 10:36
  • @EvcanMustafa He's just saying if he moves the toast to the `onCreate` scope it will work. That's because `getBaseContext()` will not return the same context within the listener and in `onCreate`. – GoRoS May 04 '14 at 10:48

3 Answers3

0

Replace the line:

Toast.makeText(getBaseContext(), "clicked", Toast.LENGTH_SHORT).show();

with

Toast.makeText(YourActivityClass.this, "clicked", Toast.LENGTH_SHORT).show();

to get the context correctly.

See the accepted answer to Android: why must use getBaseContext() instead of this for more explanation.

Community
  • 1
  • 1
Szymon
  • 42,577
  • 16
  • 96
  • 114
  • If this is must, then why I don't get any error while using `getApplicationContext` or `getBaseContext` in Toast on API 19-to-24? – exploitr May 21 '18 at 13:43
0

Get the context at the beginning and use it to show the Toast:

private static Context mContext;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = getApplicationContext();

    setContentView(R.layout.activity_password);

    lock = (ImageView) findViewById(R.id.lock);
    lock.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(mContext, "clicked", Toast.LENGTH_SHORT).show();
        }
    });
}
GoRoS
  • 5,183
  • 2
  • 43
  • 66
  • Why is the downvote? It's absolutely fantastic to see how bad some people take corrections. As soon as I corrected a user's answer within this question, he's just deleted his post and probably downvoted my answer without giving any explanation. There is nothing wrong in this answer, and if it is, please explain it for our own good!! – GoRoS May 04 '14 at 20:56
  • 1
    +1 because I'm using the same method for years, and don't get any error. Typical useless people will downvote you. – exploitr May 21 '18 at 13:45
0

Use this static method so you can simply call it instead of recreating the method every time. So create this into your Class.java (example)

public static void showToast(Context context, String text) {
    Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}

And then call it by using

Class.showToast(getActivity().getApplicationContext(), "text");
Hasta'98
  • 184
  • 2
  • 9