javaandroidkotlinlayerdrawable

Kotlin - Issue in converting Kotlin method to java for LayerDrawable


Below is the converted java function from Kotlin code funcion.

@RequiresApi(api = Build.VERSION_CODES.M)
public void setWhiteNavigationBar(@NonNull Dialog dialog) {
    Window window = dialog.getWindow();
    if (window != null) {
        DisplayMetrics metrics = new DisplayMetrics();
        window.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        GradientDrawable dimDrawable = new GradientDrawable();
        GradientDrawable navigationBarDrawable = new GradientDrawable();
        navigationBarDrawable.setShape(GradientDrawable.RECTANGLE);
        navigationBarDrawable.setColor(Color.WHITE);

        val layers = arrayOf<Drawable>(dimDrawable, navigationBarDrawable)

        LayerDrawable windowBackground = new LayerDrawable(layers);
        windowBackground.setLayerInsetTop(1, metrics.heightPixels);

        window.setBackgroundDrawable(windowBackground);
    }
}

I have trouble for the below line inside that fucntion. I am confused how can I write below kotlin line in Java :

val layers = arrayOf<Drawable>(dimDrawable, navigationBarDrawable)

So, Anyone please guide how can we write this line in java ?

Thanks.


Solution

  • As it turns out arrayOf() in kotlin is a method to create an array of specific type. In your case Drawable. In Java, you can create this by:

    Drawable[] drawables = new Drawable[] {dimDrawable, navigationBarDrawable}
    

    You can omit new Drawable[] and write:

    Drawable[] drawables = {dimDrawable, navigationBarDrawable}