Flag Attributes in Android — How to Use Them

attribute="option1|option2"

What Are Bit Flags?

110
0 = 0*2³ + 0*2² + 0*2¹ + 0*2⁰ = 0000
1 = 0*2³ + 0*2² + 0*2¹ + 1*2⁰ = 0001
2 = 0*2³ + 0*2² + 1*2¹ + 0*2⁰ = 0010
4 = 0*2³ + 1*2² + 0*2¹ + 0*2⁰ = 0100
8 = 1*2³ + 0*2² + 0*2¹ + 0*2⁰ = 1000

Declaring XML Flag Attributes

<resources>
<declare-styleable name="MyView">
<attr name="drawBorder">
<flag name="none" value="0" />
<flag name="top" value="1" />
<flag name="right" value="2" />
<flag name="bottom" value="4" />
<flag name="left" value="8" />
<flag name="all" value="15" />
</attr>
...
</declare-styleable>
</resources>
  • Every value has exactly one bit which is set to 1. All other bits are 0.
  • None of the values have the same bit set to 1.
<my.package.name.MyView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:drawBorder="bottom|top" />

Read XML Flag Attributes

public MyView extends View{    // Constants for the flags
private static final int BORDER_NONE_DEFAULT = 0;
private static final int BORDER_TOP = 1;
private static final int BORDER_RIGHT = 2;
private static final int BORDER_BOTTOM = 4;
private static final int BORDER_LEFT = 8;
// Variable for the current value
private int mDrawBorder = BORDER_NONE_DEFAULT;
public MyView(Context context){
// Delegate to next constructor
this(context, null);
}
public MyView(Context context, AttributeSet attrs){
// Delegate to next constructor
this(context, attrs, 0);
}
public MyView(Context context, AttributeSet attrs,
int defStyleAttr){
super(context, attrs, defStyleAttr);
// Read attributes
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs, R.styleable.MyView);
try {
mDrawBorder = a.getInteger(
R.styleable.MyView_drawBorder,
BORDER_NONE_DEFAULT);
} finally {
a.recycle();
}

}
...
}

Working With Bit Flags

| (bitwise logically or)
Example: 100 | 001 = 101
The result has a 1 on the position where at least one value has a 1.
& (bitwise logically and)
Example: 100 & 101 = 100
The result has a 1 on the position where both values have a 1.
~ (bitwise inverse)
Example: ~100 = 011
All bits get inverted.
^ (bitwise exclusive or)
Example: 100^101 = 001
The result has a 1 on the position where one value has a 1 whereas the other value has a 0.
Example 1: app:drawBorder="none|top"
Example 2: app:drawBorder="bottom|all"

Check If a Flag is Contained in The Set

private boolean containsFlag(int flagSet, int flag){
return (flagSet|flag) == flagSet;
}
// Method call
boolean drawBorderTop = containsFlag(mDrawBorder, BORDER_TOP);

Add a Flag to The Set

private int addFlag(int flagSet, int flag){
return flagSet|flag;
}
// Method call
mDrawBorder = addFlag(mDrawBorder, BORDER_LEFT);

Toggle a Flag in The Set

private int toggleFlag(int flagSet, int flag){
return flagSet^flag;
}
// Method call
mDrawBorder = toggleFlag(mDrawBorder, BORDER_LEFT);
Example 1: 110^010 = 100 (Binary)
6 ^ 2 = 4 (Decimal)
Example 2: 100^010 = 110 (Binary)
4 ^ 2 = 6 (Decimal)

Remove a Flag From the Set

private int removeFlag(int flagSet, int flag){
return flagSet&(~flag);
}
// Method call
mDrawBorder = removeFlag(mDrawBorder, BORDER_LEFT);
110&(~010) = 110&101 = 100 (Binary)
6 &(~ 2 ) = 6 & 5 = 4 (Decimal)

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store