Handle Android Sweet Errors

Rajan Maurya
4 min readMar 22, 2018

Every Android developer always think, How to show error message to user and which one, whenever he/she add the feature in the project.

Actually It somehow easy to build a feature and code looks awesome until we did not covered every failure condition and did not added the error UI code and logic.

I strongly believe, If your code looks sexy then there are some possibility that other developers can understand easily your code and can contribute.

I have developed many android application and I faced this same thing every time to write the logic of failure cases and I made this utils code and converted into library. Actually using this library user don’t need to think over the failure cases logic.

Here you can find library on Github: Sweet Error

Usage

Gradle dependency

  • Add the following to your project level build.gradle:
allprojects {
repositories {
jcenter() or mavenCentral() // whatever you use
}
}
  • Add this to your app build.gradle:
dependencies {
implementation 'com.github.therajanmaurya:Sweet-Error:1.0.0'
}

Usage

  • Add the Sweet-Error xml layout in the parent view of your project xml layout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/cl_sweet"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">

<LinearLayout
android:id="@+id/ll_sweet"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">

.................

</LinearLayout>

<include
layout="@layout/layout_sweet_exception_handler"
android:id="@+id/layout_error"
android:visibility="gone"/>

</android.support.design.widget.CoordinatorLayout>
  • Initialize the SweetUIErrorHandler in your Activity and use it.
public class SweetErrorInActivity extends AppCompatActivity {

private SweetUIErrorHandler sweetUIErrorHandler;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sweet_error);

View layoutError = findViewById(R.id.layout_error);
LinearLayout llSweet = findViewById(R.id.ll_sweet); // It can be any child of your xml like Relativelayout, RecyclerView etc, as we defined in above xml.

sweetUIErrorHandler = new SweetUIErrorHandler(this, findViewById(android.R.id.content));

// Now you are all set. Whatever error UI you want to show according to condition like

# Empty UI (1.1)
// Need to pass the feature(s) name that is empty and icon image of the feature that you want to show and if
// you have feature of addition then pass the feature name and your child view that you want hide and
// layoutError UI to visible. It will handle everything.
sweetUIErrorHandler.showSweetEmptyUI(getString(R.string.sweets),
getString(R.string.sweet), R.drawable.ic_adb_black_24dp,
llSweet, layoutError);


# Empty UI (1.2)
// Need to pass the feature(s) name that is empty and icon image of the feature that you want to show
// and your child view that you want hide and layoutError UI to visible. It will handle everything.
// See 2nd screenshot above as an example
sweetUIErrorHandler.showSweetEmptyUI(getString(R.string.sweets), R.drawable.ic_adb_black_24dp,
llSweet, layoutError);


# Error UI (1.1)
// Need to pass the feature name in which error occured and
// your child view that you want hide and layoutError UI to visible. It will handle everything.
sweetUIErrorHandler.showSweetErrorUI(getString(R.string.sweets), llSweet, layoutError);


# Error UI (1.2)
// Need to pass the feature name in which error occured and image that you want to show like
// bad internet connection image etc.
// your child view that you want hide and layoutError UI to visible. It will handle everything.
sweetUIErrorHandler.showSweetErrorUI(getString(R.string.sweets), R.drawable.ic_no_network llSweet, layoutError);


// Use this button click to refresh UI if any error occured or any Network issue occured.
Button btnTryAgain = findViewById(R.id.btn_try_again);


# No Network I UI (1.1)
// Need to pass your child view that you want hide and layoutError UI to visible. It will handle everything.
// It will look like above 4th screenshot.
sweetUIErrorHandler.showSweetNoInternetUI(llSweet, layoutError);


# Custom Error UI (1.1)
// Need to pass Image that will show up at the top of title and
// the title text that will show bottom to the image and subtext that will show in bottom of title text.
// and your child view that you want hide and layoutError UI to visible. It will handle everything.
sweetUIErrorHandler.showSweetCustomErrorUI(getString(R.string.no_sweets_found),
getString(R.string.come_later_again), R.drawable.ic_adb_black_24dp,
llSweet, layoutError);


# Custom Error UI (1.2)
// Need to pass Image that will show up at the top of title and subtitle of error message.
// the title text that will show bottom to the image and there will be no subtext, only image and error text.
// and your child view that you want hide and layoutError UI to visible. It will handle everything.
sweetUIErrorHandler.showSweetCustomErrorUI(getString(R.string.no_sweets_found), R.drawable.ic_adb_black_24dp,
llSweet, layoutError);
}
}
  • Initialize the SweetUIErrorHandler in your Fragment and use it.
public class SweetErrorInFragment extends Fragment {

private View rootView;
private SweetUIErrorHandler sweetUIErrorHandler;

public SweetErrorInFragment() {

}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_sweet, container, false);

View layoutError = rootView.findViewById(R.id.layout_error);
LinearLayout llSweet = rootView.findViewById(R.id.ll_sweet); // It can be any child of your xml like Relativelayout, RecyclerView etc, as we defined in above xml.

sweetUIErrorHandler = new SweetUIErrorHandler(getActivity(), rootView);

// Now you are all set. Whatever error UI you want to show according to condition like
// you can use rest of things as we are using in above activity.

// Use this button click to refresh UI if any error occured or any Network issue occured.
Button btnTryAgain = rootView.findViewById(R.id.btn_try_again);

return rootView;
}
}

Now you are good to go and think about your feature logic only.

If you want to contribute and want to add your Sweet Error UIs

Make your contribution here: https://github.com/therajanmaurya/Sweet-Error

Thanks for reading. If you have question regarding the library feel free to open an issue and ask me anything.

--

--

Rajan Maurya

Senior Software Engineer at OpenLane Inc, Open source contributor at Mifos Initiative and mentoring GSoC Students. https://github.com/therajanmaurya