Let's continue from our current activity!
The main difference in creating a fragment is that you have to manage the Fragment using FragmentManagerarrow-up-right and FragmentTransactionarrow-up-right statements that can add, remove, and replace a Fragment
Creating a Fragment Class
Just as you did just now, create a fragment called RatingFragment .
Set the Layout of the Class
This time, let's use a RatingBar instead of a RadioGroup with RadioButtons
Copy <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/fragment_color"
tools:context=".RatingFragment">
<TextView
android:id="@+id/carText"
android:layout_marginLeft="@dimen/padding_standard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:padding="4dp"
android:text="@string/rating_message" />
<RatingBar
android:id="@+id/ratingBar"
style="@android:style/Widget.DeviceDefault.RatingBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="6"
android:padding="8dp"
android:rating="0"
android:stepSize="0.5"
android:isIndicator="false"
/>
</LinearLayout> Now just as we did before, we can edit and place interactivity in our fragment right away.
In this new fragment, we have a new function newInstance(). This allows us to instantiate the Fragment in the MainActivity later on.
Great! Now all we need is to edit the rest on the MainActivity and activity_main.xml
Creating functions to display/hide Fragments
We are going to use a <FrameLayout> as a fragment container instead of a <fragment> in activity_main.xml
First, let's specify ViewGrouparrow-up-right for Fragment in layout
Add the following code within the MainActivityclass.
What the code is doing here is instantiating RatingFragmentand FragmentManager
Use getSupportFragmentManager() for compatibility
Use FragmentTransactionarrow-up-right for adding/removing/replacing dynamically.
FragmentManager:
FragmentManager interacts with Fragment which are within an Activity.
start fragment transaction and returns FragmentTransaction
by passing id, it returns fragment instance.
FragmentTransaction
FragmentTransactionperforms fragment operations. Find some methods.
add(int containerViewId, Fragment fragment)
adds a given fragment to activity state.
attach(Fragment fragment)
The detached fragment can be re-attached by using this method.
detach(Fragment fragment)
Detaches the given fragment from UI. Fragment state is still managed by FragmentManager.
remove(Fragment fragment)
Removes the given fragment from UI and container.
replace(int containerViewId, Fragment fragment)
For the given container view id, we can replace existing fragment by new given fragment.
Transaction is committed.
Fragment operations are wrapped into a transaction:
Do all Fragment operations (add, remove, etc.)
addingtoBackStack(null) saves transaction to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Back button.
Alright, now we just need to add a Button and set up its OnClickListener() to our functions
First, place the button in the activity_main.xml layout
Now, find its view in the MainActivity and setOnClickListener()
And you should be able to build and run your app!