Ωmega
Ωmega

Reputation: 43673

View added from XML file is not shown in its parent view

In my Player activity I am trying to add a view from a different XML by calling addView:

    view = (ViewGroup)findViewById(R.id.player_ConstraintLayout);
    notifierView = LayoutInflater.from(view.getRootView().getContext())
                                 .inflate(R.layout.notifier, null);
    view.addView(notifierView);
    // view.invalidate();

The Player activity comes with this associated XML file:

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/player_ConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.Player">

    ...

</android.support.constraint.ConstraintLayout>

And the one I want to add is notifier.xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    android:translationZ="9dp"
    android:elevation="9dp">

</android.support.constraint.ConstraintLayout>

I have debugged the app and by inspecting variable view I can confirm that the notifierView has been added and is in the children list of the view. However the notifierView is not shown, even I have expected it to be of full screen size and red. When I inspected notifierView in debugger, it showed its mTop, mBottom, mLeft, and mRight variables being all 0. I assuming it means that the

android:layout_width="match_parent"
android:layout_height="match_parent"

have been ignored? Or is there some other reason why I don't see this notifierView at all?


EDIT/UPDATE:

Let me show you more of the code, as the previous above is a little simplified one.

In Player activity I have:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player);

    notifier = new Notifier((ViewGroup)findViewById(R.id.player_ConstraintLayout));

    ...

}

and in Notifier.java I have

Notifier(ViewGroup view) {    
    handler = new android.os.Handler();    
    LayoutInflater inflater = LayoutInflater.from(view.getRootView().getContext());
    notifierView = inflater.inflate(R.layout.notifier, view, false);    
}

Upvotes: 0

Views: 60

Answers (2)

halfer
halfer

Reputation: 20440

(Posted solution on behalf of the question author).

Invalidate Caches / Restart helped. Android Studio sometime gets things wrong.

Upvotes: 1

Ben P.
Ben P.

Reputation: 54204

ConstraintLayout doesn't actually support match_parent for its children. You have to use this for "match parent" width:

android:layout_width="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"

And this for "match parent" height:

android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"

Additionally, your LayoutInflater.inflate() call should be changed. When you pass null as the second argument, you don't give the system enough information to understand how to parse layout_ attributes on the inflated view; you should pass a real parent (in this case, view). However, if you change null to view, the return value of the inflate() call will change, so you have to add an additional param (attachToRoot) to make sure the rest of the semantics don't change.

LayoutInflater inflater = LayoutInflater.from(view.getRootView().getContext());
notifierView = inflater.inflate(R.layout.notifier, view, false);

Upvotes: 3

Related Questions