Lesson 40. LayoutInflater. Learning to use.

Lesson 40. LayoutInflater. Learning to use.


In this lesson:

– Let’s see how LayoutInflater can be used

After studying SQLite, it’s time to start studying Lists. But before that it will be useful to learn about LayoutInflater. This knowledge will come in handy for us to create extended lists. Also, before this lesson, I recommend reading the LayoutParams lesson again, refreshing your knowledge.

LayoutInflater is a class that can create a View element from the contents of a layout file. The method it does is called inflate. There are several implementations of this method with different parameters. But they all use each other and the result of their execution is one – View.

We will look at this implementation – public View inflate (int resource, ViewGroup root, boolean attachToRoot)

As you can see, the input method takes three parameters:

resource – The ID of the layout file that will be used to create the View. For example – R.layout.main
root is the parent ViewGroup element for the View being created. LayoutParams from this ViewGroup are assigned to the created View.
attachToRoot – attach the created View to root. If true, root becomes the parent of the created View. That is, it is equivalent to the root.addView (View) command. If false, the created View simply receives LayoutParams from root, but does not become its child element.

Let’s see in practice.

Let’s create a project:

Project name: P0401_LayoutInflater
Build Target: Android 2.3.3
Application name: LayoutInflater
Package name: ru.startandroid.develop.p0401layoutinflater
Create Activity: MainActivity

open main.xml and we draw the following screen:



    
        
        
    
    
        
        
    

There are two ViewGroup on the screen – linLayout and relLayout. They have a TextView with the appropriate text.

Let’s create another layout file text.xml:



it’s simple here TextView without any ViewGroup. We will feel it LayoutInflater.

open MainActivity.java and write the code:

package ru.startandroid.develop.p0401layoutinflater;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {
  
  final String LOG_TAG = "myLogs";
  
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        LayoutInflater ltInflater = getLayoutInflater();
        View view = ltInflater.inflate(R.layout.text, null, false);
        LayoutParams lp = view.getLayoutParams();
        
        Log.d(LOG_TAG, "Class of view: " + view.getClass().toString());
        Log.d(LOG_TAG, "LayoutParams of view is null: " + (lp == null));
        Log.d(LOG_TAG, "Text of view: " + ((TextView) view).getText());
    }
}

We get it LayoutInflater getLayoutInflater method, we use it to get it View– element from the layout file text.xml and read LayoutParams in the newly created view.

Notice the parameters we used for the method inflate. we have specified ID layout-resource, passed null as the parent element and, accordingly, the attachment to the father or mother – false.

We’ll save everything and run it.

Nothing has changed on the screen. Because we have converted layout to view but it will not fit anywhere. It just hangs in memory.

We look at the log:

Class of view: class android.widget.TextView
LayoutParams of view is null: true
Text of view: Layout with TextView

We see the class of the created element – TextView. That’s right – this element was in the file text.xml. further we see null instead LayoutParams. This was due to the fact that we specified the father in the inflate method null. Namely from his father view and had to get it LayoutParams. The third line of the log shows TextView text. It is the same as in the layout file text.xml – everything is correct.

Let’s change the program a little. We will add our created item to linLayout with main.xml. This is done simply by the team addView.

LayoutParams lp = view.getLayoutParams();
        
LinearLayout linLayout = (LinearLayout) findViewById(R.id.linLayout);
linLayout.addView(view);
        
Log.d(LOG_TAG, "Class of view: " + view.getClass().toString());  

(Only add highlighted lines)

We found linLayout from the screen and added an item created using LayoutInflater.

Save, run. We see that the element was added to the screen in linLayout.

