I understand that it has to do with the App layout, but when do I have to use it? I tried to look for a link that explained this method, but I couldn't find it. Thank you in advance!
-
5Here is a nice visual explanation of [Android setContentView](https://androidride.com/what-setcontentview-android-studio/) – Athira Reddy Jun 04 '19 at 03:49
6 Answers
In Android the visual design is stored in XML files and each Activity is associated to a design.
setContentView(R.layout.main)
R means Resource
layout means design
main is the xml you have created under res->layout->main.xml
Whenever you want to change the current look of an Activity or when you move from one Activity to another, the new Activity must have a design to show. We call setContentView in onCreate with the desired design as argument.
- 350
- 3
- 14
- 778
- 4
- 8
-
3What does it do? How do we use it? Why are we forced to use it? – Niklas Rosencrantz Nov 21 '17 at 06:24
-
As per the documentation :
Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.
Your Launcher activity in the manifest first gets called and it set the layout view as specified in respective java files setContentView(R.layout.main);. Now this activity uses setContentView(R.layout.main) to set xml layout to that activity which will actually render as the UI of your activity.
- 4,675
- 5
- 27
- 43
-
This documentation comes from the version of `setContentView()` which takes a `View` parameter. – Code-Apprentice Jul 11 '14 at 21:05
-
@Code-Apprentice Thanks for correcting. Working for 16 hrs now need some sleep :) – CodeWarrior Jul 11 '14 at 21:16
Why setContentView() in Android Had Been So Popular Till Now?
setContentView(int layoutid) - method of activity class. It shows layout on screen.
R.layout.main - is an integer number implemented in nested layout class of R.java class file.
At the run time device will pick up their layout based on the id given in setcontentview() method.
- 217
- 3
- 6
You can set content view (or design) of an activity. For example you can do it like this too :
public void onCreate(Bundle savedinstanceState) {
super.onCreate(savedinstanceState);
Button testButon = new Button(this);
setContentView(testButon);
}
Also watch this tutorial too.
Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.
- Activity is basically a empty window
- SetContentView is used to fill the window with the UI provided from layout file incase of setContentView(R.layout.somae_file).
- Here layoutfile is inflated to view and added to the Activity context(Window).
- 31
- 1
in order to explain the method setContentView(:int) which takes a layout file as a parameter.
so let's imagine i want to create a view and add to it a button :
so in android , the setContentView will only takes an xml file (layout) and inflate it to an empty activity.
at the end to add my customized view which contains a button to the empty activity i will only use the same method setContentView(v).
- 11
- 1