'AlertDialog window in landscape view is not wide enough
I have an application that is always in landscape view. My AlertDialog windows are of the desired height, but are too narrow. See below,
Code from this fragment is:
private void changeSpeciesNoticeDialog() {
AlertDialog.Builder changeSpeciesNoticeDialog = new AlertDialog.Builder(getActivity());
View v = this.getLayoutInflater().inflate(R.layout.change_species, null);
changeSpeciesNoticeDialog.setView(v);
final AlertDialog alert = changeSpeciesNoticeDialog.create();
TextView tvShowSpecies = v.findViewById(R.id.tvchangedfieldlabelxml);
tvShowSpecies .setTypeface(null, Typeface.BOLD);
tvShowSpecies .setText(szSpecies);
alert.setView(v);
alert.show();
}
XML code is:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvchangedfieldlabelxml"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="@color/yellow"
android:gravity="center_horizontal|fill_horizontal"
android:text="Chinook"
android:textColor="@color/black"
android:textSize="80pt"
android:textStyle="bold"
android:textAlignment="center"
tools:layout_editor_absoluteX="11dp"
tools:layout_editor_absoluteY="64dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Finally, note that orientation is set in MainActivity thus,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
...
}
Thanks in advance.
Solution 1:[1]
Code from LUUK's comment here has helped to resolve this for me.
Now reads:
private void changeSpeciesNoticeDialog() {
AlertDialog.Builder changeSpeciesNoticeDialog = new AlertDialog.Builder(getActivity());
View v = this.getLayoutInflater().inflate(R.layout.change_species, null);
changeSpeciesNoticeDialog.setView(v);
final AlertDialog alert = changeSpeciesNoticeDialog.create();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alert.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
alert.getWindow().setAttributes(lp);
TextView tvShowSpecies = v.findViewById(R.id.tvchangedfieldlabelxml);
tvShowSpecies .setTypeface(null, Typeface.BOLD);
tvShowSpecies .setText(szSpecies);
alert.show();
}
AlertDialog now fills the whole width of the screen and enough of the height to fit the text.
UPDATE: Additional relevent material here... How to set the height and width of the Default Alert Dialog
Setting,
alertDialog.getWindow().setLayout(100, 100);
after calling
alertDialog.show();
worked as well.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

