Wednesday, 28 August 2013

Displaying Custom Fragment in DailogFragment

Displaying Custom Fragment in DailogFragment

I have a FragmentActivity which have a button, onclick of 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 dailog 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 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 ie
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 is 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
Any have any suggestion for this ?, why cant a fragment be included in
dailogFragment layout ?

No comments:

Post a Comment