Cody
Cody

Reputation: 1879

Android AlertDialog Not Displaying Entire XML

Bit of an odd problem, I have a layout that opens as an AlertDialog in my Android app but for some reason only the second of two LinearLayouts in the xml file is displayed. I didn't realize it was possible for a layout to have its views partially ignored. Oddly enough, the layout is drawn correctly if I use a PopupWindow instead of an AlertDialog, so I think theres some difference between the two causing the issue.

The XML layout in question is below. At its root, its a LinearLayout with two children, with respective weights 0.10 and 0.90. When drawn, the layout with weight 0.90 fills the entire parent and the layout with weight 0.10 is nowhere to be found. Changing these weight values does nothing to remedy this.

What is the fix?

menu_popup_set_chords.xml

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

<!-- This is the missing Layout on draw -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="vertical"
    android:layout_weight="0.10"
    android:background="#000000">
</LinearLayout>

<!-- This layout fills the entire dialog window instead of 90% of it -->
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#212121"
    android:layout_weight="0.90">

    <LinearLayout
        android:layout_weight="1.40"
        android:layout_width="0dp"
        android:layout_height="match_parent">

        <!-- Custom view for Canvas -->
        <com.example.foo.CustomChordCanvas
            android:id="@+id/chordCanvas"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/neck_hud_background_alpha85"/>

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:padding="8dp"
        android:layout_weight="3.60"
        android:layout_width="0dp"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3.10">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_userChords"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5">

            <LinearLayout
                android:id="@+id/fl_actionButtons"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_weight="0.5"
                    android:layout_width="0dp"
                    android:layout_height="match_parent">

                    <android.support.v7.widget.AppCompatImageButton
                        android:id="@+id/addMarker"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:padding="16dp"
                        android:clickable="true"
                        android:src="@drawable/ic_add_circle_black_24dp"
                        android:background="@android:color/transparent"
                        android:tint="#C4252C"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_weight="0.5"
                    android:layout_width="0dp"
                    android:layout_height="match_parent">

                    <android.support.v7.widget.AppCompatImageButton
                        android:id="@+id/removeMarker"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:background="@android:color/transparent"
                        android:padding="16dp"
                        android:src="@drawable/ic_remove_circle_black_24dp"
                        android:tint="#C4252C"/>

                </LinearLayout>

            </LinearLayout>


        </LinearLayout>

    </LinearLayout>

</LinearLayout>

For comprehensiveness, heres the code that builds and shows the AlertDialog in question:

FragmentChordMenu.java

public class FragmentChordMenu extends Fragment implements CustomChordAdapter.onItemClickListener {
public static int chordCanvasWidth, chordCanvasHeight;

private static RecyclerView mCustomChordList;
private static CustomChordAdapter mRecyclerViewAdapter;
private static Context mContext;

private FloatingActionButton mFAB;
private View mPopupView;
private AlertDialog mDialogChordMenu;
private CustomChordCanvas chordCanvas;
private ImageButton btnAddMarker, btnRemoveMarker;

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mRecyclerViewAdapter = new CustomChordAdapter(this);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    mContext = getActivity();   //stores application context for later use in fragment without risk
                                                        //of detachment

    View v = inflater.inflate(R.layout.menu_fragment_chord, container, false);
    LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    mPopupView = layoutInflater.inflate(R.layout.menu_popup_set_chords, null);

    mDialogChordMenu = new AlertDialog.Builder(getActivity()).setView(mPopupView).create();

    chordCanvas = (CustomChordCanvas) mPopupView.findViewById(R.id.chordCanvas);

    ...

    //This method fetches view parameters for one of the Dialog child layout 
    //A ViewTreeObserver is used to allow view parameters to be accessed BEFORE view is drawn on screen
    ViewTreeObserver viewTreeObserver = chordCanvas.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                chordCanvas.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                chordCanvasWidth = chordCanvas.getMeasuredWidth();
                chordCanvasHeight = chordCanvas.getMeasuredHeight();
                CustomChordCanvas.setViewParams(chordCanvasWidth, chordCanvasHeight);
            }
        });
    }

    mFAB = (FloatingActionButton) v.findViewById(R.id.addChord);
    mFAB.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mDialogChordMenu.show();
            mCustomChordList = (RecyclerView) mPopupView.findViewById(R.id.rv_userChords);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            mCustomChordList.setLayoutManager(layoutManager);
            mCustomChordList.setAdapter(mRecyclerViewAdapter);
        }
    });

    return v;
}
...}

Upvotes: 0

Views: 47

Answers (1)

Vidhi Dave
Vidhi Dave

Reputation: 5684

Set some default height to custom layout of dialog.

Upvotes: 1

Related Questions