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

 

  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 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 | 1 comment
Tags: , ,

String Builder and String Buffer

String

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.

 

 

ref:
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

https://docs.oracle.com/javase/tutorial/java/data/buffers.html

https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html

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