How to add Vector image support to old Android versions using new AppCompat?

Standard

Finally we had vector support for all Android versions, 8-)

  • bye bye all different PNG size files in different drawable folders!
  • bye bye FontAwesome!
  1. first of all add these dependencies to your module’s build.gradle:
dependencies {
    .
    .
    .
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:support-vector-drawable:23.3.0'
  1. add this to your android defaultConfig tag in your module’s build.gradle:
android {
    .
    .
    .

    defaultConfig {
        .
        .
        .
        vectorDrawables.useSupportLibrary = true
    }
  1. replace all your ImageView with android.support.v7.widget.AppCompatImageView
    and ImageButton with android.support.v7.widget.AppCompatImageButton in all your project’s xml layouts.

  2. replace all android:src attribute in all ImageView with app:srcCompat.

  3. now import your vector image using New -> “Vector Asset” submenu by right clicking on your drawable folder…

That’s it!!!

Best practise for Logcat colours in Android Studio!

Android Studio Logcat color scheme Preferences
Standard

I know that using solid black colour logcat in Android Studio  is awful !!!

Here is my best practice colour codes that you can use for changing the face of you logcat in Android Studio to make it more useful. go to Android Studio Preferences and search for logcat and save as your own scheme:

Assert9C27B0
Debug2196F3
ErrorF44336
Info4CAF50
WarningFFC107

All colours are picked from material design colour pattern: https://www.google.com/design/spec/style/color.html

App Settings

Standard

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:
https://github.com/mirhoseini/app_settings

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

 loadValues();
 }

 // 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:

<dependency>
 <groupId>com.mirhoseini.appsettings</groupId>
 <artifactId>appsettings</artifactId>
 <version>1.0.1</version>
 <type>pom</type>
</dependency>

or Gradle:

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

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 enable the three-finger drag gesture in OS X El Capitan

Standard

Step 1: Open System Preferences

Step 2: Click Accessibility

Step 3: Click Mouse & Trackpad

Step 4: Click Trackpad Options…

Step 5: Click Enable dragging

Step 6: Select “three finger drag” in the drop down box

Not only does the three finger drag gesture allow you to move around windows and other desktop items, it also allows you to select text.

Tehran Traffic Map App v4.0.2 Released!

Standard

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.

Screenshot_2014-04-11-16-58-02

Make Them Fall, Unity3D Sample Game

Standard

Make Them Fall is a challenging game that requires multitasking capability. Control 2 falling players and let them fall as far as possible without touching any obstacle.

I redesigned Make Them Fall Game using Unity3D and you can access source code as a unity tutorial material at my Github account:

https://github.com/mirhoseini/makethemfall