Home

Saturday, 3 September 2016

EventBus for Android

Now if you have been developing Android apps the most common logical problems you face are how to share data objects among different Activities or maybe more complicated situations like sharing between Activity and Fragment or maybe even more complex where there is no Context of any Activity or Fragment.
Though by now you must be thinking of Context passing or static methods, but then let’s be honest they are really painfull to handle. So what’s a better alternative?

Aha! The EventBus!

Before we start learning it, here is the gradle dependency:
compile 'org.greenrobot:eventbus:3.0.0'
EventBus is an android library by GreenRobot. Let’s understand how it will really help. There are basically three things you should know to use it.

The POST call

The POST method fires an Event with the required object data that you want to share where ever you need. Let’s see the code for it:
EventBus.getDefault().post(new CustomEvent(eventItem));
What? Just one line and all the worries are gone? YES!
So can I call this from anywhere? Yes, as you can notice getDefault() is a static method and the call does not need any other parameter other than a CustomEvent class and a eventItem(we’ll learn in the next step about them). So no more Contexts or object passing. Yay!yThe CustomEvent.class

The CustomEvent.class

So as we had a look at how to call POST method, we came across CustomEvent. This is a custom class in which we pass our required objects and it creates it’s own object packing all the objects into one to pass further. Let’s create one!
public class CustomEvent { 
    
    public CustomItem eventItem;
    public CustomEvent(CustomItem eventItem) {      
    this.eventItem = eventItem;   
    }
}
So we made a new class CustomEvent with CustomItem object and constructor to pass the values.
Note: You can create as many objects in the class as per the requirement.

The @Subscribe

So till now we created a POST method to pass the values and CustomEventclass to contain all our object values, but now we need to retreive the passed values when the POST call is fired. Let’s see the code how to do it:
@Subscribe   public void OnCustomEvent(CustomEvent event) {  
CustomItem eventItem = event.eventItem; 
}
And we have our CustomEvent class object and we can get values easily.
The @Subscribe annotation here specifies that the below function will execute taking single parameter of type CustomEvent. Where ever this is placed along with the CustomEvent type parameter, this will run as soon as the POST method is fired.
Note: Your parameter must have same parameter type as fired with POST.
Now we also need to register EventBus to make the current activity or fragment able to recieve the event message.
@Override    
public void onStart() {        
    super.onStart();    
    if (!EventBus.getDefault().isRegistered(this))
         EventBus.getDefault().register(this);    
}     
@Override    
public void onDestroy() {        
    super.onDestroy();
    EventBus.getDefault().unregister(this);    
}
Now the applications of this is limitless and it sure does give the developer ease and edge of handling data sharing and focusing on other aspects of the application.

Repository

You can see a running example with a complex situation solved using EventBus.
https://github.com/code-crusher/android-demos/tree/master/EventBusDemo

Conclusion

This is just a small part of some EventBus concepts. If you want to learn more about this, checkout the EventBus repo:
https://github.com/greenrobot/EventBus
Hope this was usefull and it makes your application better. See you soon in the upcoming stories :)

Thursday, 4 August 2016

Android ‘Enums’: Good or Bad ?

While making Android apps there is a time when every developer wonders the efficiency of ‘Java Enums’ in their Android application. Well before getting into it’s usability let’s see how Enums(short for ‘Enumeration’)works:
As written by Oracle, ‘An enum type is a special data type that enables for a variable to be a set of predefined constants.’ What this means is, the variable is assigned a constant value within a set of values within that Enum type. Let’s see an example:


public enum Snacks {
  NACHOS,
  FRIES,
  COOKIES;
}
So now each item is a part of the enum set Snacks with a static constantvalue but how to be really sure? Let’s do some ‘hacky’ stuff!
Compiling the above enum Snacks:


javac Snacks.java
and disassembling resulting class file with javap:


javap Snacks.class
gives the following:


public final class Snacks extends java.lang.Enum<Snacks>{
    public static final Snacks NACHOS;
    public static final Snacks FRIES;
    public static final Snacks COOKIES;
    public static Snacks[] values(); 
    public static Snacks valueOf(java.lang.String); 
    static {}; 
}
Hence we see that all the variables are static and the static variables takes memory irrespective if they are used or not and are not collected by Java Garbage Collector unless the respective class is dropped as they have scope across the entire program.
So static variable in your Android app, looks ugly already but statics are not always bad. What? How? To use any general variable we need to create an object of it’s class, but if that object is created multiple times, the Garbage Collector has to collect it again and again, hence it’s better to use Enums. If there is no such condition then you must avoid enums as much as possible.

Sunday, 17 April 2016

Android Relative Layout vs Linear Layout

Relative Layout has been in use a lot due the ease and quick implementation of Android UI layout. But it comes with a great loss. The way the Relative Layout works is "it measures each child twice". What does the mean?
To explain this lets understand via code :


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Sample Text"/>

</RelativeLayout>


Here the TextView would be measured twice while processing your UI during runtime which would take more time to display.
Now if you have a complex view hierarchy this would take up a lot of time just measuring. And it can go worse if Nested Relative Layout is used, the already twice measured child of inner Relative Layout would be again measured twice by outer Relative Layout which becomes 4 times just for one view!
So Linear Layout should preferred over Relative Layout!
Also if you use weight_sum attribute within Linear Layout it would again "measure the child twice". So avoid using this attribute as much as possible.

