Presenter Layout


Source link: https://github.com/CROSP/presenter-layout

Presenter Layout

This library helps to display overlapping views (topmost) in order to show some kind of message to capture user attention.
It can be an error, success message, loading bar or just simple text message.

Mostly every app has some actions that are performed in background like data loading, I/O operations, database querying and to notify user shows loading, but error may occurr or suddenly internet connection may lost, so user
have to be notified about this.
Moreover prevent app crashes because of user clicking everywhere while data is loading.

Presenter name comes from Android Clean Architecture There is a presenter layer which responsible for updating and controlling UI and commonly has methods like showLoading, showErrorView, showSuccessView.

So you are implementing such logic in every single Activity or Fragment. This is done through creating extra layouts and views like ProgressBar in XML markup file or creating separate XML file and than using <include> tag.

To sum up, the idea of this library is to have one Layout that wraps all child views and layouts like any other ViewGroup, but can show overlapping view on demand.

I would say this is more an example rather than library.

This library provides following features :

  • Several predefined overlappinge views ErrorView, NoConnectionView, LoadingView and SuccessView
  • Custom ViewGroup for controlling child and overlapping views
  • Posibility to create custom overlapping views
  • Support starting from Android 2.1 ( API 7)

Screenshots

Download and Installation

Use Maven:

<dependency>
<groupId>ua.com.crosp.solutions.library</groupId>
<artifactId>presenter-layout</artifactId>
<version>0.1.1</version> </dependency>

or Gradle:

compile 'ua.com.crosp.solutions.library:presenter-layout:0.1.1'

How does it work ?

PresenterLayout is custom compound FrameLayout that has two FrameLayouts in its XML layout.

<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="match_parent"

  android:layout_height="match_parent"

  android:orientation="vertical">
  <!--All child views are added to this layout in order to make overlapping views be 

  always over these views -->
  <FrameLayout

android:id="@+id/frame_layout_child_container"

android:layout_width="match_parent"

android:layout_height="match_parent"/>
  <!--Container where all views like progress, error, success ... are stored-->
  <FrameLayout

android:id="@+id/frame_layout_overlapping_views_container"

android:layout_width="match_parent"

android:layout_height="match_parent"/> </merge>

First FrameLayout is children container, all children from XML or added via addView() method go here.
Second layout is used to keep all overlapping views, so they always is on the top of children

Usage

First of all you are indentifying each view by ID, so you need to have unique ID when you are adding new overlapping view.

To add overlapping view there three methods

public void addOverlappingView(View view, boolean hide);
 public void addOverlappingView(View view);
 public void addOverlappingView(View view, int index, ViewGroup.LayoutParams params);
 public void addOverlappingView(View view, int width, int height);

As you may notice they have the same signature as default addView methods.

To remove overlapping view

public void removeOverlappingView(View overlappingView);
 public void removeOverlappingView(int viewId);

To show added view

public boolean showOverlappingView(int viewId, boolean hideOthers);
 public boolean showOverlappingView(int viewId);

Again you have to pass unique ID of view, of course it should be set before using setId() method

Every app has it is own design and specific UI so it is very hard to create View that fits everywhere. But just to show the idea I created four custom default overlapping views.

You can find all details in source code, it is simple.

There are four default overlapping view provided out of the box : ErrorView, SuccessView, NoConnectionView and LoadingView.

PresenterLayout class provide delegation methods for these views.

ErrorView

public void showErrorMessage(String errorMessage);
 public void showErrorMessage();
 public void hideErrorMessage();
 public SwipeRefreshLayout getErrorRefreshLayout();
 public void setErrorMessage(String errorMessage);
 public void setErrorIcon(String errorIcon);
 public void setErrorRefreshListener(SwipeRefreshLayout.OnRefreshListener onRefreshListener);
 public void setErrorBackgroundColor(int backgroundColor);

LoadingView

public void showLoading(String loadingMessage);
 public void showLoading();
 public void hideLoading();
 public void setLoadingText(String loadingText);
 public void setLoadingIcon(String loadingIcon);
 public void setLoadingBackground(int color);

SuccessView

public void showSuccess();
 public void hideSuccess();
 public void hideErrorMessage();
 public void showSuccess(String successText);
 public void setSuccessIcon(String successIcon);
 public void setSuccessButtonText(String buttonText);
 public void setSuccessBackgroundColor(int backgroundColor);
 public void setSuccessText(String successText);
 public void setSuccessButtonClickListener(OnClickListener successButtonClickListener);

NoConnectionView

public void showNoConnection();
 public void hideNoConnection();
 public void showNoConnection(String message);
 public void setNoConnectionText(String noConnectionText);
 public void setNoConnectionIcon(String noConnectionIcon);
 public void setNoConnectionBackgroundColor(int backgroundColor);
 public void setNoConnectionRefreshListener(SwipeRefreshLayout.OnRefreshListener refreshListener);
 public void SwipeRefreshLayout getNoConnectionRefreshLayout();

Moreover layout provides getters for these views, to my mind it is bad idea to expose view directly, but maybe someone wants to customize
default view in runtime.

public SuccessView getSuccessView();
 public LoadingView getLoadingView();
 public NoConnectionView getNoConnectionView();
 public ErrorView getErrorView();

Also you can configure PresenterLayout in XML

Here is an example

<ua.com.crosp.solutions.library.presenterlayout.layout.PresenterLayout
  android:id="@+id/presenter_layout_main"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:custom="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  custom:inflate_default_views="true"
  custom:show_initial_view="no_connection">

Example

You can find example of app on Google Play Store
And of course in source code - module app

Contribution

I would be grateful for any advice on improving library, especially default views.
Thanks.

#Credits

License

Copyright 2016 Oleksandr Molochko  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

This library will provide you to have Parallax effect on every item of your ListView.

APK parser for Android.

Features:

  • Retrieve basic apk metas, such as title, icon, package name, version, etc.
  • Parse and convert binary xml file to text
  • Classes from dex file
  • Get certificate metas and verify apk signature

Android widget that can render PDF documents stored on SD card, linked as assets, or downloaded from a remote URL.

Tab Layout for ViewPager just like Weixin.

This lib produces some sugar and type safety for Android Shared Preferences.

Reactive layer (similar to the pattern repository) for the Realm. Main idea is using reactive layer for access to the Realm. If you make a request to the layer always return Observable. The layer will remember the entity, the predicate and returns Observable in the future if the entity will change the subject learns about it.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes