Gabriel
We have some ways to do this manipulation of layouts on different devices.
Drawable
Provides the following sizes
- ldpi (low) ~ 120dpi
- mdpi (medium) ~ 160dpi
- hdpi (high) ~ 240dpi
- xhdpi (extra-high) ~ 320dpi
- xxhdpi (extra-extra-high) ~ 480dpi
- xxxhdpi (extra-extra-extra-high) ~ 640dpi
With this, each file within these folders will treat a different screen size.
Dimens.xml
This xml that should be in res/values/dimens.xml
allows us to make customizations such as:
Note: Briefly SP for typography and DP for everything else.
Fragments
We have since 3.0 HoneyComb
the use of Fragments
for creation and dynamic layouts, thinking about the different screen sizes that currently exist (Tablets and cell phones with different screens).
Note : You can use the support library for lower versions, careful when import should be:
android.support.v4.app.Fragment
Theaboveimagedemonstratesthefragmentsinaction,forexampleintabletsthescreencancompletelydisplayitslayout,whenyouclickonanyiteminthelist,itwillbeloadednexttoitwithoutchangingtheiterationofthescreens,inthefigureonthesiderightwehavetheuseoffragmentsondeviceswithsmallerscreenswherewehaveFragmentAthatwhenclickeddirectstofragmentB,Corhowmanyareneededtomountyourscreen./p>
Exampletoinflateyourfragmenttakenfrom documentation
public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
In order to inflate the Fragments we need to override the onCreateView
method and return the inflated view, but nothing prevents us from using the OnCreate
method. Just keep an eye on the Fragment lifecycle.
Fragments Life Cycle
onAttach (activity) - This method is called soon after the fragment is associated with the activity
onCreate (bundle) - This method is called only once and when the fragment is being created. It will receive the Bundle that was saved during the onSaveInstanceState (state) method
onCreateView (inflater, viewgroup, bundle) - In this method, the fragment needs to create the view that will be inserted into the activity > layout
onActivityCreated (bundle) - This method is called shortly after the activity's onCreate () has been terminated.
onDestroyView () - This method is called when the fragment view has been removed and no longer belongs to the fragment.
onDestroy () - Called to indicate that the fragment is no longer being used.
OnDetach () - Opposing the onAttach (activity) method.