Support Annotation #StringDef, #IntDef declaration standards? - android

#StringDef, #IntDef etc has made life much simpler and good alternatives to using enums.
Typically we see that these are defined as follows:
#IntDef({NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})
#Retention(RetentionPolicy.SOURCE)
public #interface NavigationMode {}
public static final int NAVIGATION_MODE_STANDARD = 0;
public static final int NAVIGATION_MODE_LIST = 1;
public static final int NAVIGATION_MODE_TABS = 2;
....
Would there be any issue if one was to define these inside of the annotation itself. eg:
#IntDef({NavigationMode.NAVIGATION_MODE_STANDARD, NavigationMode.NAVIGATION_MODE_LIST, NavigationMode.NAVIGATION_MODE_TABS})
#Retention(RetentionPolicy.SOURCE)
public #interface NavigationMode {
int NAVIGATION_MODE_STANDARD = 0;
int NAVIGATION_MODE_LIST = 1;
int NAVIGATION_MODE_TABS = 2;
}
....
This comes in handy when these def annotation is defined separately so it could be used across modules.

I am using #IntDef notoriously. It's awesome, especially since Android Studio 2.0 can work correctly with this annotation and switch statement.
I am usually using this pattern:
public static final class NavigationMode {
#IntDef({NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})
#Retention(RetentionPolicy.SOURCE)
public #interface Value {
}
public static final int NAVIGATION_MODE_STANDARD = 0;
public static final int NAVIGATION_MODE_LIST = 1;
public static final int NAVIGATION_MODE_TABS = 2;
// Suppress default constructor for noninstantiability
private NavigationMode() {
throw new AssertionError();
}
}
Then I can refer to the value through NavigationMode.NAVIGATION_MODE_STANDARD and the annotation is #NavigationMode.Value with a bit simpler #IntDef definition (without NavigationMode. prefixes).
But I must say... I like your second approach really much!
#IntDef({NavigationMode.NAVIGATION_MODE_STANDARD, NavigationMode.NAVIGATION_MODE_LIST, NavigationMode.NAVIGATION_MODE_TABS})
#Retention(RetentionPolicy.SOURCE)
public #interface NavigationMode {
int NAVIGATION_MODE_STANDARD = 0;
int NAVIGATION_MODE_LIST = 1;
int NAVIGATION_MODE_TABS = 2;
}
I might even switch to it. Although I'm not a big fan of the way Android Studio colors it then:
I don't think there should be any issues with values defined this way.

Related

Android #Intdef for flags how to use it

I am not clear how to use #Intdef when making it a flag like this:
#IntDef(
flag = true
value = {NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})
this example is straight from the docs. What does this actually mean ? does it mean all of them are initially set to true ? if i do a "or" on the following:
NAVIGATION_MODE_STANDARD | NAVIGATION_MODE_LIST
what does it mean ...im a little confused whats happening here.
Using the IntDef#flag() attribute set to true, multiple constants can be combined.
Users can combine the allowed constants with a flag (such as |, &, ^ ).
For example:
public static final int DISPLAY_OP_1 = 1;
public static final int DISPLAY_OP_2 = 1<<1;
public static final int DISPLAY_OP_3 = 1<<2;
#IntDef (
flag=true,
value={
DISPLAY_OP_1,
DISPLAY_OP_2,
DISPLAY_OP_3
}
)
#Retention(RetentionPolicy.SOURCE)
public #interface DisplayOptions{}
public void setIntDefFlag(#DisplayOptions int ops) {
...
}
and Use setIntDefFalg() with '|'
setIntDefFlag(DisplayOptions.DISPLAY_OP1|DisplayOptions.DISPLAY_OP2);

Tons of items in R.java created by android studio

