0

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.

Jaimin Modi
  • 1,530
  • 4
  • 20
  • 72

2 Answers2

1

Why don't you just convert kotlin code to java we do have option in android studio.

you can go to Tools > Kotlin > Show kotlin bytecode 

And it will show you entire java code for that class.

And here is your solution :

  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);

        GradientDrawable [] layers =new GradientDrawable[] 
   {dimDrawable,navigationBarDrawable};

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

        window.setBackgroundDrawable(windowBackground);
    }
}
haresh
  • 1,424
  • 2
  • 12
  • 18
1

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}
Prashant
  • 4,775
  • 3
  • 28
  • 47