Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout stretching when dismissing DialogFragment

I have an Activity that has the following layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical">

    <RelativeLayout
        android:id="@+id/top_menu_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="16dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        // ICONS

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/temperatura_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="184dp"
        android:layout_centerHorizontal="true">

        <com.github.pavlospt.CircleView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/reading_value"
            android:layout_width="192dp"
            android:layout_height="192dp"
            android:layout_gravity="center"
            />
    </FrameLayout>

    <ImageView
        android:id="@+id/add_item_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottomBar"
        android:layout_centerHorizontal="true"
        android:padding="12dp"
        android:layout_marginTop="36dp"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:clickable="true"
        android:focusable="true"
        android:scaleType="center"
        app:srcCompat="@drawable/ic_add_item_black_24px"
        android:contentDescription="@string/add_item_btn_description" />

    <com.roughike.bottombar.BottomBar
        android:id="@+id/bottomBar"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        app:bb_behavior="iconsOnly"
        app:bb_tabXmlResource="@xml/bottom_navbar" />

</RelativeLayout>

The add_item_btn has an event on click that creates a full-screen (edited to add this information) DialogFragment to insert the item information and add it to local database. The following code shows how I create the DialogFragment:

FragmentManager fragmentManager = getSupportFragmentManager();
InsertItemDialogFragment fragment = new InsertItemDialogFragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.add(android.R.id.content, fragment).addToBackStack(null).commit();

The problem is that when I dismiss() the created fragment, the activity layout stretches, in a way that the BottomBar goes half behind the on-screen button bar (see images).

enter image description here enter image description here

I tried changing the FragmentTransaction (from android.R.id.content to R.id.main_content, when main_content was the id of the root RelativeLayout) with no success. I also tried using 2 different implementations of the Material Design Bottom Navigation Bar, but both had the same behaviour.

like image 947
Minoru Avatar asked Aug 21 '17 16:08

Minoru


2 Answers

Found the answer! I don't actually know why, since I have almost no experience with CoordinatorLayout, but switching the root Layout of the FragmentDialog to LinearLayout solved the problem.

like image 145
Minoru Avatar answered Oct 01 '22 02:10

Minoru


First: Your are changing your Activity to fullscreen and calling to add the fragment. After dismissing the dialog you have to make the activity restore its layout the same way you did to set it as fullscreen but this time as a normal screen. The space you see there is the space use by the android's systems bar.

Second: Don't use relative layout. Use a linear layout to layout your activity learn to use weights and use if you need too.

Third: Why are you using a coordinator layout in your Dialog fragment. You don't need to.

Fourth: Android has its own bottom bar https://developer.android.com/reference/android/support/design/widget/BottomNavigationView.html Use it. They even have a working example and template you can see from Android Studio.

It is really hard to know what is wrong with your app with the information you provided

like image 22
Pedro Varela Avatar answered Oct 01 '22 04:10

Pedro Varela