Now let’s try to specify the father (root) When calling a method inflate. rewrite the method onCreate:

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LayoutInflater ltInflater = getLayoutInflater();

    LinearLayout linLayout = (LinearLayout) findViewById(R.id.linLayout);
    View view1 = ltInflater.inflate(R.layout.text, linLayout, false);
    LayoutParams lp1 = view1.getLayoutParams();

    Log.d(LOG_TAG, "Class of view1: " + view1.getClass().toString());
    Log.d(LOG_TAG, "Class of layoutParams of view1: " + lp1.getClass().toString());
    Log.d(LOG_TAG, "Text of view1: " + ((TextView) view1).getText());

    RelativeLayout relLayout = (RelativeLayout) findViewById(R.id.relLayout);
    View view2 = ltInflater.inflate(R.layout.text, relLayout, false);
    LayoutParams lp2 = view2.getLayoutParams();

    Log.d(LOG_TAG, "Class of view2: " + view2.getClass().toString());
    Log.d(LOG_TAG, "Class of layoutParams of view2: " + lp2.getClass().toString());
    Log.d(LOG_TAG, "Text of view2: " + ((TextView) view2).getText());
  }

We find the elements linLayout and relLayout from the screen and with LayoutInflater we create two View items from the text.xml layout file. for the first specify root – linLayout, for the other relLayout. But the third parameter attachToRoot leave false. That means created View-Element will receive LayoutParams from root– element, but Didn’t add to him.

Let’s save everything, run it. Nothing has changed on the screen. Because we didn’t add anything to anything attachToRoot = false.

We look at the log:

Class of view1: class android.widget.TextView
Class of layoutParams of view1: class android.widget.LinearLayout $ LayoutParams
Text of view1: Layout with TextView
Class of view2: class android.widget.TextView
Class of layoutParams of view2: class android.widget.RelativeLayout $ LayoutParams
Text of view2: Layout with TextView

The logs show that the class of created items – TextView. And the class LayoutParams is different. IN the first the case is LinearLayout $ LayoutParamsBecause as a root we specified the element in the inflate method linLayoutThis is a class object LinearLayout. In the second case, the class LayoutParams from the created element – RelativeLayout $ LayoutParams. Because the root is specified relLayout (class RelativeLayout).

Now we have two options for how to add created ones view1 and view2 on the screen.

1) Use the methods again addView

2) Transmit true as the third parameter of the method inflate. then created View-The item will be added to root.

Select the second option and make changes to the code:

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LayoutInflater ltInflater = getLayoutInflater();

    LinearLayout linLayout = (LinearLayout) findViewById(R.id.linLayout);
    View view1 = ltInflater.inflate(R.layout.text, linLayout, true);
    LayoutParams lp1 = view1.getLayoutParams();

    Log.d(LOG_TAG, "Class of view1: " + view1.getClass().toString());
    Log.d(LOG_TAG, "Class of layoutParams of view1: " + lp1.getClass().toString());

    RelativeLayout relLayout = (RelativeLayout) findViewById(R.id.relLayout);
    View view2 = ltInflater.inflate(R.layout.text, relLayout, true);
    LayoutParams lp2 = view2.getLayoutParams();

    Log.d(LOG_TAG, "Class of view2: " + view2.getClass().toString());
    Log.d(LOG_TAG, "Class of layoutParams of view2: " + lp2.getClass().toString());
  }

pass true as the third parameter in the method inflate and remove the text output lines from TextView. Now it will be clear why.

We will save everything and launch the application.

As you can see, created TextView appeared in their parents, which we specified in the method inflate. IN RelativeLayout items overlapped because we didn’t adjust the placement. At the moment this is not significant.

We look at the log:

Class of view1: class android.widget.LinearLayout
Class of layoutParams of view1: class android.widget.LinearLayout $ LayoutParams
Class of view2: class android.widget.RelativeLayout
Class of layoutParams of view2: class android.widget.LinearLayout $ LayoutParams

Note the element class. In the first case, it is LinearLayoutAnd in the other – RelativeLayout. That is a method inflate returned to us do not create from the layout file View elements, and the ones we specified as root. AND created from the layout file View elements of it added in root as subsidiaries similar to the team addView. This is because of what we have indicated true in the third parameter (attachToRoot) of the inflate method.

in accordance LayoutParams for view1 and view2 will LinearLayout $ LayoutParams, Because linLayout and relLayout have parents LinearLayout. AND LayoutParams take from him.

To consolidate the topic in the next lesson we will make an example more interesting.

In the next lesson:

– make your own version of the list

getLayoutInflater




Discuss in the forum [49 replies]

Leave a Comment