With such restrictions it becomes challenging sometimes to implement complex layouts but the application performance matters too, so let's be balanced next time we make a layout.

Wednesday, 8 July 2015

Implement Navigation View - Android Support Library

Implement Navigation View : 

To implement navigation view we need to setup few files :

1) drawer_viewer.xml - which will be placed under res >menu. It will contain all the items to be displayed in the drawer. Now we can directly make menu items and add them into drawer we will make.
2) activity_main.xml - which will contain the layout of drawer.
3) main_layout.xml - your main layout(you can add more as per the fragments requirements).

drawer_viewer.xml :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_home"
                    android:icon="@drawable/ic_dashboard"
                    android:title="@string/home" />
                <item
                    android:id="@+id/nav_category1"
                    android:icon="@drawable/ic_event"
                    android:title="@string/category1" />
                <item
                    android:id="@+id/nav_category2"
                    android:icon="@drawable/ic_forum"
                    android:title="@string/category2" />
            </group>


    <item android:title="@string/subItems">
        <menu>
            <item
                android:icon="@android:drawable/radiobutton_off_background"
                android:title="@string/Subitem" />
            <item
                android:icon="@android:drawable/radiobutton_off_background"
                android:title="@string/Subitem" />
            <item
                android:icon="@android:drawable/radiobutton_off_background"
                android:title="@string/Subitem" />
        </menu>
    </item>

</menu>


activity_main.xml :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

        <include layout="@layout/main_layout"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        android:fitsSystemWindows="true"
        app:menu="@menu/drawer_view" />

</android.support.v4.widget.DrawerLayout>

As you can see i have added a headerLayout "nav_header". It is used to display either an image or text above item, maybe you need to create a profile layout and it is very easy to do now.

nav_header.xml :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="192dp"
    android:background="?attr/colorPrimaryDark"
    android:padding="16dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:orientation="vertical"
    android:gravity="bottom">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="AppyWare"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

</LinearLayout>


MainActivity.java :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;

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


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final ActionBar ab = getSupportActionBar();
        ab.setHomeAsUpIndicator(R.drawable.ic_menu);
        ab.setDisplayHomeAsUpEnabled(true);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        if (navigationView != null) {
            setupDrawerContent(navigationView);
        }
    }

//to get selected menuitem
    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        menuItem.setChecked(true);
                        Toast.makeText(getApplicationContext(), "Current selection : " + menuItem.getTitle(), Toast.LENGTH_SHORT).show();
                        mDrawerLayout.closeDrawers();
                        return true;
                    }
                });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Screen Shots :


Complete Source Code : NavigationViewDemo

For any doubts you can comment below, i'll respond ASAP.

Monday, 22 June 2015

Using SnackBar - Material Design

Using SnackBar - Material Design : 

To display a post action notification we used Toasts, but the introduction to material design came SnackBar. To use SnackBar :

In your gradle file(app), within your dependencies add :

dependencies {
    ...
    compile 'com.android.support:design:22.2.0'
}

In your java file, where you want to show notification :

Snackbar
   .make(parentLayout,"This is a SnackBar!",Snackbar.LENGTH_LONG)
   .setAction("Action", null)
   .show();





**You’ll note the use of a View as the first parameter to make() - Snackbar will attempt to find an appropriate parent of the Snackbar’s view to ensure that it is anchored to the bottom.

parentLayout example :

public void snackBar(View view) {

     Snackbar.make(view, "This is SnackBar!"Snackbar.LENGTH_LONG)
           .setAction("Action", null)
           .show();
    }

ScreenShot :





Complete Source Code : SnackBarDemo

For any doubts you can comment below, i'll respond ASAP.

Using CardView - Material design

Using CardView - Material design : 

First of all to use CardView add this in your gradle file(app) within dependencies add :

dependencies {
    ...
    compile 'com.android.support:cardview-v7:22.0.+'

}


To use CardView add this in your layout(xml) file :

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground"
        card_view:cardBackgroundColor="#ffff4966">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:text="This is a Card View"
            android:textSize="25dp" />

    </android.support.v7.widget.CardView>



Complete Source Code : CardVewDemo

For any doubts you can comment below, i'll respond ASAP.

Monday, 18 May 2015

Creating Advance Custom Button

Creating Advance Custom Button :

For a better User Experience we need to show button responsive, for that we will change the button when the user presses it. To do that we need to create:

• button_bg_normal.xml  - For normal button.
• button_bg_pressed.xml - When the button is pressed.
• button_selector.xml - To switch between the backgrounds.


button_bg_normal.xml :

1
2
3
4
5
6
7
8
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/holo_blue_dark" />
    <corners android:radius="25dp" />
    <stroke
        android:color="@android:color/holo_blue_bright"
        android:width="2dp" />
</shape>


button_bg_pressed.xml :

1
2
3
4
5
6
7
8
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/holo_red_dark" />
    <corners android:radius="25dp" />
    <stroke
        android:color="@android:color/holo_red_light"
        android:width="2dp" />
</shape>


button_selector.xml :

1
2
3
4
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/button_bg_normal" />
</selector>


activity_main.xml : 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">

    <Button
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a Custom Button"
        android:padding="10dp"
        android:textColor="@android:color/white"
        android:background="@drawable/button_selector"
        android:layout_centerInParent="true"/>

</RelativeLayout>

ScreenShots :