2

I am converting an old project to viewBinding. BaseActivity has a coordinator layout. App changes view of relative layout which has main_view as id. For example when user opens WorkActivity, main_view switches with activity_work.

activity_base.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_main_screen"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <include
        android:id="@+id/toolbar_layout"
        layout="@layout/toolbar_layout" />

    <RelativeLayout
        android:id="@+id/main_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

    <RelativeLayout
        android:id="@+id/title_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="gone" />

    <RelativeLayout
        android:id="@+id/bottom_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="gone" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

replaceViews function in BaseActivity:

    private void replaceViews(View holder, int layoutID) {
        View toReplace = getLayoutInflater().inflate(layoutID, binding.coordinator, false);
        ViewTools.replaceView(holder, toReplace);
    }

viewTools.replaceView function :

    public static void replaceView(View currentView, View newView) {
        ViewGroup parent = getParent(currentView);
        if (parent == null) {
            return;
        }
        final int index = parent.indexOfChild(currentView);
        removeView(currentView);
        removeView(newView);
        parent.addView(newView, index);
    }

Code snippet below (from replaceViews function in BaseActivity) seems to be the trouble, since binding has no effect or layout inflater does not inflate the view.

    View toReplace = getLayoutInflater().inflate(layoutID, binding.coordinator, false);

Is viewBinding compatible with replace operations like this? Did I forget any operation for layoutInflater?

moken
  • 3,227
  • 8
  • 13
  • 23
alpertign
  • 296
  • 1
  • 4
  • 13
  • well i could be mistaken but i doubt that viewbinding will be able to handle this, i don't think there's support for switching out views – a_local_nobody Mar 14 '22 at 13:54
  • I guess you are using 1 BaseActivity and multiple normal activity. All activities has not any setContentView method right? Normal activities are extending BaseActivity and you are using always BaseActivity's XML file. And you are changing main_view, title_view or bottom_view sometimes. When you find solutution please let me know :) – Emefar Mar 14 '22 at 13:57
  • Also check layout params of newly added view, to confirm it's size is not 0, or that some other view did not take whole screen and there is no space for new views. Also try activityBaseBinding.inflate(...) – Torima Mar 14 '22 at 14:00
  • @Torima newly added layout params are -1 so getLayoutInflater().inflate(layoutID, binding.coordinator, false); function probably gives null – alpertign Mar 14 '22 at 14:16
  • TextView status = findViewById(R.id.status_element); is working but binding.statusElement is not working. I cannot change text, visibility or click listener with viewBinding. But i can change with findViewById. I cannot access status or any element via binding. And also i compared ids and 2 ids are equal. – alpertign Mar 14 '22 at 14:23
  • @AlperenAcikgoz try setting new layout params like `tv.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));` – Torima Mar 14 '22 at 14:33
  • Can you add onCreate of your activity? Do you have something like `binding = ActivityBaseBinding.inflate(layoutInflater) setContentView(binding.root)`? – Torima Mar 14 '22 at 14:36

0 Answers0