1
votes

I am facing a issue which i need to resolve by making my view intelligent about what type of view is used currently.

I have two layout one for Portrait and another for landscape(land).

  • MyFragmentLayout.xml
  • MyFragmentLayout.xml (land)

I am currently setting my child View of the Fragment with some Padding and i want the view to know which type of layout is currently in use.(Portrait/Landscape). This is my code i used:

 val verticalPadding = if (context.resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE)

            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50f, displayMetrics)

        else

            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150f, displayMetrics)

Above work's perfectly for orientation change case.

Problem:

I am giving Multi-Window support to my application And the issue i am facing is when Window is in portrait mode (i.e orientation is Portrait)

But application due to region available switches the layout from portrait mode to landscape mode.

ScreenShot As you can see the upper window with red bar is in Portrait mode but Application use's it's Landscape Layout.(which is perfect for my case)

I want to know how can know which type of layout is currently used by my application independent of the current Orientation of the mobile.

2
;D negative vote i don't think person got the context of question. Please read question then vote.I tried to search a lot and i found no solution.thanksAnmol

2 Answers

1
votes

I don't know if it's the best approach but you may give to your layouts different containers ids and search for those ids in onViewCreated method.

For example

  • MyFragmentLayout.xml:
androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/portraitContainer"
        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">
  • MyFragmentLayout.xml (land)
<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/landscapeContainer"
        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">

Then, in your onViewCreated method:

View v = view.findview(R.id.portraitContainer)
if (v !=null){
  // portrait layout loaded
} else{
  // landscape layout loaded
}

Let me know if this helps ;)

0
votes

My Current Solution for this problem is as below.

I have created:

  • dimens.xml (/values folder)

  • dimens.xml(land) (/values-land folder)

To store the Padding Dimension for different type's of layout(portrait and landscape).

val verticalPadding = context.resources
                             .getDimension
                             .getDimension(R.dimens.vertical_padding)

This is the best approach i found. I am looking for a better approach which i can control by JAVA/Kotlin code and not by separate xml files

In Above Solution when Application choses to use MyFragmentLayout.xml (land) (Landscape layout) then automatically Application take's dimensions from dimens(land)