Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Layout and Fragment Layout

When creating an new Android Activity, it used to create an xml file in the layouts folder where I would define the UI.Now it creats two files:

1.Layout file

2.Fragment Layout File.

Could someone explain the difference between the two? Also when trying to add items as listviews, buttons...etc. in which file should I add them to be called in my activity file.

like image 301
omarsafwany Avatar asked Jul 17 '26 13:07

omarsafwany


1 Answers

Starting with Android 3.0, Activities may now host Fragments which can be used to develop portions of the UI, and be displayed in different configurations depending on screen size, orientation, and other factors. It is highly recommended to use Fragments in modern Android applications, but is not required.

You can create an Activity layout which will hold one or more Fragments, and then place your UI components in the Fragment's layout. The Activity will load the Fragment, and then the Fragment will inflate the layout you wish to present inside it. You can also dynamically add/remove/swap out different Fragments inside the same Activity, depending on what you wish to display to the user.

You can read more about how to use Fragments here: Fragments | Android Developers

You can also choose to ignore the Fragment design principle, and continue to place all of your layouts into the Activity layout file. In that case, you can delete the Fragment layout.

like image 122
savanto Avatar answered Jul 20 '26 03:07

savanto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!