Our Quickstart guide shows you how to load Situm WYF in an Android Activity. This is a common escenario, but it is also common to load it in a Fragment. Fortunately, Situm WYF provides the means to do it easily by using the method SitumMapsLibrary.loadInFragment, which accepts an androidx.fragment.app.Fragment where SitumWYF will load.
In this example, we will assume that you will load your Fragment from the MainActivity, but you can do it anywhere else. First, you need to define your MainActivity layout in “res/layout/activity_main.xml”. For instance:
<?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" >
<FrameLayout
android:id="@+id/my_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
Then, in our MainActivity.java, you need to start the Fragment (which will be loaded in the view element previously defined in the activity_main.xml):
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.my_placeholder, new MyFragment());
ft.commit();
}
}
Your Fragment will be composed of two elements. The first one will be the layout, created for example in “res/layout/my_fragment.xml”. It can be as simple as:
<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"
tools:context="com.example.basicwyfapp.MyFragment">
<FrameLayout
android:id="@+id/maps_library_target"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
The second one will be the Fragment itself, that will load SitumWYF as usual, but using the loadInFragment method (instead of the regular load method). You will also be able to configure the static & dynamic UI settings if you want.
public class MyFragment extends Fragment {
SitumMapsLibrary mapsLibrary = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
//Create the view using "my_fragment.xml"
View view = inflater.inflate(R.layout.my_fragment, parent, false);
return view;
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
//Create LibrarySettings
LibrarySettings librarySettings = new LibrarySettings();
librarySettings.setApiKey("EMAIL", "APIKEY");
//Configure dynamic UI settings (if you want)
librarySettings.setMinZoom(15);
...
//Prepares SitumMapsLibrary to load itself on "maps_library_target" view element
//Note that the second parameter is "this.getActivity()", since it's an androidx.fragment.app.FragmentActivity
//When called from an Activity, we would pass "this" as a second parameter instead
mapsLibrary = new SitumMapsLibrary(R.id.maps_library_target, this.getActivity(), librarySettings);
//Setup listener to configure dynamic UI settings (optional)
mapsLibrary.setSitumMapsListener(mapsListener);
//Load WYF in Fragment!
mapsLibrary.loadInFragment(this);
}
private SitumMapsListener mapsListener = new SitumMapsListener() {
@Override
public void onSuccess() {
//Configure dynamic UI settings after SitumWYF view loads correctly
mapsLibrary.centerBuilding("12485", null);
mapsLibrary.setFloorsListVisible(true);
mapsLibrary.setInfoViewVisible(false);
...
}
@Override
public void onError(int i) {
//...
}
};
}
And voila! SitumWYF loads in a Fragment now!
