user3146095
user3146095

Reputation: 419

Creating fragments programmatically Android Xamarin

I am creating fragment in Android Xamarin.

This is what I have done so far:

PortfolioFragment.cs

class PortfolioFragment: Fragment
{
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        base.OnCreateView(inflater, container, savedInstanceState);

        var view = inflater.Inflate(Resource.Layout.portfolio_fragment, container, false);

        return view;
    }
}

fragment.axml

<?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:minWidth="25px"
    android:minHeight="25px">
    <TextView
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1" />
</LinearLayout>

Main.cs

 PortfolioFragment pFragment = new PortfolioFragment();
      FragmentTransaction fragmentTx = this.FragmentManager.BeginTransaction().Replace(Resource.Id.fragment_container, pFragment);

But I receive an error:

Error   1   'MyProject.Resource.Id' does not contain a definition for 'fragment_container'  

What can be wrong?

Edit:

I tried changing to:

homeactivity.axml

    <?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:minWidth="25px"
    android:minHeight="25px">
    <FrameLayout
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frame_container" />
</LinearLayout>

Main.cs

SetContentView(Resource.Layout.homeactivity);



             HomeFragment hFragment = new HomeFragment(this);
             FragmentTransaction fTx = this.FragmentManager.BeginTransaction().Add(Resource.Id.frame_container, hFragment);

Now I get a error:

 Error  3   The best overloaded method match for 'Android.App.FragmentTransaction.Add(int, Android.App.Fragment)' has some invalid arguments    
Error   4   Argument 2: cannot convert from 'MyProject.Views.HomeFragment' to 'Android.App.Fragment'    

Upvotes: 0

Views: 9646

Answers (1)

Chatura Dilan
Chatura Dilan

Reputation: 1572

Resource.Id.fragment_container name is wrong

Seems you do not have a fragment container in your Activity. Create a Layout (FrameLayout) to hold your fragment in your activity and give that layout id to the first parameter of replace method.

Eg. R.id.fragment_container

Please follow this tutorial

http://wptrafficanalyzer.in/blog/dynamically-add-fragments-to-an-activity-in-android/

Upvotes: 2

Related Questions