EventBus Library In Android

Building an Android application that has various dynamic segments speaking with one another can get repetitive. To save time, developers often end up with tightly coupled components in their apps like Interfaces or directly calling an object’s function.

EventBus is a popular open-source library that was created to solve this problem using the publisher/subscriber pattern.

Using the EventBus library, you can pass messages from one class to one or more classes in just a few lines of code.

Methods of EventBus :

  • register : To register your component to subscribe to events on the bus.
  • unregister : To stop receiving events of the bus.
  • post : To publish your event on the bus.

Continue Reading


Runtime Permission in Pre-Marshmallow

As we have already discussed about Requesting Runtime Permission in Marshmallow, now what if we want to give suuport for Runtime Permission in Pre-Marshmallow devices.

And the solution is : Support Library

If we want to use checkSelfPermission() we need to use minSdkVersion 23 but what if we want to give support in both Marshmallow and pre-marshmallow devices. There are small changes we need to do with those methods we are using for Marshmallow.

– ContextCompat.checkSelfPermission()

No matter application is run on Marshmallow or not. This function will correctly return PERMISSION_GRANTED if the permission is granted. Otherwise PERMISSION_DENIED will be returned.

– ActivityCompat.requestPermissions()

If this function is called on pre-Marshmallow, OnRequestPermissionsResultCallback will be suddenly called with correct PERMISSION_GRANTED or PERMISSION_DENIED result.

-ActivityCompat.shouldShowRequestPermissionRationale() 

it will always return false, if called on pre-marshmallow.

Here i am sharing demo code, please take a look at that.


addHeaderView in RecyclerView

As we all know there is addHeaderView in ListView to add header item. But there is no equivalent to this in RecyclerView. Here i am sharing short demo for addHeaderView in RecyclerView.

add dependency in build.gradle

add RecyclerView in your main xml

row_layout.xml

Continue Reading


Requesting Runtime Permission – Part 2

We have discussed about Requesting Runtime Permission in last tutorial, but that was for single permission. Today we will discuss about asking for multiple permissions.

Here is some code snippet to take multiple permissions at a time.

Continue Reading


Requesting Runtime Permission – Part 1

In previous post we have discussed about theory part of Android’s Runtime Permission. Today we will discuss about Requesting Runtime Permission through coding.

As you all know it is Marshmallow functionality so start your new project by setting compileSdkVersion and targetSdkVersion to 23 and mention permission in manifest.xml file.

Now we will check whether READ_EXTERNAL_STORAGE permission is Granted or not with help of checkSelfPermission().

Continue Reading