Reputation: 1232
Following a tutorial in https://developer.android.com/guide/topics/large-screens/make-apps-fold-aware, which is in Kotlin, I'm attempting to use it in Java. I have issues with the library not being recognised. One of many compilation errors is: Cannot resolve method 'getInstance' in 'WindowManager'. Does anyone know how to check if a device is flat or folded in Android Java?
This is my code, but there are lots of compilation errors indicating the new library is not brought in.
build.gradle
implementation 'androidx.window:window:1.0.0'
In Activity which extends AppCompactActivity
import androidx.window.layout.FoldingFeature;
import androidx.window.layout.WindowLayoutInfo;
import androidx.window.layout.WindowMetrics;
import androidx.window.WindowManager;
WindowManager windowManager = WindowManager.getInstance(this);
LiveData<WindowLayoutInfo> layoutInfoLiveData = windowManager.getWindowLayoutInfo();
layoutInfoLiveData.observe(this, new Observer<WindowLayoutInfo>() {
@Override
public void onChanged(WindowLayoutInfo windowLayoutInfo) {
List<FoldingFeature> foldingFeatures = windowLayoutInfo.getDisplayFeaturesOfType(FoldingFeature.class);
if (!foldingFeatures.isEmpty()) {
// This is a foldable device
FoldingFeature foldingFeature = foldingFeatures.get(0);
switch (foldingFeature.getState()) {
case FoldingFeature.State.FLAT:
// The device is currently flat/unfolded
break;
case FoldingFeature.State.HALF_OPENED:
// The device is half-opened (like a laptop)
break;
case FoldingFeature.State.FOLDED:
// The device is fully folded
break;
}
}
}
});
Upvotes: 3
Views: 1222