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!!!

How to create a toggle button with custom image and no text in Android

Standard

Create a file ic_toggle.xml in your res/drawable folder

<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item
 android:state_checked="false"
 android:drawable="@drawable/ic_slide_switch_off" />
 <item
 android:state_checked="true"
 android:drawable="@drawable/ic_slide_switch_on" />
</selector>

Here @drawable/ic_slide_switch_on & @drawable/ic_slide_switch_off are images you create.

Then create another file in the same folder, name it ic_toggle_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+android:id/background" 
 android:drawable="@android:color/transparent" />
<item android:id="@+android:id/toggle"
 android:drawable="@drawable/ic_toggle" />
</layer-list>

Now add to your custom theme, (if you don’t have one create a styles.xml file in your res/values/folder)

<style name="Widget.Button.Toggle" parent="android:Widget">
 <item name="android:background">@drawable/ic_toggle_bg</item>
 <item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
</style>

<style name="toggleButton" parent="@android:Theme.Black">
<item name="android:buttonStyleToggle">@style/Widget.Button.Toggle</item>
<item name="android:textOn"></item>
<item name="android:textOff"></item>

This creates a custom toggle button for you.
Now use the custom style and background in your view.

<ToggleButton
 android:id="@+id/toggleButton"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout_gravity="right"
 style="@style/toggleButton"
 android:background="@drawable/ic_toggle_bg"
 />

How to use Google Analytic for your app

Standard

Introduction

The Google Analytics SDK for Android makes it easy for developers to collect user engagement data from their apps. Developers can then use the Google Analytics app-tracking reports to measure:

  • The number of active users are using their applications.
  • From where in the world the application is being used.
  • Adoption and usage of specific features.
  • In-app purchases and transactions.
  • The number and type of application crashes.
  • And many other useful metrics.