App Settings


This is a best practice for Android Applications Config Management using Shared Preferences.
AppSettings class help you to manage deferent types of data in your application and load them in memory for faster use also save them in shared preferences for next run.

  • Eliminate SharedPreferences and Editor Commits calls.
  • Different method ovverride to set and get all data types.
  • Allow to use different XML files for saving and accessing settings.

Github_LogoGithub repo:

// load saved values
 private void loadValues() {
 etName.setText(AppSettings.getString(this, KEY_NAME, ""));
 spAge.setSelection(AppSettings.getInt(this, KEY_AGE, 0));
 rbMale.setChecked(AppSettings.getBoolean(this, KEY_IS_MALE, true));
 rbFemale.setChecked(!AppSettings.getBoolean(this, KEY_IS_MALE, true));
 sbWeight.setProgress((int) (AppSettings.getFloat(this, KEY_WEIGHT, 0f) * WEIGHT_FACTOR));

 // clear saved values
 private void clearValues() {
 AppSettings.clearValue(this, KEY_NAME);
 AppSettings.clearValue(this, KEY_AGE);
 AppSettings.clearValue(this, KEY_IS_MALE);
 AppSettings.clearValue(this, KEY_WEIGHT);


 // save input values
 private void saveValues() {
 AppSettings.setValue(this, KEY_NAME, etName.getText().toString().trim());
 AppSettings.setValue(this, KEY_AGE, spAge.getSelectedItemPosition());
 AppSettings.setValue(this, KEY_IS_MALE, rbMale.isChecked());
 AppSettings.setValue(this, KEY_WEIGHT, (float) sbWeight.getProgress() / WEIGHT_FACTOR);

Grab via Maven:


or Gradle:

compile 'com.mirhoseini.appsettings:appsettings:1.0.1'

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


Sometimes depends on libraries that you have used in your Android project this annoying error shows up during gradle build using Android Studio: 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/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?


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()

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


        mRetrofit =;

now have fun with logs at logcat…

Tehran Traffic Map App v4.0.2 Released!


Tehran Traffic Map IconTehran Traffic Map
Category: Transportation
Version: 4.0.2
Size: 3.3 MB

Tehran Traffic Map
This Android application use your mobile internet connection (WIFI/GPRS) to show a real-time Traffic information for surface street traffic conditions within the city of Tehran. All information received from “Tehran Traffic Control Co.” cameras server and visualize on a map. It is a dual language program (Persian/English depending on device default language).
It also lets you zoom on the streets and compare new received and older map.


How to connect your activity to its fragment


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);

    public void onAttach(Activity 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:

How to manage your Android Application Settings


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


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


Based on Android SQLite Essentials book:
Android SQLite Essentials

Insulin Reminder App v1.0 Released!


Insulin Reminder
Category: Health
Version: 1.0
Size: 2 MB

Insulin Reminder
This application is an insulin reminder for diabetes people. It uses syringe with animation shown with two different types of insulins to remind the patient not to forget and shows how to fill the syringe. It also could alarm another person with SMS if the patient forgot to inject his insulin after a while.