0

Hello I'm new to android and I'm confused with this keyword in activity contexts. Here is a code snippet which simply prints to the screen when a button is pressed. But the studio is raising an issue.

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d("My app","onCreate is called");
    Toast1("onCreate");
    Button btn=(Button)findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("My app","Button is pressed");
            Toast.makeText(this,"Button pressed",Toast.LENGTH_SHORT).show();//Here is a error
        }
    });
}

How to know which activity or class the this keyword referencing?

Vickel
  • 7,879
  • 6
  • 35
  • 56
Thakur Karthik
  • 3,034
  • 3
  • 15
  • 24

4 Answers4

2

Inside anonymous class, this refers to the block of the anonymous class. To refer to the Activity class which contains the anonymous class, you need to append the class name and . before the this keyword

ActivityClassName.this

Toast either requires the context of the activity on which it is to be displayed or the context of the application

Toast using activity context

Toast.makeText(Activityname.this,"Button pressed",Toast.LENGTH_SHORT).show();

Note: If your Toast is inside any anonymous class, then you need to use ActivityName.this. If that's not the case, simply using this would do the job.

Toast using application context

Toast.makeText(getApplicationContext(),"Button pressed",Toast.LENGTH_SHORT).show();
Yousaf
  • 27,861
  • 6
  • 44
  • 69
1

If The activity that you use Called "MyActivity" then you can do the following:

MyActivity.this

This chunk of code will return the this "current" object of outer class this will work for you.

     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d("My app","onCreate is called");
    Toast1("onCreate");
    Button btn=(Button)findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("My app","Button is pressed");
            Toast.makeText(MyActivity.this
    ,"Button pressed",Toast.LENGTH_SHORT).show();//here is the working code for you
        }
    });
}
Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
1

For toasts, which are short-lived, you can usually use whatever context you want. Typically, you would use the activity context, but application context is fine as well.

So you can use Classname.this Eg:- MainActivity.this or getApplicationContext();

Saurav Prakash
  • 588
  • 1
  • 5
  • 26
1

Basically onClick(View v) method is anonymous class which implements method of interface android.view.View.onClickListner so only this keyword of context doesn't belong to anonymous class

so you may define reference of this keyword with related Activity in button onClickListner like below code

 btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d("My app","Button is pressed");
        Toast.makeText(MainActivity.this,"Button pressed",Toast.LENGTH_SHORT).show();  //Change here
    }
});

for more reference check this link

Omkar
  • 3,040
  • 1
  • 22
  • 42