Background
I wanted to support full screen navigation UI as shown here:
https://medium.com/androiddevelopers/windowinsets-listeners-to-layouts-8f9ccc8fa4d1 https://developer.android.com/guide/navigation/gesturenav
The problem
While all Activities of my app worked fine, I suddently reached a problematic one that has a RecyclerView with thumbs.
Here, I got 2 weird issues:
- When scrolling to the bottom, the last item/s don't get fully shown.
- When in landscape mode, the thumbs go outside of anywhere that I can reach, so they are also not touchable. Not only that, but I can also see the normal scrollbar, and both get to disappear when scrolling to the bottom:
What I've tried
I tried to apply the insets to various views, including both padding and margins, but nothing helped.
Also, in some websites I saw that I should use View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
and in some that I need to also add View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
. Didn't help.
Here's my current code (project available here, as I think this is a bug) :
MainActivity.kt
class MainActivity : AppCompatActivity(R.layout.activity_main) {
inline fun View.updateMargins(@Px left: Int = marginLeft, @Px top: Int = marginTop, @Px right: Int = marginRight, @Px bottom: Int = marginBottom) {
updateLayoutParams<ViewGroup.MarginLayoutParams> {
this.bottomMargin = bottom
this.topMargin = top
this.leftMargin = left
this.rightMargin = right
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setSupportActionBar(toolbar)
findViewById<View>(android.R.id.content).systemUiVisibility =
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
ViewCompat.setOnApplyWindowInsetsListener(appBarLayout) { _, insets ->
val systemWindowInsets = insets.systemWindowInsets
appBarLayout.updateMargins(
left = systemWindowInsets.left,
top = systemWindowInsets.top,
right = systemWindowInsets.right
)
insets
}
ViewCompat.setOnApplyWindowInsetsListener(recyclerView) { _, insets ->
val systemWindowInsets = insets.systemWindowInsets
recyclerView.updatePadding(
left = systemWindowInsets.left,
bottom = systemWindowInsets.bottom,
right = systemWindowInsets.right
)
insets
}
recyclerView.adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
init {
setHasStableIds(true)
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): RecyclerView.ViewHolder {
return object : RecyclerView.ViewHolder(
LayoutInflater.from(this@MainActivity).inflate(R.layout.list_item, parent, false)
) {}
}
override fun getItemId(position: Int): Long = position.toLong()
override fun getItemCount(): Int = 100
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
holder.itemView.imageView.setColorFilter(if (position % 2 == 0) 0xffff0000.toInt() else 0xff00ff00.toInt())
holder.itemView.textView.text = "item $position"
}
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menu.add("test").setIcon(android.R.drawable.ic_dialog_email).setOnMenuItemClickListener {
true
}.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
return super.onCreateOptionsMenu(menu)
}
}
styles.xml (I use AppTheme in the manifest as theme)
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:navigationBarColor" tools:targetApi="lollipop">
@android:color/transparent
</item>
<item name="android:statusBarColor" tools:targetApi="lollipop">@color/colorPrimaryDark
</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar" />
</resources>
activity_main.xml
<androidx.coordinatorlayout.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" tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout" android:layout_width="match_parent" android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<!--app:popupTheme="@style/AppTheme.PopupOverlay"-->
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:scrollbars="vertical" app:fastScrollEnabled="true"
app:fastScrollHorizontalThumbDrawable="@drawable/thumb_drawable"
app:fastScrollHorizontalTrackDrawable="@drawable/line_drawable"
app:fastScrollVerticalThumbDrawable="@drawable/thumb_drawable"
app:fastScrollVerticalTrackDrawable="@drawable/line_drawable"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:itemCount="100"
tools:listitem="@layout/list_item" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
line.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@android:color/darker_gray" />
<padding
android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
</shape>
line_drawable.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/line" android:state_pressed="true" />
<item android:drawable="@drawable/line" />
</selector>
thumb.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners
android:bottomLeftRadius="44dp" android:topLeftRadius="44dp" android:topRightRadius="44dp" />
<padding
android:paddingLeft="22dp" android:paddingRight="22dp" />
<solid android:color="@color/colorPrimaryDark" />
</shape>
thumb_drawable.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/thumb" android:state_pressed="true" />
<item android:drawable="@drawable/thumb" />
</selector>
The questions
- Why does it occur? It worked fine for various other places...
- How can I make the RecyclerView avoid both these cases, yet allow the navigation bar at the bottom show content of the RecyclerView as it is transparent?
- In which cases should I add the flag
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
? What does it do to help in these cases?
EDIT: a possible workaround is to avoid using CoordinatorLayout. It works well, but I wanted to do things "the official way". Here's this workaround:
Instead of CoordinatorLayout I used :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
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" tools:context=".MainActivity">
...
And in code, I've set both margins and padding to the RecyclerView:
ViewCompat.setOnApplyWindowInsetsListener(recyclerView) { _, insets ->
val systemWindowInsets = insets.systemWindowInsets
recyclerView.updatePadding(
bottom = systemWindowInsets.bottom
)
recyclerView.updateMargins(
left = systemWindowInsets.left,
right = systemWindowInsets.right
)
insets
}