0

I have an application that is always in landscape view. My AlertDialog windows are of the desired height, but are too narrow. See below,

Fig 1.- AlertDialog window in application landscape view.

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.

portsample
  • 1,986
  • 4
  • 19
  • 35
  • 1
    Is this working?: [Try wrapping your custom dialog layout into RelativeLayout instead of LinearLayout. That worked for me.](https://stackoverflow.com/a/15594269/724039) – Luuk Dec 16 '21 at 18:18
  • Doesn't work. Curious, even doing: ` int width = (int)(getResources().getDisplayMetrics().widthPixels*0.90); int height = (int)(getResources().getDisplayMetrics().heightPixels*0.90); alert.getWindow().setLayout(width, height);` isn't changing layout. – portsample Dec 16 '21 at 19:05

1 Answers1

0

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.

portsample
  • 1,986
  • 4
  • 19
  • 35