2

Greetings Stack Overflow Community

When creating a custom Toast the android developer guide as well as this stack overflow post, both provide the following example:

my_custom_toast.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/toast_container"
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          >
    <ImageView android:src="@drawable/droid"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              />
</LinearLayout>

MainActivity.java

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(
    R.layout.my_custom_toast,
    (ViewGroup) findViewById(R.id.toast_container)
);

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("This is a custom toast");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

A Few Notes

If the second argument is null, then inflate() should return the root of the xml file, which is the LinearLayout called toast_container. Otherwise, if a ViewGroup is passed as the second argument, then inflate() should attach the inflated hierarchy to this ViewGroup and return the provided ViewGroup (which is now the parent of the inflated layout).

I have 2 questions:

First:

What is the purpose of providing a second argument? By default, the LinearLayout with the id @+id/toast_container will be returned if we pass null.

Second:

How can the inflated hierarchy be inflated (and embedded) to one of its own members? Or, is the above usage of inflate() considered improper?

In other words, this code is inflating (and embedding) the layout into one of its own members, that is the LinearLayout (check @+id/toast_container). This duplicates the LinearLayout by inflating the xml file into a second LinearLayout.

Maged
  • 71
  • 1
  • 8
  • As part of this website norms, please avoid making more than one question per post as that makes answering and voting trickier. – Pedro Loureiro Sep 28 '17 at 22:49

1 Answers1

-1

Answering your first question, the inflation of the view will use the second parameter in order to do a few steps correctly. Mainly to do with the LayoutParams classes (many view classes have a correspondent LayoutParams class). If you were trying to use margins, these would probably only work if you passed the second parameter.

Question 2: You misunderstood the documentation. If you pass the second parameter ViewGroup which I will call 'parent', your layout will be inflated into 'parent' and the function returns 'parent'.

In the case where you don't pass the second parameter, the function inflates your layout and will return the layout itself (the root/top view).

Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
  • Regarding Question 2: **I completely agree with what you say**. But, If the only purpose of the second argument is to infer the type of ViewGroup.LayoutParams then they should have used `inflate(R.layout.xml_file, ViewGroup, false)`. This way the LayoutParams type will be inferred (according to the documentation). My problem is, they are inflating (and embedding) the inflated layout into one of its own members, that is the linear layout (check @+id/toast_container). They are duplicating the linearlayout by inflating the xml file into a second LinearLayout. That is what I meant by question 2. – Maged Sep 28 '17 at 23:40
  • Here is the documentation I was refering to. https://developer.android.com/reference/android/view/LayoutInflater.html#inflate%28int%2C%20android.view.ViewGroup%2C%20boolean%29 – Maged Sep 28 '17 at 23:40