Search code examples
androidxmllayoutandroid-activityfragment

How to add an Android fragment to an activity?


I would like to add a fragment to my main activity, so I have this fragment class (TitleFragment.java):

package com.myapp.app1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

Next is the actual content of my fragment, contained in fragment_title.xml:

<?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="match_parent"
    android:orientation="vertical" >
       <ImageView 
        android:id="@+id/logo_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo_main"
        android:contentDescription="@string/app_name"
        android:scaleType="fitStart"
        android:adjustViewBounds="true" />
</LinearLayout>

And inside my activity_main.xml I have this snippet amongst the regular content of the activity:

<fragment 
            android:name="com.myapp.app1.TitleFragment"
            android:id="@+id/title_fragment"
            android:layout_width="0dp"
            android:layout_height="match_parent" />

Is this method a correct way to create a fragment? My app just crashes and the LogCat seems to indicate it's to do with inflating the fragment view, but I'm not sure.

By the way the reason for this fragment is to have the app logo (image and some updateable text) that exists on every page, is this a good method to do something like that? Sorry for my newbie questions.


Solution

  • i have started fragment from my MainActivity. main activity extends FragmentActivity. the way i have used is:

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(R.id.body_frame, new MyFragment()).commit();
    

    in your case, it should look like:

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(R.id.title_fragment, new TitleFragment()).commit();
    

    remember i have used an FragmentActivity to start Fragment. i have also used android-support-v4.jar to support fragment in lower version OS. without android-support-v4.jar, FragmentManager manager = getSupportFragmentManager(); may be look like : FragmentManager manager = getFragmentManager();

    Edited:

    you should modify your fragment class:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_title, container, false);
        // you can use findViewById() using the above 'view'
          ......................
          ....your code........
           ................
        return view;
    }