Learn how to create different versions of your App like Free and Paid

Android framework makes it quite easy for you to generate different flavors and variants of your App. Like you can create a free version or a paid version of your App.

In order to do this you need to follow these steps:

You need to specify the variants or flavors in your App’s build.gradle file.

Like

productFlavors {
    demo {
        applicationId "com.edocent.demo"
        versionName "1.0-demo"
    }
    full {
        applicationId "com.edocent.full"
        versionName "1.0-full"
    }
}

In all likelihood your Free App version will need a different Activity compared to the Paid one. So you need to create different folders for the two flavors.

So go to the Project->src folder and create a new Java Folder – free.

Create another one for paid similarly.

Add appropriate Activity and other files to this folder.

And you are all set. Execute Generate Signed APK task from the Build option. This will ask you which flavor you need the APK for.

Cool isn’t it.

Source code is available here

Learn how to generate a signed APK file

All Android Apps must be digitally signed before they are installed on a device.

This is required to verify identity of developer who published it. And also to verify if it has been tampered.

Follow the video to generate the signed APK file and also to learn how to automate the process.

 

Learn how to create and use a Android Module in your Android Project

You saw why using modules is important and how to create a Java module in an earlier Post. In a similar fashion you can add an Android module to your project.

Android Studio makes it easy to add a new module. Follow the video to understand the steps.

Source code is available here

What is Android Debug Bridge or ADB ?

An ADB is a command line tool which can be used to communicate with Emulator or Android Devices.

When your development machine needs to communicate with an Android Device it does so using ADB. It’s a process that is controlled by a command also known as adb.

The adb command works by talking to an adb server which runs in the background at port 5037. The server is also known as adb daemon or adbd.

Android Studio also talks to this server when it needs to run an app via an Android Device.

To work with ADB you need Android SDK.

Learn how to create and use a Java Module in Android Project

Creating modules is a good practice since it aids in reusability. In your Android project you can easily add a Java or Android module.

Follow the video to create a Java module and add it as a dependency in your Android Project.

Project source code is available in GitHub

Android Basics – Learn how the APK file Works in Android

Once you have transferred an APK file to your Android device its stored in /data/app/<package-name>

The classes.dex file is extracted from it and it’s converted to native library, when the App is run the first time, and stored in /data/dalvik-cache. This Machine Code can be run by the CPU.

Each Android device runs a process called Zygote. When an App needs to run Zygote creates a forked version of itself. Which means its a process in memory. Using this forked process and by loading the native library the App can be loaded pretty quickly.

Android Basics – Understanding Android Run Time, Dalvik and APK

If you are familiar with the Java Virtual Machine(JVM) then you will know that a JVM works on class files which are comprised of byte code. A class file is a compiled version of the Java code.

When it comes to Android then the Java code is first compiled to a class file which in turn is converted to a file called classes.dex – Dalvik Executable format. This is done by a tool called dx

Like JVM in Android’s case its Dalvik which works on the dex file.

A JVM is stack based processor whereas Dalvik is register based. Both are virtual processors though.

The classes.dex file is then compressed with a bunch of other files, like resource and data files, into a ZIP file called an Application Package or APK

This is the file you will eventually upload to the Google Play Store.

Android Basics – Learn how to handle the user clicks in a RecyclerView

You are familiar with how to respond to user clicks in a ListView using an OnItemClickListener. RecyclerView does not have a similar set of built in functionality so you will have to write some code to handle user clicks.

The code you will add will be in the Adapter’s onBindViewHolder. You will also add an interface, similar to the ay you did for Fragment interaction.

Follow the steps below:

Add a Listener interface to the Adapter.

public static interface Listener{
    public void onClick(int position);
}

Add a setListener method. This method will be called from the Activity.

public void setListener(Listener listener) {
    mListener = listener;
}

Add setOnClickListener

cardView.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View view){
        if(mListener != null){
            mListener.onClick(position);
        }
    }
});

In Activity class set the Listener defined in Adapter.

SampleAdapter adapter = new SampleAdapter(inputData);
adapter.setListener(new SampleAdapter.Listener() {
    @Override
    public void onClick(int position) {
        //Add code here
    }
});

Source code is available here

Android Basics – Using Layout Manager with Recycler View to arrange Views

The advantage of using a RecyclerView over ListView is that you get the option to arrange how the views display.

You have the option to display views in a linear list, a grid or a staggered grid.

To use the LayoutManager you need to add the following code

LinearLayoutManager manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);

Source code is available here

Android Basics – Learn how to create Recycler View

So far you have created an Adapter and added a View Holder and also code to onBindViewHolder.

Now it’s time to create a Recycler View which will use this Adapter. It will also pass data to this Adapter.

In order to do this you can create a new Activity, for the purpose of understanding. Let’s call this MaterialActivity. You will call this from the MainActivity on click of a button.

In the layout file for the new Activity you need to add the RecyclerView.

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sample_recycler"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:scrollbars="vertical"
    />

In the Activity class you need to set the Adapter.

public class MaterialActivity extends AppCompatActivity {

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

        String[] inputData = {"Hello Sam", "Hello John", "Hello Riki"};

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.sample_recycler);

        SampleAdapter adapter = new SampleAdapter(inputData);
        recyclerView.setAdapter(adapter);
    }

}

Source code is available here.

In the next Article we will take this further.