TextView text = new TextView(this);
Why is this keyword required here?
This refers to the current object, which in your case is Activity, since you are probably executing this code from onCreate of your activity class. And the constructor of the TextView class at least requires context as an argument. And Activity is a subclass of Context, so passing "this" does the trick. That is why you cannot do something like this.
TextView text = new TextView();
Now, to answer why we do it. Think so. This is a point of view that should be tied to some context. so that it can also consume many context-related privileges in the system.
See context as an individually existing wrapped component of your application that will bind so many things to it and has a well-defined life cycle.
Activity is a type of context. Activity is one visible screen in an Android app. In fact, the activity is much more. But just to understand it at the initial level.
setContentView says it all by itself. The content that will be displayed on the visible screen to which it belongs.
So, you declared a TextView and set it as the content of the displayed activity. Plain.
Hope this helps to better understand this. You should better follow http://developer.android.com
amuses
source share