It's my first use of Android studio. But when I use it to create my first project, I found that in R.java(The location is app/build/source/r/debug/com.example.myapplication)
The beginning is like this:
public final class R {
public static final class anim {
public static final int abc_fade_in = 0x7f040000;
public static final int abc_fade_out = 0x7f040001;
public static final int abc_slide_in_bottom = 0x7f040002;
public static final int abc_slide_in_top = 0x7f040003;
public static final int abc_slide_out_bottom = 0x7f040004;
public static final int abc_slide_out_top = 0x7f040005;
}
public static final class attr {
public static final int actionBarDivider = 0x7f01005a;
public static final int actionBarItemBackground = 0x7f01005b;
public static final int actionBarPopupTheme = 0x7f010054;
public static final int actionBarSize = 0x7f010059;
public static final int actionBarSplitStyle = 0x7f010056;
public static final int actionBarStyle = 0x7f010055;
public static final int actionBarTabBarStyle = 0x7f010050;
public static final int actionBarTabStyle = 0x7f01004f;
public static final int actionBarTabTextStyle = 0x7f010051;
public static final int actionBarTheme = 0x7f010057;
public static final int actionBarWidgetTheme = 0x7f010058;
public static final int actionButtonStyle = 0x7f010072;
public static final int actionDropDownStyle = 0x7f01006d;
public static final int actionLayout = 0x7f01002c;
public static final int actionMenuTextAppearance = 0x7f01005c;
public static final int actionMenuTextColor = 0x7f01005d;
public static final int actionModeBackground = 0x7f010060;
public static final int actionModeCloseButtonStyle = 0x7f01005f;
public static final int actionModeCloseDrawable = 0x7f010062;
It list so many items that if I put them to Word, they will fill 38 pages in 11 font size. I tried deleted them, rebuilt them, restart, create another project, but they still exist. I had used Eclipse so I know how it should looks like. What's the problem? Is it caused by Gradle or Android Studio itself?
Deleting, rebuilding, restarting are not the correct approaches you should take.
These lines are generated by the Android Support Library.
Including the Support Libraries in your Android project is considered a best practice for application developers, depending on
the range of platform versions your app is targeting and the APIs that
it uses. Using the features the libraries provide can help you improve
the look of your application, increase performance and broaden the
reach of your application to more users. If you use the Android code
template tools, you will notice that all the Android application
templates include one or more of the Support Libraries by default.
[1] http://developer.android.com/tools/support-library/index.html
Yet, I don't understand why you want to take them off.
R.java is an auto-genrated file by android AAPT(Android Asset Packaging Tool). It has some significance, for every resource in the res folder a corresponding static integer field is created automatically.
So irrespective of the number of times you delete it, it will be generated again.
If you want to reduce the number of lines in the R.java file try deleting some values in the res folder.
The more layout files, widgets and images and other resources you have, the length of the R.java file is going to increase.
I hope this answers your doubts.

Android Header? How to create enums visible in whole project?

I'm looking for a way to create a "header" or something like that to specify some variables like:
enum Misc
{
double EFFECT_DAMAGE = Math.pow(2,0);
double EFFECT_ABSORB = Math.pow(2,1);
double EFFECT_HEAL = Math.pow(2,2);
int SPELL_FIREBALL = 51673;
}
And in every .java file I want to be able to write:
double effect = 1;
if (effect == EFFECT)
{
...some code...
}
Is there a nice way to do this?
I'm creating a mini game for now and want to have all the files nice and tidy to manage my project in the future easier once it gets bigger.
Thx in advance.
public enum Misc
{
EFFECT_DAMAGE(0), // 2^0
EFFECT_ABSORB(1), // 2^1
FIREBALL(245151);
private double value;
private Misc(double d){
value = d;
}
public String toString(){
return String.valueOf(value);
}
}
Access like this:
System.out.println("Fireball damage:" + Misc.FIREBALL);
You cannot assign values this way to a enum in java.
Instead you should use a public class with public static final variables to make them constants.
public class Misc {
public static final double EFFECT_DAMAGE = Math.pow(2,0);
public static final double EFFECT_ABSORB = Math.pow(2,1);
public static final double EFFECT_HEAL = Math.pow(2,2);
public static final int SPELL_FIREBALL = 51673;
}
So you can use in your code like
if (effect == Misc.EFFECT_DAMAGE )
{
...some code...
}
If you want use only the field without the class name first you should import the class as static:
import static test.Misc.*;
....
if (effect == EFFECT_DAMAGE ) {

How do i create a class and the methods I need?

What i need to do is create an app which will generate a random mathmatical expression for the user to solve based on a difficulty level that they select.
eg. novice: a random operation on 2 terms
easy: random operations on 2 or 3 terms
What I'm struggling with is creating a class to handle the creation of expressions
My Game class is as follows:
package w1279057.CW1;
import android.app.Activity;
import android.os.Bundle;
public class Game extends Activity {
public static final String KEY_DIFFICULTY = "w1279057.CW1.difficulty";
public static final int DIFFICULTY_NOVICE = 0;
public static final int DIFFICULTY_EASY = 1;
public static final int DIFFICULTY_MEDIUM = 2;
public static final int DIFFICULTY_GURU = 3;
private int puzzle[];
#Override
public void onCreate(Bundle savedInstanceState) {
int diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_NOVICE);
setContentView(R.layout.gameview);
}
}
What i think i need is a class which has methods for generating a specific amount of operations randomly and then generating the random numbers for the expressions as well, once i have a valid expression i need to update a textview to display the expressions.
Am i on the right track?
I think this could be a good approach, as you will decouple the mathematical logic from your Activity. So... my advice is to go in this way :)

How to organize Android dialog IDs?

I have a base class ActivityA that has some dialogs:
public static final int SOME_DIALOG_ID = 1;
public static final int OTHER_DIALOG_ID = 2;
ActivityB inherits ActivityA and comes with its own dialogs:
public static final int MY_DIALOG_ID = 1;
public static final int CONFLICT_DIALOG_ID = 2;
As you can see, the dialog IDs are no longer unique within the activity. This is a problem that I keep running into. Easy to fix, but tedious to maintian. There must be some good design pattern for this particular issue. Please, enlighten me!
Q: What is the best way to organize dialog IDs?
In my situation, I create class called AppConstant.java that contain all constant in current application, some of it:
public static final int INTENT_REQUEST_CODE_ACT_MAIN_BROWSE_PICTURE = 1;
public static final int INTENT_REQUEST_CODE_ACT_MAIN_CAMMERA_SNAP = 2;
public static final int INTENT_REQUEST_CODE_ACT_PROCESS_IMAGE_PROCESS = 3;
public static final int INTENT_REQUEST_CODE_ACT_PROCESS_BROWSE_PICTURE_FOR_BACK_DATA = 4;
Using this convention, I can easily maintain my ID within single class.
How about use 1, 2 for ActivityA, then 11, 12 for inherited class, then 101, 102 (or 21, 22) for further inheritance depth.

Categories

Resources