Android

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

 

  Like
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: http://square.github.io/retrofit/ )

2. Create models 

User.java

3. Create interface

ApiInterface.java

expected api response

get/users

get/user

4. Create a service class

ApiService.java

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.

networkmainthread

 

 

How to create AsyncTask : http://wiki.workassis.com/android-asynctask

How to create Service : http://wiki.workassis.com/android-intentservice-example

Ref : http://square.github.io/retrofit/

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

Android full screen Activity

Androidmanifest.xml

You need to use NoactionBar theme

Activity class (SplashActivity.java)

  Like
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 

javalib

Give library name and class name

javalib2

After adding the library the folder structure will be like this

befouremoduleexe

add GreenDao dependency in the created module, and sync

for Green dao 3.x please use the plugin

generatergradile

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

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

Run GeneratorClass.java file

rungenerator

It will Create a db folder inside out main app package

aftermoduleexe

 

2. Implement data base interaction in Activity

Add Green Dao gradle dependency in our main project and sync

for Green dao 3.x:-

appgradle

MainActivity .java

activity_main.xml

Out Put

greendaooutput

 

Note:-

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

note

 

Ref:

http://greenrobot.org/greendao/

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

Android using multiple layout in RecyclerView

In this tutorial I am explaining how to implement multiple layout in a single RecyclerView

Add ‘com.android.support:recyclerview‘ in Gradle dependencies and sync it (Check this tutorial for Recycler View example http://wiki.workassis.com/android-recyclerview-example/)

MainActivity

MyMediatorInterface

MyAdapter

GroupTitleModel

UserModel

ItemInterface

Layouts
activity_main.xml

row_item_group_title.xml

row_user_item.xml

Output

recyclerview-multiple-layout

 

  Like
By bm on August 31, 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 ‘com.android.support:recyclerview‘ in Gradle dependencies and sync it

my app/build.gradle file

MainActivity

Model

Adapter

Interface:

Layouts

Recycler View row

Out Put

recyclerview

 

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)

 

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