I have one activity and want to display another inside it. Here’s my layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
How can I display an Activity in inner LinearLayout on button click?
Use one layout for several activities
Layout instance is inflated for each activity using a
setContentViewmethod, usually inonCreate:So there’s no problem to use the same
XMLlayout for different activities.Display one activity inside another
You can use a
FragmentAPI to complete the task. See full details in developer’s guide.Declare a layout like that and Android will create fragments for you:
Then create a
MyFragmentclass and load it when appropriate.Create fragments yourself. Do not define the
FragmentinXMLlayout:After your parent
Activityis created you can add a newFragmentin this way:Here
MyFragmentis defined like this:If you’re targeting below Android 3.0, consider using support package for that.