margins around fragment have no effect or cut the fragment off

32 Views Asked by At

I have this Fragment:

<FrameLayout 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/fragment_dual_sorting"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.dualsorting.DualSortingFragment">

    <!-- TODO: get rid of cut off buttons -->
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">


        <LinearLayout
            android:id="@+id/ll_simplebtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/horizontal_gap"
            android:orientation="horizontal"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@id/ll_complexbtn">

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btn_sort_importance"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:clickable="true" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btn_importance_icon"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:clickable="true" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_complexbtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/horizontal_gap"
            android:orientation="horizontal"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toEndOf="@id/ll_simplebtn"
            app:layout_constraintEnd_toEndOf="parent">

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btn_sort_options"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btn_options_icon"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2" />

        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>


</FrameLayout>

and I am instantianting it in SetFilterActivity like that:

 sortingFragment = DualSortingFragment.newInstance(manager.getSorting())
        supportFragmentManager.beginTransaction().replace(R.id.fragment_sorting, sortingFragment).commit()
    

This is how it is setup in the activity_set_filter.xml:

<androidx.fragment.app.FragmentContainerView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_sorting"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/filter_title"
        />

Text and custom background for each of these buttons are set like this

// Background
button1.setBackgroundResource(R.drawable.sorting_button)
button2.setBackgroundResource(R.drawable.sorting_icon_button)
// Icon
button2.setIconResource(R.drawable.baseline_keyboard_arrow_up_24)
// Text
button1.text = evaluatorManager.getEvaluator()?.getKeyword() // returns a String

How it looks

What I want: I want to give it a margin left and right (empty space). I tried adding a margin to FragmentContainerView in activity_set_filter.xml with:

android:layout_margin="16dp"

But this does nothing. Same for android:layout_marginHorizontal. Also thought of using android:layout_marginStart and ...End But this results in cutting of the buttons on both sides. android:layout_margin creates gaps vertically but there is no action horizontally

Using padding in either the parent or fragment layout results in cut-off buttons.

Buttons are cutoff with using padding

Question: Why does that not work? I suspect the buttons just don't want to go smaller, even though they visually definitly could?

I am using custom backgrounds for the buttons, could that be a reason?

EDIT: more information about how I set content in the buttons and example behaviour.

0

There are 0 best solutions below