Vivek Patel
Vivek Patel

Reputation: 1

i am having problem on creating bottomNavigationView related to height, the height is doubled as i expected even when the height is set to wrap

    <com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottomAppBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primary"
    app:itemIconSize="20dp"
    app:itemTextAppearanceActive="@style/BottomNavigationView.Active"
    app:itemTextAppearanceInactive="@style/BottomNavigationView.Inactive"
    android:layout_alignParentBottom="true"
    app:menu="@menu/bottom_menu"/>

enter image description here

i am expecting default height of the bottom navigation view.

Upvotes: 0

Views: 70

Answers (2)

Mahedi Hasan
Mahedi Hasan

Reputation: 1

If you are using this in an Activity then, remove these following lines form the onCreate() method,

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
    val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
    v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
    insets
}

And Add this following lines

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
    val params = binding.bottomNavigationView.layoutParams as ViewGroup.MarginLayoutParams
    params.bottomMargin = insets.systemWindowInsetBottom
    binding.bottomNavigationView.layoutParams = params
    insets.consumeSystemWindowInsets()
}

And If you are using it in a fragment then, remove these following lines form the onCreate() method of the host activity,

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
    val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
    v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
    insets
}

And add this following lines to the onViewCreated() method of the fragment

ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
    val params = binding.bottomNavigationView.layoutParams as ViewGroup.MarginLayoutParams
    params.bottomMargin = insets.systemWindowInsetBottom
    binding.bottomNavigationView.layoutParams = params
    insets.consumeSystemWindowInsets()
}

this will solve the extra space issue.

Upvotes: 0

SoyMey25
SoyMey25

Reputation: 1

I don't know if it is the solution that you are waiting, but I solved this deleting next piece of code on my Activity.kt:

enableEdgeToEdge()

and

    ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
        val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
        v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
        insets
    }

Another way is changing

setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)

for

setPadding(systemBars.left, systemBars.top, systemBars.right, 0)

Upvotes: 0

Related Questions