0

I made an options menu for my Android app (targeting API 29) that works perfectly in portrait mode. But if I rotate the device to see it in landscape mode, then the menu items go crazy: they show a gray space at the right, not filling the parent, not being clickable in the gray space, etc.

It seems that the match-parent layout is not being applied when in portrait?

Look:

Good portrait layout

Bad landscape layout

This is the code for the menu:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:padding="1dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:padding="8dp"
        android:drawablePadding="8dp"
        android:background="#111111" />
    
    <View
        android:id="@+id/sep"
        style="@style/Separator" />

    <ListView
        android:id="@+id/options"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:dividerHeight="2dp"
        android:longClickable="true" />

</LinearLayout>

and this is the code for the option items:

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@drawable/list_selector"
          android:ellipsize="end"
          android:textColor="#DDBBBB"
          android:padding="8dp"
          android:textSize="22sp"
          android:maxLines="1">
</TextView>

EDIT 1

I added more options to the menu, and the one in portrait also becomes equally messy when the dialog scrollbar appears. So it is not a problem of landscape/portrait orientation, but if the menu list fits in the popup dialog or not. Now I am even more confused.

EDIT 2

Switching to

android:layout_width="2000sp"

in the TextView 'magically' solves the issue. This is not a true solution but a hack, and hence may probably bring problems in the future...

EDIT 3

By request of BenP I am adding some extra code for the ListAdapter:

....
ArrayList<String> lst = new ArrayList<String>();
lst.add("Name to clipboard");
lst.add("Path to clipboard");
lst.add("Rename w/clipboard");
...
String[] options = new String[lst.size()];
options = lst.toArray(options);
final ListAdapter itemlist = new ArrayAdapter<String>(context, R.layout.optionsdialogitem, options);
final Dialog dial = new Dialog(context);
OnItemClickListener listener = new OnItemClickListener() {....}
optionsDialog(dial, file, getDrawableId(file), itemlist, listener, false);

And then the method optionsDialog() (in a separate library) builds the actual dialog. Hope this helps.

Luis A. Florit
  • 2,169
  • 1
  • 33
  • 58
  • Shouldn't your height of TextView in option item layout be wrap_content? And keep some fixed /wrapped height for your list view as well – Sarah Khan Dec 02 '20 at 01:25
  • @Sarah: Same problem with wrap_content (actually, I think I tried every possible combination!). What do you mean with "fixed /wrapped height for listview"? Please see my EDIT and Zain answer. – Luis A. Florit Dec 02 '20 at 01:45
  • Can you add your `ListView` `Adapter` code to the question? I'm specifically interested in the `getView()` method. – Ben P. Dec 02 '20 at 02:41
  • 1
    @BenP. I don't think it will be useful, but check my EDIT 3. No getView() method implemented. – Luis A. Florit Dec 02 '20 at 03:29

0 Answers0