Which Activity should we use?


There are four different activities:

1- android.app.Activity

Every activity inherits from this one.

This class doesn’t support fragment until API level 11.

This class doesn’t support nested fragments until API level 17.

2- android.support.v4.app.FragmentActivity

This class is from appcompact-v4 library.


  • Supporting nested fragments
  • Loader

To use this class you should add below dependency to gradle.build

compile 'com.android.support:appcompat-v4:x.x.x'

3- android.support.v7.app.AppCompatActivity

This class is from appcompact-v7 library.


  • Supporting all v4 features
  • Material Design

To use this class you should add below dependency to gradle.build

compile 'com.android.support:appcompat-v7:x.x.x'

4- android.support.v7.app.ActionBarActivity

This class is the old name of AppCompatActivity. For various reasons they changed the name.


  • If you want support Material Design look and fragment under API level 21 you should use AppCompatActivity
  • If you want support nested fragments under API level 17 you should use FragmentActivity
  • If you want support fragment under API level 11 you should use FragmenActivity
  • Else use Activity

Quick Overview on Java 8 Features


After all we have some features of java 8 on android :D, lets look at them.

Default Methods

Default methods enable us to add new functionalities to interfaces without breaking the classes that implements that interface (For more information please check this link).

Important Note: This feature just work on API level 24.

Static Methods

A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.

Important Note: This feature just work on API level 24

Repeating Annotations

There are some situations where you want to apply the same annotation to a declaration or type use. Repeating annotations enable you to do this.

Important Note: This feature just work on API level 24

Lambda Expressions

Lambda expression facilitates functional programming, and simplifies the development a lot.
Important characteristics of a lambda expression:

  • Optional type declaration
  • Optional parenthesis around parameter
  • Optional curly braces
  • Optional return keyword

This feature is available on API level 24 and lower.

Method References

You use lambda expressions to create anonymous methods. Sometimes, however, a lambda expression does nothing but call an existing method. In those cases, it’s often clearer to refer to the existing method by name.

This feature is available on API level 24 and lower.



Migrating to Jack compiler


What is Jack?
Google says:

Jack is a new Android toolchain that compiles Java source into Android dex bytecode. It replaces the previous Android toolchain, which consists of multiple tools, such as javac, ProGuard, jarjar, and dx.

What about existing .jar libraries?
Jill tool translates the existing jar libraries into new library format 😀

Why we need migrating to Jack compiler?

  • Speeds compilation time
    Jack compiler can compile java code to .dex in one step

    • Old toolchain: javac (java -> .class) -> dx (.class -> .dex)
    • Jack toolchain: Jack (java -> .jack -> .dex)
  • Using Java8 features
  • Shrinking, obfuscation, repacking without using different tools


Note: Make sure your Android Studio is updated to the last available version.

To enabling Java 8 and Jack for your project your build.gradle file should be like this:

