1

I have read the following Android include layout dynamically with data-binding library question. My problem is a bit different.

I have two xml one for the the Activity:

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="item" type="myapplication.MainActivityViewModel"/>
    </data>
<android.support.constraint.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="myapplication.MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/main">
    </FrameLayout>

</android.support.constraint.ConstraintLayout>
</layout>

And another for a frame:

<layout
    xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="item" type="myapplication.FrameViewModel"/>
    </data>
<FrameLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TEST"/>

</FrameLayout>
</layout>

I would like to inflate the frame.xml dynamically to the activities FrameLayout with databinding. In other words I would like to inflate different frames in the activity, just like Fragment, without fragments. I tried the following in the Activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        ViewGroup view = activityMainBinding.main;
        FrameBinding frameBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.frame,view, false);
}

With that code I can get both layout showing in the Activity. I only can see the Activities layout. How should I change my code to be able to see my Activity layout view elements and my frame view elements as well in the Activity?

Community
  • 1
  • 1
codeme
  • 861
  • 2
  • 12
  • 29

1 Answers1

4

It is not clear what error you are seeing but, if my guess is correct, you should be doing an include for the frame layout. See "That include thing" for details on how to do it with data binding.

Update So, my initial guess was incorrect. Here is another try:

If you don't want to use Fragments then look at using simple View replacement. The following code will replace one FrameLayout with another while demonstrating that data binding does not break and that you will continue to see remaining original views in your layout. Once the main screen is displayed, click on the button to replace one FrameLayout with another.

Here is a short demonstration.

MainActivity.java

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;

import com.example.databindingreplaceview.databinding.ActivityMainBinding;
import com.example.databindingreplaceview.databinding.FrameBinding;

public class MainActivity extends AppCompatActivity {

    private String replacementText = "This is the replacement frame.";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding activityMainBinding =
                DataBindingUtil.setContentView(this, R.layout.activity_main);

        final ViewGroup view = activityMainBinding.main;
        final FrameBinding frameBinding =
                DataBindingUtil.inflate(getLayoutInflater(), R.layout.frame, view, false);
        frameBinding.setReplacementText(replacementText);
        view.getRootView().findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ConstraintLayout layout;

                v.setEnabled(false);
                layout = (ConstraintLayout) view.getRootView().findViewById(R.id.constraintLayout);
                layout.removeView(view.findViewById(R.id.main));
                layout.addView(frameBinding.getRoot());
            }
        });

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="This is the top text."
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <FrameLayout
            android:id="@+id/main"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="64dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="This is the frame to replace." />

        </FrameLayout>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="112dp"
            android:text="This is the bottom text."
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="51dp"
            android:text="Replace Frame"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView2" />


    </android.support.constraint.ConstraintLayout>
</layout>

frame.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="replacementText"
            type="String" />
    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="64dp"
            android:text="@{replacementText}" />

    </FrameLayout>
</layout>
Cheticamp
  • 61,413
  • 10
  • 78
  • 131