4

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 .

The Dark Knight
  • 5,455
  • 11
  • 54
  • 95

5 Answers5

5
 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

Rohit Sharma
  • 13,787
  • 8
  • 57
  • 72
2
 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?

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

this refers to the context. setContentview is used to set the layout resource it takes an integer as the argument and the integer refers to the layout xml

prvn
  • 916
  • 5
  • 5
0

From documentation

View elements require Context to be passed to constructor so it has access to resource like theme and stuff. Activity is a child of Context so You can use a this here.

setContentView sets a view element to be used to display activity, You may pass an instance of View element or resource id.

Gustek
  • 3,680
  • 2
  • 22
  • 36
0

If it did not exist(setContentView() method), you wouldn't ever know which file/code will be executed when your activity starts.

You are setting context with setContentView() method.

JustWork
  • 1,944
  • 3
  • 22
  • 34