Jigen
Jigen

Reputation: 465

Fragment doesn't match_parent

I just started to use fragments but I have problems to use them correctly.

This is my case. I'm using this BottomBar with some items, when user clicks on items fragments change. That's ok, but I can't set a match_parent on the second card's height. I'd like to use all the remaining space. This is the result:

Image1

This is code in my CDetails.java where I use the BottomBar and the FragmentManager:

 // BottomBar
            mBottomBar = BottomBar.attach(CustomersDetails.this, savedInstanceState);
            //mBottomBar.hideShadow();
            mBottomBar.noNavBarGoodness();
            mBottomBar.noResizeGoodness();
            //mBottomBar.noScalingGoodness();
            mBottomBar.noTabletGoodness();
            //mBottomBar.useFixedMode();
            mBottomBar.setItems(R.menu.bottom_navigation_customers);
            mBottomBar.setOnMenuTabClickListener(new OnMenuTabClickListener() {
                @Override
                public void onMenuTabSelected(@IdRes int menuItemId) {
                    if (menuItemId == R.id.bottomBarCustomerNotes) {

                        FragmentManager fragmentManager = getFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        CustomersFragmentsNotes notes = new CustomersFragmentsNotes();
                        fragmentTransaction.replace(R.id.fragment_container, notes);
                        fragmentTransaction.commit();

                    }
                    if (menuItemId == R.id.bottomBarCustomerTickets) {

                        FragmentManager fragmentManager = getFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        CustomersFragmentsTickets tcks = new CustomersFragmentsTickets();
                        fragmentTransaction.replace(R.id.fragment_container, tcks);
                        fragmentTransaction.commit();

                    }
                    if (menuItemId == R.id.bottomBarCustomerContracts) {

                        FragmentManager fragmentManager = getFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        CustomersFragmentContracts cntrs = new CustomersFragmentContracts();
                        fragmentTransaction.replace(R.id.fragment_container, cntrs);
                        fragmentTransaction.commit();

                    }
                    if (menuItemId == R.id.bottomBarCustomerContacts) {

                        FragmentManager fragmentManager = getFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        CustomersFragmentContacts cnts = new CustomersFragmentContacts();;
                        fragmentTransaction.replace(R.id.fragment_container, cnts);
                        fragmentTransaction.commit();

                    }
                }

                @Override
                public void onMenuTabReSelected(@IdRes int menuItemId) {

                }
            });

            //Setting colors
            mBottomBar.mapColorForTab(0, "#F44336");
            mBottomBar.mapColorForTab(1, "#7E57C2");
            mBottomBar.mapColorForTab(2, "#5C6BC0");
            mBottomBar.mapColorForTab(3, "#42A5F5");

This is my activity_cdetails.xml (the container):

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

<android.support.v4.widget.DrawerLayout 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/left_drawer_activity_cdetails"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/right_drawer_activity_cdetails"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/sfondo_sfumato"
        >

        <android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            >

            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme.AppBarOverlay"
                android:id="@+id/appbar_activity_cdetails">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar_activity_cdetails"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay" />

            </android.support.design.widget.AppBarLayout>


            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_marginTop="55dp"
                >

                <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:card_view="http://schemas.android.com/apk/res-auto"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginEnd="10dp"
                    card_view:cardCornerRadius="5dp"
                    card_view:cardBackgroundColor="#FFFFFF"
                    card_view:cardElevation="7dp"
                    >

                    <!--<ScrollView-->
                        <!--android:layout_width="match_parent"-->
                        <!--android:layout_height="match_parent">-->

                    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_margin="5dp"
                        android:weightSum="1"
                        android:orientation="vertical"
                        >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:id="@+id/title_cdetails"
                            android:layout_marginTop="5dp"
                            android:layout_marginBottom="5dp"
                            android:text="Title"
                            android:textSize="22sp"
                            android:typeface="serif"
                            android:layout_centerHorizontal="true"
                            android:layout_gravity="center"
                            />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="New Text"
                            android:layout_marginBottom="5dp"
                            android:layout_marginLeft="5dp"
                            android:textSize="18sp"
                            android:layout_below="@id/title_cdetails"
                            android:id="@+id/cdetails_nome"
                            />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="5dp"
                            android:layout_marginLeft="5dp"
                            android:layout_below="@id/cdetails_nome"
                            android:text="New Text"
                            android:textSize="18sp"
                            android:id="@+id/cdetails_città"
                            />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="New Text"
                            android:layout_marginBottom="5dp"
                            android:layout_marginLeft="5dp"
                            android:layout_below="@id/cdetails_città"
                            android:textSize="18sp"
                            android:id="@+id/cdetails_indirizzo"
                            />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="New Text"
                            android:layout_below="@id/cdetails_indirizzo"
                            android:layout_marginBottom="5dp"
                            android:layout_marginLeft="5dp"
                            android:textSize="18sp"
                            android:id="@+id/cdetails_provincia"
                            />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="New Text"
                            android:layout_below="@id/cdetails_provincia"
                            android:layout_marginBottom="5dp"
                            android:layout_marginLeft="5dp"
                            android:textSize="18sp"
                            android:id="@+id/cdetails_phone"
                            android:autoLink="phone"
                            />

                    </RelativeLayout>

                    <!--</ScrollView>-->

                </android.support.v7.widget.CardView>

                <!-- Fragment Container -->
                <LinearLayout
                    android:id="@+id/fragment_container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    >

                </LinearLayout>

            </LinearLayout>

            </ScrollView>


        </android.support.design.widget.CoordinatorLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view_right_activity_cdetails"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_right_c"
            app:menu="@menu/menu_right_c" />

    </android.support.v4.widget.DrawerLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view_left_activity_cdetails"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/menu_left_drawer" />

</android.support.v4.widget.DrawerLayout>

And this is activity_c_fragment_notes.xml (first fragment):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="10dp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        card_view:cardCornerRadius="5dp"
        card_view:cardBackgroundColor="#B2EBF2"
        card_view:cardElevation="7dp"
        >

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:text="This is Notes"
        />

    </android.support.v7.widget.CardView>

</LinearLayout>

Could anyone please tell me where I'm wrong?

Thanks in advance.

Edit

RRR solution works only on the default selected item, the first fragment. If I change item in the BottomBar second card's layout is the same as the pic I posted. Any other ideas?

Upvotes: 1

Views: 561

Answers (1)

Ravi
Ravi

Reputation: 35589

use android:fillViewport=true for your ScrollView inside CoordinatorLayout

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

fillViewPort defines whether the scrollview should stretch its content to fill the viewport.

source

Upvotes: 5

Related Questions