This is not the preferred method, it is betting to do it in java so that things happen dynamically at runtime, be here is a demo of doing it in XML.
The disadvantage of using fragments this way is that they are fixed within the XML code. This means that we can allow the user on runtime to control them. Sometimes we may want a fragment to change when the user clicks a button for example and using fragments this way won’t allow us to do that. For that, we need to use the Fragment Manager
To make a fragment we need to:
When we use the
Demonstration
MainActivity.java
package com.jamesfroggatt.fragments2.app; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
MyFragment.java
package com.jamesfroggatt.fragments2.app; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //return super.onCreateView(inflater, container, savedInstanceState); // we need to link the layout xml to the fragment class // The LayoutInflater object, inflater, is a special object that converts xml // view descriptions in to java objects return inflater.inflate(R.layout.my_fragment_layout, container,false); } }
activity_main.xml
This is what activity_main.xml looks like in Android Studio. The software is showing the activity_main.xml and it’s fragment in the simulator
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffff22d5"> <fragment android:layout_width="wrap_content" android:layout_height="wrap_content" android:name="com.jamesfroggatt.fragments2.app.MyFragment" android:id="@+id/fragment" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" tools:layout="@layout/my_fragment_layout" /> </RelativeLayout>
my_fragment_layout.xml
This is what my_fragment_layout.xml looks like in Android Studio layout simulator
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#999944" android:padding="20dp"> <TextView android:layout_margin="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:text="This is from my_fragment_layout.xml" android:id="@+id/textView" android:layout_gravity="center_horizontal" /> </LinearLayout>
When running on the emulator this looks like:
in portrait mode, and
in landscape mode.