About
In Android, you can create an UI:
- decoratively through a layout XML
In this context, Inflate means reading a layout XML (often given as parameter) to translate them in Java code.
This process happens:
An inflate process will:
- read a layout XML
- parsing it
When you compile your application, each XML layout file is compiled into a View resource. The compiled layout resource must be loaded from the application code.
Articles Related
Inflate
For an Activity
setContentView
- setContentView method of an activity.
@Override
protected void onCreate(Bundle savedInstanceState) {
// Parent call (All state function must have it)
super.onCreate(savedInstanceState);
// The XML layout located in res>layout>activity_main.xml
// will be inflated.
setContentView(R.layout.activity_main);
}
The loading must occur:
- in the Activity.onCreate() callback implementation (called by the Android framework when the Activity is launched. See Android - (Activity|Fragment) Lifecycle)
- by calling setContentView() and passing as parameter the XML layout resource location in the form of: R.layout.layout_file_name.
For example, if your XML layout is saved as main_layout.xml, you would load it for your Activity like so:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
LayoutInflater
LayoutInflater inflater = LayoutInflater.from(YourActivity.this); // or (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewMyLayout = inflater.inflate(R.layout.my_layout, null);
For a Fragment
Inflate (One Pane)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
The inflate method inflates the View hierarchy and binds to it all it one step.
Multi- Screen (Two-pane)
- The layout file activity_main.xml:
<!--
This layout is a two-pane layout for the Items master/detail flow.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
tools:context="com.example.android.myapp.MainActivity">
<!-- Static Fragment that references the main fragment class statically in the android:name attribute-->
<fragment
android:id="@+id/fragment_main"
android:name="com.example.android.myapp.myMainFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="@android:layout/list_content" />
<!-- Dynamic detail Fragment that will be instantiated at runtime in the activity -->
<FrameLayout
android:id="@+id/fragment_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4" />
</LinearLayout>
- The code: From MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_detail_container) != null) {
// The detail container view will be present only in the large-screen layouts
// (res/layout-sw600dp). If this view is present, then the activity should be
// in two-pane mode.
// Replacing the container dynamically using a fragment transaction.
// Check if savedInstance is null because
// If we rotate the phone, the system saves the fragment state in the saved state bundle (savedInstanceState)
// and is smart enough to restore this state.
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.weather_detail_container, new DetailFragment(), "DETAILFRAGMENT_TAG")
.commit();
}
}
// Set an Adapter or other onCreate activity
// *** SunshineSyncAdapter.initializeSyncAdapter(this);
}