android {
   defaultConfig {
      targetSdkVersion 24
         enabled true
   compileOptions {
      sourceCompatibility JavaVersion.VERSION_1_8
      targetCompatibility JavaVersion.VERSION_1_8

Rebuild your project and it’s should work without any error, But if you see below error:

Error:Could not get unknown property ‘classpath’ for task ‘:app:transformJackWithJackForDebug’ of type com.android.build.gradle.internal.pipeline.TransformTask.

It’s means you should use annotationProcessor dependency scope instead of apt.


Important Note: Instant Run doesn’t work with Jack.

Cropping Video In Android


Hey guyz, First of all to cropping video we need to use TextureView and this component available from android API 14.

Step 1: Create activity/fragment layout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_height="match_parent" />


Step 2: Add video file

If you want to add video to your project you should add it to /res/raw directory

Step 3: Add below code to activity/fragment


    public static final int MY_VIDEO_WIDTH = 1920;
    public static final int MY_VIDEO_HEIGHT = 1080;

    // I'm using ButterKnife binding my views you can use findViewById instead
    TextureView textureView;
    MediaPlayer mediaPlayer;


    public void onStart() {

    public void onStop() {
        if (mediaPlayer != null) {
            mediaPlayer = null;

    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
        Surface surface = new Surface(surfaceTexture);
        scaleToCropCenter(MY_VIDEO_WIDTH, MY_VIDEO_HEIGHT, this.textureView.getWidth(), this.textureView.getHeight());

        try {
            mediaPlayer = MediaPlayer.create(getActivity(), R.raw.video);

        } catch (Exception e) {

    private void scaleToCropCenter(int videoWidth, int videoHeight, int viewWidth, int viewHeight) {
        float scaleX = 1.0f;
        float scaleY = 1.0f;

        if (videoWidth > viewWidth && videoHeight > viewHeight) {
            scaleX = division(videoWidth, viewWidth);
            scaleY = division(videoHeight, viewHeight);
        } else if (videoWidth < viewWidth && videoHeight < viewHeight) {             scaleY = division(viewWidth , videoWidth);             scaleX = division(viewHeight , videoHeight);         } else if (viewWidth > videoWidth) {
            scaleY = division(division(viewWidth, videoWidth), division(viewHeight, videoHeight));
        } else if (viewHeight > videoHeight) {
            scaleX = division(division(videoWidth, viewWidth), division(viewHeight, videoHeight));

        int pivotPointX = viewWidth / 2;
        int pivotPointY = viewHeight / 2;

        Matrix matrix = new Matrix();
        matrix.setScale(scaleX, scaleY, pivotPointX, pivotPointY);

        textureView.setLayoutParams(new FrameLayout.LayoutParams(viewWidth, viewHeight));

    private float division(float value1, float value2) {
        if (value1 > value2)
            return value1 / value2;
        else return value2 / value1;


That’s it 😉

بکش تا بروز شه (SwipeRefreshLayout)

قدم اول: اضافه کردن support library v4

کد زیر رو به gradle اضافه می کنیم

dependencies {
compile ‘com.android.support:support-v4:23.1.0’

قدم دوم: اضافه کردن SwipeRefreshLayout به Layout


android:layout_height=”match_parent” />

قدم سوم: نمایش SwipeRefresh در هنگام لود

هنگامی که از سرور داده ای رو درخواست می کنیم SwipeRefreshLayout رو فعال می کنیم و در زمانی که درخواست ناموفق و یا موفق بود غیر فعال می کنیم

new Handler().postDelayed(new Runnable() {
public void run() {
}, ۱۰۰);

قدم چهارم: پیاده سازیه OnRefreshListener
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
public void onRefresh() {
//دریافت داده ها از سرور

قدم آخر: تغییر رنگ SwipeRefreshLayout

اکشن بار بر روی اندروید 2.1 به بالا (Action Bar)

اگه برای اپلیکیشن تون می خواین اکشن بار بزارین و کاربراتون از نسخه 3 به بالا استفاده می کنن با خواندن مطلب پایین در ۳ ثانیه می توانید این کارو انجام بدید.

اگه دارین این رو می خوانین یعنی باسه کاربراتون ارزش قائلید و از نسخه های 2.1 به بالا می خواین پشتیبانی کنید ، آستیناتون رو بدین بالا که یه ذره کار سخته 😀 . اگه دارین از Eclipse استفاده می کنید با جاش این صفحه رو ببندین و بعد از اینکه پروژه تون رو بردین رو IntelliJ IDEA یا Android Studio ادامه مطلب رو بخوانین.
  1. اول از همه باید Android Support Library  رو از Android sdk manager  نصب کنید

  2.  در Intellij Idea به مسیر زیر برید:
File -> Project Structure …
  3. در قسمت Libraries دو فایل jar (داخل اندروید SDK) زیر را اضافه کنید
Sdk Path/extras/android/support/v7/appcompat/lib/android-support-v4.jar
Sdk Path/extras/android/support/v7/appcompat/lib/android-support-v7-appcompat.jar

  4.  در قسمت Modules ، ماژول appcompat را از مسیر زیر ایمپورت کنید و در تب Dependencies دو تا فایل jar ی که در مرحله قبل اضافه کردید رو اینجا هم اضافه کنید مثل عکس زیر

Sdk Path/extras/android/support/v7/appcompat

  5.  تا اینجای کار android-support-v7 رو به پروژه اضافه کردید حالا مانده ازش استفاده کنیم ، داخل فایل AndroidManifest.xml به تگ Activity (اکتیویتیه اصلیه پروژه و یا هر جایی که می خواین اکشن بار بزارین) تکه کد زیر رو وارد کنید

  فایل AndroidManifest.xml پروژه من :
 6. کلاس اکتیویتی که می خواین اکشن بار داشته باشه رو باز کنید و از کلاس ActionBarActivity ارث ببرید !! (extend کنید)
تمووم شد آورین ، برای اینکه با نحوه کار اشکن بار آشنا بشین مطلب زیر رو بخونید