what is inflater in android

Solutions on MaxInterview for what is inflater in android by the best coders in the world

showing results for - "what is inflater in android"
Lilli
27 Nov 2018
1LINK: https://stackoverflow.com/questions/3477422/what-does-layoutinflater-in-android-do
2## there are images on the website to see better what's going on.
3
4What does LayoutInflator do?
5
6When I first started Android programming, I was really confused by LayoutInflater and findViewById. Sometimes we used one and sometimes the other.
7
8    LayoutInflater is used to create a new View (or Layout) object from one of your xml layouts.
9    findViewById just gives you a reference to a view than has already been created. You might think that you haven't created any views yet, but whenever you call setContentView in onCreate, the activity's layout along with its subviews gets inflated (created) behind the scenes.
10
11So if the view already exists, then use findViewById. If not, then create it with a LayoutInflater.
12Example
13
14Here is a mini project I made that shows both LayoutInflater and findViewById in action. With no special code, the layout looks like this.
15
16enter image description here
17
18The blue square is a custom layout inserted into the main layout with include (see here for more). It was inflated automatically because it is part of the content view. As you can see, there is nothing special about the code.
19
20public class MainActivity extends AppCompatActivity {
21    @Override
22    protected void onCreate(Bundle savedInstanceState) {
23        super.onCreate(savedInstanceState);
24        setContentView(R.layout.activity_main);
25    }
26}
27
28Now let's inflate (create) another copy of our custom layout and add it in.
29
30enter image description here
31
32LayoutInflater inflater = getLayoutInflater();
33View myLayout = inflater.inflate(R.layout.my_layout, mainLayout, false);
34
35To inflate the new view layout, all I did was tell the inflater the name of my xml file (my_layout), the parent layout that I want to add it to (mainLayout), and that I don't actually want to add it yet (false). (I could also set the parent to null, but then the layout parameters of my custom layout's root view would be ignored.)
36
37Here it is again in context.
38
39public class MainActivity extends AppCompatActivity {
40
41    @Override
42    protected void onCreate(Bundle savedInstanceState) {
43        super.onCreate(savedInstanceState);
44
45        // inflate the main layout for the activity
46        setContentView(R.layout.activity_main);
47
48        // get a reference to the already created main layout
49        LinearLayout mainLayout = (LinearLayout) findViewById(R.id.activity_main_layout);
50
51        // inflate (create) another copy of our custom layout
52        LayoutInflater inflater = getLayoutInflater();
53        View myLayout = inflater.inflate(R.layout.my_layout, mainLayout, false);
54
55        // make changes to our custom layout and its subviews
56        myLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));
57        TextView textView = (TextView) myLayout.findViewById(R.id.textView);
58        textView.setText("New Layout");
59
60        // add our custom layout to the main layout
61        mainLayout.addView(myLayout);
62    }
63}
64
65Notice how findViewById is used only after a layout has already been inflated.
66Supplemental Code
67
68Here is the xml for the example above.
69
70activity_main.xml
71
72<?xml version="1.0" encoding="utf-8"?>
73<LinearLayout
74    xmlns:android="http://schemas.android.com/apk/res/android"
75    android:id="@+id/activity_main_layout"
76    android:orientation="horizontal"
77    android:layout_width="match_parent"
78    android:layout_height="match_parent"
79    android:padding="16dp">
80
81    <!-- Here is the inserted layout -->
82    <include layout="@layout/my_layout"/>
83
84</LinearLayout>
85
86my_layout.xml
87
88<?xml version="1.0" encoding="utf-8"?>
89<RelativeLayout
90    xmlns:android="http://schemas.android.com/apk/res/android"
91    android:layout_width="100dp"
92    android:layout_height="100dp"
93    android:background="@color/colorPrimary">
94
95    <TextView
96        android:id="@+id/textView"
97        android:layout_width="wrap_content"
98        android:layout_height="wrap_content"
99        android:layout_centerInParent="true"
100        android:padding="5dp"
101        android:textColor="@android:color/white"
102        android:text="My Layout"/>
103
104</RelativeLayout>
105
106When do you need LayoutInflater
107
108    The most common time most people use it is in a RecyclerView. (See these RecyclerView examples for a list or a grid.) You have to inflate a new layout for every single visible item in the list or grid.
109    You also can use a layout inflater if you have a complex layout that you want to add programmatically (like we did in our example). You could do it all in code, but it is much easier to define it in xml first and then just inflate it.
110
111
queries leading to this page
how layoutinflater works in androidandroid studio what is inflaterinflator in androidinflaiter android studi owhat is inflate in androidandroid what is inflaterwhy we use inflater in androidandroid studio inflaterandroid viewgroup inflateandroid studio layout inflater inflate new layout in androidinflate in android meaningwhat is layoutinflater androidinflate layout androidandroid inflate layout android studio inflate viewuse of inflater in androidinflater android exampleinflater androidinflator view in androidwhat is inflate in android studioinflate a layout androidinflate androidwhat does inflater do in androidinflater example in androidwhat is inflation in android studioinflater in android studiolayoutinflater from 28this 29 inflate 28android r layoutandroid inflater inflateinflater in androidandroid studio inflater not workinglayoutinflater in androidwhat is layoutinflater in android exampleandroid studio layoutinflaterview view 3d layoutinflater from 28 mcontext 29 inflate 28 android support v4 r layout user items 2c parent 2cfalse 29 3binflater inflate 28r layout activity my toast layout with color 2c null 29 3bwhat is the use of inflater in androidandroid inflateinflater inflateview inflate c3 b9view inflateandroid inflatorandroid layout not inflatingwhat is the use of inflater in android studioandroid inflaterwhat does the inflater do androidwhat is layout inflation in androidjava view inflate androidwhat is inflater in androidinflater inflate androidlayout inflaterfind a android container to place custom view which is inflated using layoutinflatorandroid view from layoutlayout inflation in androidandroid inflate viewinflate in androidwhat is inflater in android