You’ll use Entry List Screenlet by following the same steps to use any
Screenlet: insert the Screenlet’s XML in an activity or fragment layout, and
then implement the Screenlet’s listener in the activity or fragment class.
You’ll follow these steps here to insert Entry List Screenlet in
EntriesFragment
.
First, you’ll insert Entry List Screenlet’s XML in EntriesFragment
’s layout,
fragment_entries.xml
.
Inserting the Screenlet in the Layout
Inserting Entry List Screenlet’s XML is very simple. Since all you want
fragment_entries.xml
to do is display the Screenlet, it must only contain
the Screenlet’s XML. Replace the contents of fragment_entries.xml
with the
following markup:
<com.liferay.docs.entrylistscreenlet.EntryListScreenlet
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/entrylist_screenlet"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutId="@layout/list_entries"/>
As with Guestbook List Screenlet, you’ll use the android:id
value to get a
Screenlet reference. Next, you must implement the Screenlet’s listener.
Implementing the Screenlet’s Listener
Recall that to use a Screenlet, you must implement its listener. The listener
methods let the app developer respond to the Screenlet’s behavior in the
activity or fragment class that contains the Screenlet. Also recall that because
Guestbook List Screenlet didn’t need extra listener methods, you used it in
GuestbooksActivity
by implementing the
BaseListListener
interface
with GuestbookModel
as a type argument. Entry List Screenlet also doesn’t need
extra listener methods. Like Guestbook List Screenlet, you can use it by
implementing BaseListListener
with its model class as a type argument.
Follow these steps to implement Entry List Screenlet’s listener in
EntriesFragment
:
-
Change
EntriesFragment
’s class declaration to implementBaseListListener<EntryModel>
. The class declaration should now look like this:public class EntriesFragment extends Fragment implements BaseListListener<EntryModel> {...
This requires that you add the following imports:
import com.liferay.docs.model.EntryModel; import com.liferay.mobile.screens.base.list.BaseListListener;
-
Now you must implement the listener’s methods. Recall that this includes the
BaseCacheListener
interface’s,error
method, sinceBaseListListener
extendsBaseCacheListener
. For a full explanation of the methods in both listeners, see using Guestbook List Screenlet. Note that inEntriesFragment
, you don’t need to take any action in these methods. There are no UI elements or other parts of the fragment that must be updated or processed in response to the Screenlet’s behavior. All this Screenlet must do is display its content, which it does regardless of anything you do in the listener methods. The only thing you may want to add is a toast message inonListPageFailed
to notify the user if the server call fails, but this isn’t required. Implement these methods now:@Override public void onListPageFailed(int startRow, Exception e) { Toast.makeText(getActivity(), "Page request failed", Toast.LENGTH_LONG).show(); } @Override public void onListPageReceived(int startRow, int endRow, List<EntryModel> entries, int rowCount) { } @Override public void onListItemSelected(EntryModel entry, View view) { } @Override public void error(Exception e, String userAction) { }
This requires you to add the following imports:
import android.widget.Toast; import java.util.List;
-
Now you’re ready to register
EntriesFragment
as the Screenlet’s listener. You’ll do this the same way you registeredGuestbooksActivity
as Guestbook List Screenlet’s listener: get a reference to the Screenlet and call itssetListener
method. After doing this, you’ll use the Entry List Screenlet reference’ssetGuestbookId
method to set its guestbook ID. This sets the guestbook where the Screenlet gets its entries. You’ll do these things in theonCreateView
method. Replace theonCreateView
method with this updated version:@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_entries, container, false); long guestbookId = getArguments().getLong("guestbookId"); EntryListScreenlet screenlet = (EntryListScreenlet) view.findViewById(R.id.entrylist_screenlet); screenlet.setListener(this); screenlet.setGuestbookId(guestbookId); return view; }
This requires you to import
com.liferay.docs.entrylistscreenlet.EntryListScreenlet
.As you can see,
onCreateView
now registersEntriesFragment
as the Screenlet’s listener and sets the Screenlet’s guestbook ID. The rest ofonCreateView
is unchanged.
Now run the app in the emulator and log in with your credentials when prompted. The app then presents you with the first guestbook’s entries. Open the navigation drawer by pressing the hamburger button, then select a different guestbook. The drawer then closes to show the selected guestbook’s entries. Nice work! Your app now uses Guestbook List Screenlet and Entry List Screenlet to show the same guestbooks and entries as the Guestbook portlet. The following screenshots show these Screenlets in action.
Although your Screenlets work, you may have noticed something odd about the navigation drawer’s header–it’s hideous. The action bar is somewhere on the purple-blue spectrum, while the drawer header is green. You’ve probably seen more attractive finger paintings. Fortunately, it’s simple to change the drawer header’s color. Also, the drawer header contains the generic text Android Studio. You should change this to something more suitable for your app, like Liferay Guestbook.
Follow these steps to apply these changes to the drawer header:
-
In
res/drawable/side_nav_bar.xml
, replaceandroid:centerColor
,android:endColor
, andandroid:startColor
with the following settings:android:centerColor="@color/colorPrimary" android:endColor="@color/colorPrimaryDark" android:startColor="@color/colorPrimary"
This sets the drawer header’s colors to match the colors used in the rest of the app.
-
Define the following string resource in
res/values/strings.xml
:<string name="liferay_guestbook">Liferay Guestbook</string>
-
In
nav_header_guestbooks.xml
, find theTextView
element that containsandroid:text="Android Studio"
, and replaceAndroid Studio
with@string/liferay_guestbook
. You can delete any otherTextView
elements in this file. Run the app again, and open the drawer after signing in. The drawer header now shows your greeting. It’s a lot prettier too.
Congratulations! Now you know how to use Liferay Screens and create your own Screenlets. This opens up a world of possibilities for developing apps that leverage Liferay Portal. Although you learned a great deal in this Learning Path, there’s still more. You can customize your Screenlet’s appearance, package it for redistribution, and even configure it to receive push notifications. These topics, and more, are covered in the tutorials on Android apps with Liferay Screens.