Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display one activity inside another in Android?

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?

like image 207
AMH Avatar asked May 27 '12 14:05

AMH


People also ask

How do you call one activity from another activity?

Start the Second Activity To start an activity, call startActivity() and pass it your Intent . The system receives this call and starts an instance of the Activity specified by the Intent .

Can activity run in background android?

However, activity can't be run unless it's on foreground. In order to achieve what you want, in onPause(), you should start a service to continue the work in activity. onPause() will be called when you click the back button. In onPause, just save the current state, and transfer the job to a service.

Can an activity have multiple layouts?

Yes its possible. You can use as many layouts as possible for a single activity but obviously not simultaneously. You can use something like: if (Case_A) setContentView(R.


1 Answers

Use one layout for several activities

Layout instance is inflated for each activity using a setContentView method, usually in onCreate:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_common_layout);
}

So there's no problem to use the same XML layout for different activities.

Display one activity inside another

You can use a Fragment API to complete the task. See full details in developer's guide.

  • Declare a layout like that and Android will create fragments for you:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <fragment android:name="com.example.MyFragment"
                android:id="@+id/my_fragment"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" />
    
    </LinearLayout>
    

    Then create a MyFragment class and load it when appropriate.

  • Create fragments yourself. Do not define the Fragment in XML layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/my_parent_layout">
    
    </LinearLayout>
    

    After your parent Activity is created you can add a new Fragment in this way:

    FragmentManager fragmentManager = getFragmentManager()
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    MyFragment fragment = new MyFragment();
    fragmentTransaction.add(R.id.my_parent_layout, fragment);
    fragmentTransaction.commit();
    

    Here MyFragment is defined like this:

    public class MyFragment extends Fragment {
        ...
    }
    

If you're targeting below Android 3.0, consider using support package for that.

like image 77
Andrey Ermakov Avatar answered Nov 11 '22 21:11

Andrey Ermakov