How to solve “Duplicate files copied in APK META-INF/LICENSE.txt”?

Standard

Sometimes depends on libraries that you have used in your Android project this annoying error shows up during gradle build using Android Studio:

Error:com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE.txt

can be solve this issue and many more by adding this to the Android tag of your build.grade file:

packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/LGPL2.1'
}

How to enable Android retrofit2 network library logging?

Standard

As any one who had an experiment with retrofit2 knows, logging is some times required to check what is happening!

Since the retrofit2 required HTTP layer is now completely based on OkHttp, the developers of OkHttp added a logging interceptor in release 2.6.0

add to your build.gradle file:

dependencies {
    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
    compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
    compile 'com.squareup.okhttp3:logging-interceptor:3.0.0-RC1'
    ...
}

and this is how to build retrofit:

Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl(Consts.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create());

        //show retrofit logs if is debugging
        if (sIsDebug) {
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
            httpClient.addInterceptor(logging);

            builder.client(httpClient.build());
        }

        mRetrofit = builder.build();

now have fun with logs at logcat…

How to connect your activity to its fragment

Standard

Often you will want one Fragment to communicate with its Activity also all Fragment-to-Fragment communication must be done through the associated Activity. Two Fragments should never communicate directly.

Define an Interface
To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity.

public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener mCallback;

    // Container Activity must implement this interface
    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }
    
    ...
}

Now the fragment can deliver messages to the activity by calling the onArticleSelected() method (or other methods in the interface) using the mCallback instance of the OnHeadlineSelectedListener interface.

Implement the Interface
In order to receive event callbacks from the fragment, the activity that hosts it must implement the interface defined in the fragment class.

public static class MainActivity extends Activity
        implements HeadlinesFragment.OnHeadlineSelectedListener{
    ...
    
    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment
        // Do something here to display that article
    }
}

 

full version at:
http://developer.android.com/training/basics/fragments/communicating.html

How to manage your Android Application Settings

Standard

Here is my best practice for managing your Android application settings using AppSettings class. by using this class you can save your different variables in shared preferences and also load them to ram when ever needed and use them in memory.

String methods sample:

AppSettings.setValue(this, KEY_NAME, name);
AppSettings.getString(this, KEY_NAME, "");
AppSettings.clearValue(this, KEY_NAME);

Android SQLite create query syntax diagram

Standard

All SQLite create queries are based on the following syntax diagram:

sqlite syntax diagram

Here, the keyword create table is used to create a table. This is followed by the table name, the declaration of columns, and their datatype. After preparing our SQL statement, we will execute it using the execSQL() method of the SQLite database.

In case something is wrong with the query statement that we built earlier, we will encounter the exception, android.database.sqlite.SQLiteException. By default, the database is formed in the internal memory space allocated to the application. The folder can be found at

 /data/data//databases/.

Based on Android SQLite Essentials book:
Android SQLite Essentials

Where can I find the best reference to learn Design Patterns in Java?

Standard

Design Patterns in Java Tutorial Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development. These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time. This tutorial will take you through step by step approach and examples using Java while learning Design Pattern concepts.

Prerequisites Before you start proceeding with this tutorial, I’m making an assumption that you are already aware about basic java programming concepts. If you are not well aware of these concepts then I will suggest to go through our short tutorial on Java Programming.

Reference:
Design Patterns Tutorial in PDF

How to Attach Android Source-Code to eclipse?

Standard

After installing ADT, use SDK Manager to download the source codes for the latest android API.

Open SDK Manager–>install source under api(i.e Android 4.0)
if You got No sources attached then click the button, then choose the Directory
e.g: D:\Android\android___sdk\sources\android-17\ —> this my android sdk location
Depend upon your android installation directory choose accordingly.