John
John

Reputation: 8946

Displaying Custom Fragment in DailogFragment

I have a FragmentActivity which has a button, on clicking the button I am showing a dailog

public class FragMainActivity extends FragmentActivity implements android.view.View.OnClickListener {
    Button shoBut;
    public String DailogTag="dialog";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        shoBut= (Button) findViewById(R.id.button1);
        shoBut.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        MyDialogFragment newFragment = MyDialogFragment.newInstance();
        FragmentManager fm ;
        fm=getSupportFragmentManager();
        newFragment.show(fm, DailogTag);
    }
}

My dialog Class

public  class MyDialogFragment extends DialogFragment {

    static MyDialogFragment newInstance() {     
        return new MyDialogFragment();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View inView= inflater.inflate(R.layout.dialog_layout, container, false);

        return inView;
    }
}

this is my dialog_layout xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingBottom="36dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="36dp" >

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:gravity="center"
    android:text="Hi, i am the dailog BOX...Boom"

    android:textAppearance="?android:textAppearanceMedium" />

</LinearLayout>

I am getting the dailog properly But I have a Fragment class, ExampleFragment like this

public  class ExampleFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         // Inflate the layout for this fragment
         return inflater.inflate(R.layout.frag_content, container, false);
     }
}

Whose layout is

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:gravity="center"
    android:text="Fragment Conttent"

    android:textAppearance="?android:textAppearanceMedium" />

<RatingBar
    android:id="@+id/ratingBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

When i try to put this fragment in my dailog layout , my application is crashing.

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:gravity="center"
    android:text="You really want to read this text?"

    android:textAppearance="?android:textAppearanceMedium" />

    android:layout_width="match_parent" android:layout_height="match_parent">

   <fragment class="com.example.addfragment.ExampleFragment"
        android:id="@+id/example_fragment"
        android:layout_width="match_parent" android:layout_height="match_parent" />

</LinearLayout>

When I put it in my activity_main layout it shows the fragment, but the same does not work in dailogFragment.

I am getting

08-28 15:44:09.969: E/AndroidRuntime(438): android.view.InflateException: Binary XML file line #22: Error inflating class fragment

Anyone have any suggestion for this ? Why cant a fragment be included in dailogFragment layout ?

Why can't we add a fragment in a daiologFragment ?

Upvotes: 0

Views: 208

Answers (1)

user
user

Reputation: 87064

, why cant a fragment be included in dailogFragment layout ?

Because nested fragment can't be added to a parent fragment through inflation of a layout file containing them. Below is a note from the docs:

Note: You cannot inflate a layout into a fragment when that layout includes a < fragment >. Nested fragments are only supported when added to a fragment dynamically.

Any have any suggestion for this ?

Add the ExampleFragment in code: in the onCreateView() method of the dialog fragment create a instance of ExampleFragment and using getChildFragmentManager() add it to a container from dialog_layout.

Upvotes: 2

Related Questions