Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do 'this' and 'setContentView' mean here?

Tags:

android

I was trying to deploy a small android app, where in , i was trying to display "Hello World" to the user .

The lines of code that i applied here in were (a bit from internet resources) :

    TextView text = new TextView(this);
    text.setText("Hello World, Here");
    setContentView(text);

What i fail to understand is : Why is this key word is required here ? Can't i just create a plain vanilla TextView object to set the text like this :

    TextView text = new TextView();
    text.setText("Hello World, Here");

And, what is the purpose of the setContentView method here ? Does it work like System.out.println of java ? A bit confused, any help will be appreciated. Thanks .

like image 441
The Dark Knight Avatar asked Feb 17 '23 11:02

The Dark Knight


2 Answers

 TextView text = new TextView(this);

Why is this key word is required here ?

This refers to the current object which in your case is the Activity as you are probably executing this code from the onCreate of your activity class. And the constructor of TextView class at least requires a context as an argument. And Activity is a subclass to Context so passing "this" does the trick. Thats why you cannot do something like this.

 TextView text = new TextView(); 

Now to answer why we are doing this. Think it in this way. This is a view that needs to attach itself 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 be binding so many thing in it and has a properly defined life cycle.

Activity is a type of context. Activity is a one visible screen in android application. Actually activity is much more than that. But just to understand this at elementary level.

setContentView says it all itself. The content that activity is going to display in the visible screen it belongs to.

So you declared a TextView and sets it as the content of the activity to be get displayed. Simple.

Hope it helps in understanding it better. You should better follow http://developer.android.com

cheers

like image 177
Rohit Sharma Avatar answered Feb 19 '23 00:02

Rohit Sharma


 TextView text = new TextView(this);

this refers to the current context in your case Activity context since you want text view in your activity.

public void setContentView (View view)

Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy. It can itself be a complex view hierarchy. When calling this method, the layout parameters of the specified view are ignored. Both the width and the height of the view are set by default to MATCH_PARENT. To use your own layout parameters, invoke setContentView(android.view.View, android.view.ViewGroup.LayoutParams) instead.

Parameters

view The desired content to display.

http://developer.android.com/reference/android/app/Activity.html#setContentView(android.view.View)

In your case setContentView (text) you are setting the view ie textview to the activity ie screen.

System.out.println("hello") in android will print hello in logcat.

What's meant by the "this" context?

like image 41
Raghunandan Avatar answered Feb 18 '23 23:02

Raghunandan