I am currently facing the problem where the whole content of my DialogFragment
disappears when I change the screen orientation to landscape mode.
fragment_dartboard.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dartboard_fragment"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:src="@drawable/base"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:importantForAccessibility="no"
android:id="@+id/base" />
</androidx.constraintlayout.widget.ConstraintLayout>
DartboardDialogFragment.java
public class DartboardDialogFragment extends DialogFragment {
...
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_dartboard, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);
ConstraintLayout layout = view.findViewById(R.id.dartboard_fragment);
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.topToTop = layout.getId();
params.startToStart = layout.getId();
final TextView step = new TextView(getContext());
step.setTextColor(0xFFE4FF5A);
step.setTextSize(TypedValue.COMPLEX_UNIT_SP, 40);
step.setText("1");
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if(getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
lp.startToEnd = R.id.base;
lp.topToTop = R.id.dartboard_fragment;
lp.bottomToBottom = R.id.dartboard_fragment;
}
else {
lp.startToStart = R.id.dartboard_fragment;
lp.endToEnd = R.id.dartboard_fragment;
lp.topToBottom = R.id.base;
}
step.setLayoutParams(lp);
layout.addView(step);
...
}
In portrait-mode
Removing the line layout.addView(step)
, the picture stays after rotation, but my intention is to show the TextView
on the bottom in portrait and on the right in landscape.
I had to change the LayoutParams
of the ImageView
on orientation change:
So changing
if(getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
lp.startToEnd = R.id.base;
lp.topToTop = R.id.dartboard_fragment;
lp.bottomToBottom = R.id.dartboard_fragment;
}
to
if(getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
view.findViewById(R.id.base).setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
lp.startToEnd = R.id.base;
lp.topToTop = R.id.dartboard_fragment;
lp.bottomToBottom = R.id.dartboard_fragment;
}
resolved the issue.