Home > Back-end >  BindingAdapter to set a layout fullscreen
BindingAdapter to set a layout fullscreen

Time:10-07

I am trying to replicate, in java, the following code written in Kotlin. It practically set the layout in fullscreen following some logic, in particular the binding takes place on the element CoordinatorLayout through app:layoutFullscreen="@{true}".

@BindingAdapter("layoutFullscreen")
fun View.bindLayoutFullscreen(previousFullscreen: Boolean, fullscreen: Boolean) {
    if (previousFullscreen != fullscreen && fullscreen) {
        systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
            View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
    }
}

I tried to turn everything into java but I have various errors

@BindingAdapter("layoutFullscreen")
public static void bindLayoutFullscreen(Boolean previousFullscreen, Boolean fullscreen) {
    if (previousFullscreen != fullscreen && fullscreen) {
        View.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
    }
}

How can I get the same functioning of the kotlin code in java?

Thanks in advance, Giacomo.

CodePudding user response:

Notice that:

  • Kotlin extensions functions is provided as the first parameter to java, so here the view is the first parameter.
  • The or is | in java.
  • Java provides setters/getters instead of directly accessing attributes, so that the setter version of systemUiVisibility used on the view
  • Semicolons ; in java end of line is a must.

Here is the working java version:

@BindingAdapter("layoutFullscreen")
public static void bindLayoutFullscreen(View view, boolean previousFullscreen, boolean fullscreen) {
    if (previousFullscreen != fullscreen && fullscreen) {
        view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
    }
}
  • Related