What do 'this' and 'setContentView' mean?

I tried to deploy a small Android application in which I tried to display "Hello World" to the user.

The lines of code I used here were (a bit from internet resources):

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

What I don't understand: Why is the this required here? Can't I just create a simple vanilla TextView object to set the text as follows:

  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 java? A little embarrassed, any help would be appreciated. Thanks.

+4
source share
5 answers
  TextView text = new TextView(this); 

this refers to the current context in your case. An activity context, since you want a textual representation in your activity.

public void setContentView (view)

Set the activity contents to an explicit view. This view is placed directly in the hierarchy of activities. It can be a complex hierarchy of representations. When this method is called, the layout parameters of the specified view are ignored. Both width and height of the view are set by default MATCH_PARENT. To use your own layout options, call setContentView (android.view.View, android.view.ViewGroup.LayoutParams) instead.

Options

Show the desired content to display.

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

In your case, setContentView (text) you set the view, i.e. textview, on activity, i.e. screen.

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

What is meant by the context of "his"?

+3
source
  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

+5
source

this refers to the context. setContentview is used to set a layout resource that requires an integer as an argument and the integer refers to the xml layout

0
source

From the documentation

Presentation elements require that the Context be passed to the constructor, so that it has access to a resource, such as a topic, etc. Activity is a child of context, so you can use it here.

setContentView defines the view element that will be used to display activity. You can pass an instance of the View element or resource identifier.

0
source

If it does not exist (setContentView () method), you will never know which file / code will be launched when your activity starts.

You set the context using the setContentView() method.

0
source

Source: https://habr.com/ru/post/1482141/


All Articles