Android @IntDef Annotation Example

Android @IntDef Annotation Example

Android has some helpful annotations in the support library so, in this post, I will show you an example of how to use @IntDef annotation. You can use this annotation (or @StringDef annotation) instead of Enum.

Steps

1. Add your constants

2. Add the @IntDef annotation and between ({ }) paranthesis, declare your constants

@IntDef({CONSTANT1, CONSTANT2, CONSTANT3})

3. Add the @Retention annotation

@Retention(RetentionPolicy.SOURCE)

RetentionPolicy.SOURCE means that the annotation is only available in the source code.

NOTE: If the @Retention annotation is not set, RetantionPolicy.CLASS will be used as default, which means that the annotation would be available in the source code and in the class, but not at runtime.

4. Add an annotated interface and give a name (the same you give a name for an Enum)

public @interface YourName{};

5. Add a variable which will be set with one of the Constants.

6. Add a setter and getter for the variable created above using annotation in order to force you to write one of the declared constants. The annotation should be the name you gave for the annotated interface at step 4.

Code Example

import android.os.Bundle;
import android.support.annotation.IntDef;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class MainActivity extends AppCompatActivity {

    public static final int MERCURY = 0;
    public static final int VENUS = 1;
    public static final int EARTH = 2;
    public static final int MARS = 3;
    public static final int JUPITER = 4;
    public static final int SATURN = 5;
    public static final int URANUS = 6;
    public static final int NEPTUNE = 7;

    @IntDef({MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Planets{};

    @Planets int mCurrentPlanet = EARTH;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        @Planets int currentPlanet = getCurrentPlanet();

        switch (currentPlanet) {
            case MERCURY:
                Log.e("Planet", "Mercury is the closest to the Sun");
                break;
            case VENUS:
                Log.e("Planet", "Venus is the 2nd planet from the Sun");
                break;
            case EARTH:
                Log.e("Planet", "EARTH is the 3rd planet from the Sun");
                break;
            case MARS:
                Log.e("Planet", "MARS is the 4th planet from the Sun");
                break;
            case JUPITER:
                Log.e("Planet", "JUPITER is the 5th planet from the Sun");
                break;
            case SATURN:
                Log.e("Planet", "SATURN is the 6th planet from the Sun");
                break;
            case URANUS:
                Log.e("Planet", "URANUS is the 7th planet from the Sun");
                break;
            case NEPTUNE:
                Log.e("Planet", "NEPTUNE is the 8th planet from the Sun");
                break;
        }
    }

    @Planets
    public int getCurrentPlanet() {
        return mCurrentPlanet;
    }

    public void setCurrentPlanet(@Planets int currentPlanet) {
        mCurrentPlanet = currentPlanet;
    }
}

Using the above example, you should be forced to set another planet only by using  the NAME of one of the declared constants. You will get an error if you write the value of one of the constants like in the below image:

indef annotation

, , , , ,
Previous Post
Android Set Horizontal Divider to a Vertical LinearLayout
Next Post
Android Build Types and Flavors

Related Posts

Menu