Do you want to know how setContentView() works in Android?

Let’s start…

Actually setContentView() is a method part of android.app.Activity class. It helps to set our content or render our layout on the screen.



Based on the value given by the user, views will be inflated and rendered after the measurement of the screen, root view, and its child views.

Well, here is everything you need to know about the setContentView() method.

SetContentView() method is 3 types.

  1. setContentView(int resourceId)
  2. setContentView(View view)
  3. setContentView(View view, android.view.ViewGroup.layoutparams params)

These methods are available from API level 1. Let’s talk more about these types.

 
setcontentview in android studio

setContentView(int resourceId)

This method uses layout constant from automatically generated R.java class. R.java class holds many static constant values in nested classes. So that you can access directly by specifying R class and nested class.

Nested Layout class contains layout resource constants to ease the availability of actual files in the resource directory.

At runtime, the System will select the appropriate layout based on orientation and other factors like language and region. By default, it selects the layout in the res/layout directory.
Eg:

 setContentView(R.layout.activity_main);

Here, R.layout.activity_main is an integer value contained inside of the layout class in the R.java file. Using this integer value Android system will look in the res/layout directory. That’s how android finds the correct layout.

If you are using XML layout do not try to initialize views before calling setContentView(R.layout.layout_name), because without inflating the layouts, there are no actual views in memory. It will lead to NullpointerException.

setContentView(android.view.View view) in Android

Set any view as your activity’s content. View’s height and width set to MATCH_PARENT as default.



@Override
Protected void onCreate(Bundle savedInstanceState)
{
//create a textview object
TextView textview=new TextView(this);
//set text to textview
Textview.setText(”hello world!”);
SetContentView(textview);
}

You probably need to add an import android.widget.TextView; statement.

setContentView(View view, ViewGroup.Layoutparams)

Extension of setContentView(View view) method. Here you can specify view’s parameters.


@Override
Protected void onCreate(Bundle savedInstanceState)
{
//create a LinearLayout object
LinearLayout linearLayout = new LinearLayout(this);

//set orientation
LinearLayout.setOrientation(LinearLayout.HORIZONTAL);
//make LinearLayoutParams object with width as MATCH_PARENT and height as MATCH_PARENT

LayoutParams linearLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);

SetContentView(linearLayout,linearLayoutParam);

}

You can add child views to Linearlayout using addView() method.

  • You can call many times setContentView() method, but it works only at the last call.
  • You can use setContentView() outside of the onCreate() method.
  • ListActivity does not use setContentView() Because it already contains a listview and use setListAdapter() to populate.
  • In fragments, LayoutInflater does layout rendering work with inflate() method. Just equivalent to setContentView().
  • Sometimes, you need to display a layout from many layouts based on any condition. Like showing login or signup page. So there is no need to create two activities. Just use if statement or other depends on your needs and do just like below.

If(newuser)
{
SetContentView(R.layout.sign_up_page);
}
Else
{
SetContentView(R.layout.login_page);
}

 

That’s all for now. Is there any suggestions, please comment below.

Conclusion
One of the most known methods in Android is setContentView(). I think most of us started Android development by thinking about what is setContentView and what it does. It’s used to show the app’s appearance. We can show our UI effectively by programmatically or using XML layouts. Android uses a different approach to showing XML layouts. Sometimes, it may difficult for beginners to understand. But I think, I have explained well by picture. If you have any doubts feel free to ask. By the way, sharing is caring.