Search code examples
androidclassandroid-viewpagerandroid-fragmentactivitymain-activity

Android: Using ViewPager to fill RelativeLayout from MainActivity


Ok I'll first try it short. Does anyone know how I insert this class

public class ResultBar extends FragmentActivity implements ActionBar.TabListener {
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pagerlayout);
   ...

from the MainActivity into its ViewPager layout?


Solution

  • It is not possible to add one activity into another activity. Android use Fragments for this functionality. You can learn few things about them here or here.

    So you can convert your ResultBar to Fragment, also converting your MainActivity to FragmentActivity, its previous content to MainActivityContentFragment and add ResultBarFragment to your MainActivity. This is how your main activity layour could look like:

    /res/layout/main_activity.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent" android:layout_height="match_parent">
    
        <fragment class="com.example.MainActivityContentFragment
                android:id="@+id/content" android:layout_weight="1"
                android:layout_width="0px" android:layout_height="match_parent" />
    
        <fragment class="com.example.ResultBarFragmnt
                android:id="@+id/result_bar" android:layout_weight="1"
                android:layout_width="0px" android:layout_height="match_parent" />
    
    </LinearLayout>
    

    Here also snippet how your ResultBarFragment could look like:

    public class ResultBarFragment extends Fragment
    {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            return inflater.inflate(R.layout.pagerlayout, container, false);
        }