Adding a fragment to a dialog

那年仲夏 提交于 2019-12-04 01:31:35
Kalisky

The answer (thanks to @Luksprog) is using the getChildFragmentManager instead of getActivity().getSupportFragmentManager

It wasn't available for me, cause I had to upgrade my support-v4 jar, as described here: android.support.v4.app.Fragment: undefined method getChildFragmentManager()

Layout for Dialog - R.layout.view_with_plus

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.util.me.TestActivity"
    >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Details"/>
    <fragment
        android:layout_toRightOf="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/email"
        class="com.util.me.test.PlusOneFragment"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

How to show the Dialog

public void showDialog(View vIew){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    View view = this.getLayoutInflater().inflate(R.layout.view_with_plus, null);
    builder.setView(view)
            .setPositiveButton("OK", null)
            .setNegativeButton("Cancel", null);

    AlertDialog dialog = builder.create();
    dialog.show();
}

The fragment in class="com.util.me.test.PlusOneFragment" is just a PlusOneFragment generated by Android Studio.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!