Android Archives - wiki

Android keep orientation portrait

Option 1

In the AndroidManifest.xml add ‘screenOrientation’ property in activity tag

Option 2

In the onCreate function add the following line of code before setContentView() function


By bm on November 3, 2016 | Android | A comment?
Tags: ,

Android Retrofit 2.1 HTTP client

Android Retrofit 2.1  HTTP client :

In this example i have divided in to 5 steps :

1. Add gradle dependency

2. Create models for accessing the final data (in this example, the api response json converting to java object this object class is called model )

3. Create interface to define api end point

4. Create a service class for interacting our java code with retrofit.

5. Make the api calls wherever you required

1. Add Gradle dependency (ref: )

2. Create models

3. Create interface

expected api response



4. Create a service class

5. Make the api calls

Retrofit supports synchronous and asynchronous request execution.

 Asynchronous Requests

GET calls

GET call with query string/param OR GET call with path string/param

Post call

POST call with body param

POST call with form encoding


Synchronous request

You can also call the apis synchronously.

You cannot run this code in main tread you will get exception like ( java.lang.RuntimeException:Unable to start activity ComponentInfo{  …. } android.os.NetworkOnMainThreadException). So better to use synchronous request in services or asyncTask.




How to create AsyncTask :

How to create Service :

Ref :

By bm on September 30, 2016 | Android | A comment?
Tags: , ,

Android full screen Activity


You need to use NoactionBar theme

Activity class (

By bm on September 29, 2016 | Android | A comment?
Tags: ,

Android GreenDAO Example

There are two step required for implementing Green Dao ORM library in to android project

1. Create generator java module
2. Implement data base interaction in Activity

Step 1. Create generator java module

The generator module contains a single class containing the data model definition. This will automatically generate model class and Dao calss for the database interaction

How to create a generator module?

In android studio got File->New-> New Module select Java Library 


Give library name and class name


After adding the library the folder structure will be like this


add GreenDao dependency in the created module, and sync

for Green dao 3.x please use the plugin


Add the following code in file. Here we defining a users table scema

for Green dao 3.x:- just change the imports

Run file


It will Create a db folder inside out main app package



2. Implement data base interaction in Activity

Add Green Dao gradle dependency in our main project and sync

for Green dao 3.x:-


MainActivity .java


Out Put




While running you app you need to set app from tine toolbar and run




By bm on September 28, 2016 | Android | A comment?
Tags: , ,

Android RecyclerView Example

Android RecyclerView example using view hodelr in adapter

(For using multiple layout in RecyclerView check this tutorial )

Add ‘‘ in Gradle dependencies and sync it

my app/build.gradle file






Recycler View row

Out Put



If you want to add data to RecyclerView  add data into the Array List (eg:- usersList.add(new UserModel(“new name”, “345”)); and you can call the adapter function notifyItemInserted() or notifyDatasetChanged().

While editing edit the value in array list and call notifyItemChanged(position), while deleting delete the position from array list and call notifyItemRemoved(position)


By bm on August 30, 2016 | Android | 1 comment
Tags: , ,

String Builder and String Buffer


String objects are immutable ( once created can not be changed ) and it will stored in the  Constant String Pool , so if you chose to do a lot of manipulations with String objects, you will end up with a lot of abandoned String objects in the String pool. To avoiding this memory loss we can use StringBuilder or StringBuffer and can be modified over and over again without causing memory loss

StringBuilder example

StringBuffers are thread-safe, they have synchronized methods to control access so that only one thread can access a StringBuffer object’s synchronized code at a time. Thus, StringBuffer objects are generally safe to use in a multi-threaded environment where multiple threads may be trying to access the same StringBuffer object at the same time.


StringBuffer example

StringBuilder’s access is not synchronized so that it is not thread-safe. The performance of StringBuilder can be better than StringBuffer. Thus, if you are working in a single-threaded environment, using StringBuilder instead of StringBuffer may result in increased performance.




By bm on July 26, 2016 | Android, Java | A comment?
Tags: , , ,