To start an fragment please follow the steps :
Put the below code in the fragment :
Note : 1. Remember fragment can not start itself it always need a activity to start.
So you must add a frame layout in the main layout page.
2. You need to create the axml page in the layout folder and link it with the fragment because fragment have their own design view ex; Resource.Layout.Fragment2
Framelayout :
- Create a new or existing project in Visual studio 2015
- Create a Folder name "Fragments" in the root directory
- Right click on the fragment folder and add the new item.
- Choose the Fragment from the templates.
Put the below code in the fragment :
public class Fragment2 : Fragment
{
private Context _context;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.Fragment2,
container, false);
_context = container.Context;
return view;
}
}
Note : 1. Remember fragment can not start itself it always need a activity to start.
So you must add a frame layout in the main layout page.
2. You need to create the axml page in the layout folder and link it with the fragment because fragment have their own design view ex; Resource.Layout.Fragment2
Framelayout :
Frame Layout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.
Steps to add a framelayout in the main page
- Go to the Resources folder , under this go to the Layout folder.
- Open Main.axml page and go to the coding view on the left bottom of screen.
- Put the below code in the Main.axml coding area.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="54dp"
android:minWidth="25dp"
android:visibility="gone"
android:minHeight="30dp"
android:id="@+id/linearLayoutHomeScreenMainActivity"
android:background="#27a9e1">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="@+id/imgBackMainActivity"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_gravity="center"
android:src="@drawable/back" />
<TextView
android:text="MANDDO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mainTitleMainActivity"
android:layout_weight="10"
android:textColor="#ffffff"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:textSize="20dp"
android:gravity="center" />
<ImageView
android:layout_width="25dp"
android:layout_height="20dp"
android:id="@+id/imgMenuMainActivity"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:visibility="visible"
android:layout_marginRight="10dp"
android:src="@drawable/menunew" />
</LinearLayout>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomActionBar"
android:layout_below="@+id/linearLayoutHomeScreenMainActivity1234" />
</RelativeLayout>
- Then open Main.Activity.cs page
- Add the below code in the main activity page
[Activity(ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait,
WindowSoftInputMode = SoftInput.StateAlwaysHidden,
Theme = "@style/Theme.DesignDemo")]
public class MainActivity : Activity
{
private FrameLayout mFragmentContainer;
private DoctorAvailabilityFragment mFragment3;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
mFragmentContainer = FindViewById<FrameLayout>(Resource.Id.content_frame);
mFragment3 = new DoctorAvailabilityFragment();
var trans = FragmentManager.BeginTransaction();
//To start a fragment on any of event like button click event
trans.Replace(Resource.Id.content_frame, mFragment3,
"DoctorAvailabilityFragment");
trans.AddToBackStack(mFragment3.Class.SimpleName);
trans.Commit();
}
}
No comments:
Post a Comment