|
|
<!doctype html><html lang="en"><head><title data-rh="true">Flag Attributes in Android — How to Use Them - Jakob Ulbrich - Medium</title><meta data-rh="true" charset="utf-8" /><meta data-rh="true" name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1" /><meta data-rh="true" name="theme-color" content="#000000" /><meta data-rh="true" name="twitter:app:name:iphone" content="Medium" /><meta data-rh="true" name="twitter:app:id:iphone" content="828256236" /><meta data-rh="true" property="al:ios:app_name" content="Medium" /><meta data-rh="true" property="al:ios:app_store_id" content="828256236" /><meta data-rh="true" property="al:android:package" content="com.medium.reader" /><meta data-rh="true" property="fb:app_id" content="542599432471018" /><meta data-rh="true" property="og:site_name" content="Medium" /><meta data-rh="true" property="og:type" content="article" /><meta data-rh="true" property="article:published_time" content="2018-01-19T17:06:07.358Z" /><meta data-rh="true" name="title" content="Flag Attributes in Android — How to Use Them - Jakob Ulbrich - Medium" /><meta data-rh="true" property="og:title" content="Flag Attributes in Android — How to Use Them" /><meta data-rh="true" property="twitter:title" content="Flag Attributes in Android — How to Use Them" /><meta data-rh="true" name="twitter:site" content="@Medium" /><meta data-rh="true" name="twitter:app:url:iphone" content="medium://p/ac4ec8aee7d1" /><meta data-rh="true" property="al:android:url" content="medium://p/ac4ec8aee7d1" /><meta data-rh="true" property="al:ios:url" content="medium://p/ac4ec8aee7d1" /><meta data-rh="true" name="apple-itunes-app" content="app-id=828256236,app-argument=medium://p/ac4ec8aee7d1" /><meta data-rh="true" property="al:android:app_name" content="Medium" /><meta data-rh="true" name="description"content="I’m sure you have seen something like the following line very often while writing the XML attributes for your views or layouts. Notice the | between the options. It is not only a fancy separator, it…" /><meta data-rh="true" property="og:description"content="I’m sure you have seen something like the following line very often while writing the XML attributes for your views or layouts." /><meta data-rh="true" property="twitter:description"content="I’m sure you have seen something like the following line very often while writing the XML attributes for your views or layouts." /><meta data-rh="true" property="og:url"content="https://medium.com/@JakobUlbrich/flag-attributes-in-android-how-to-use-them-ac4ec8aee7d1" /><meta data-rh="true" property="al:web:url"content="https://medium.com/@JakobUlbrich/flag-attributes-in-android-how-to-use-them-ac4ec8aee7d1" /><meta data-rh="true" property="og:image" content="https://miro.medium.com/max/1200/1*0MdHo7i3dNPY9QwQ7dT0jQ.png" /><meta data-rh="true" name="twitter:image:src"content="https://miro.medium.com/max/1200/1*0MdHo7i3dNPY9QwQ7dT0jQ.png" /><meta data-rh="true" name="twitter:card" content="summary_large_image" /><meta data-rh="true" property="article:author" content="/@JakobUlbrich" /><meta data-rh="true" name="author" content="Jakob Ulbrich" /><meta data-rh="true" name="robots" content="index,follow" /><meta data-rh="true" name="referrer" content="unsafe-url" /><meta data-rh="true" name="twitter:label1" value="Reading time" /><meta data-rh="true" name="twitter:data1" value="7 min read" /><meta data-rh="true" name="parsely-post-id" content="ac4ec8aee7d1" /><link data-rh="true" rel="publisher" href="https://plus.google.com/103654360130207659246" /><link data-rh="true" rel="search" type="application/opensearchdescription+xml" title="Medium" href="/osd.xml" /><link data-rh="true" rel="apple-touch-icon" sizes="152x152"href="https://cdn-images-1.medium.com/fit/c/152/152/1*8I-HPL0bfoIzGied-dzOvA.png" /><link data-rh="true" rel="apple-touch-icon" sizes="120x120"href="https://cdn-images-1.medium.com/fit/c/120/120/1*8I-HPL0bfoIzGied-dzOvA.png" /><link data-rh="true" rel="apple-touch-icon" sizes="76x76"href="https://cdn-images-1.medium.com/fit/c/76/76/1*8I-HPL0bfoIzGied-dzOvA.png" /><link data-rh="true" rel="apple-touch-icon" sizes="60x60"href="https://cdn-images-1.medium.com/fit/c/60/60/1*8I-HPL0bfoIzGied-dzOvA.png" /><link data-rh="true" rel="mask-icon"href="https://cdn-static-1.medium.com/_/fp/icons/monogram-mask.KPLCSFEZviQN0jQ7veN2RQ.svg" color="#171717" /><link data-rh="true" rel="icon"href="https://cdn-static-1.medium.com/_/fp/icons/favicon-rebrand-medium.3Y6xpZ-0FSdWDnPM3hSBIA.ico" /><link data-rh="true" id="glyph_link" rel="stylesheet" type="text/css"href="https://glyph.medium.com/css/e/sr/latin/e/ssr/latin/e/ssb/latin/m2.css" /><link data-rh="true" rel="author" href="/@JakobUlbrich" /><link data-rh="true" rel="canonical"href="https://medium.com/@JakobUlbrich/flag-attributes-in-android-how-to-use-them-ac4ec8aee7d1" /><link data-rh="true" rel="alternate" href="android-app://com.medium.reader/https/medium.com/p/ac4ec8aee7d1" /><style type="text/css" data-fela-rehydration="365" data-fela-type="STATIC">html{box-sizing:border-box}*,*:before,*:after{box-sizing:inherit}body{margin:0;padding:0;text-rendering:optimizeLegibility;-webkit-font-smoothing:antialiased;color:rgba(0, 0, 0, 0.8);position:relative;min-height:100vh}h1,h2,h3,h4,h5,h6,dl,dd,ol,ul,menu,figure,blockquote,p,pre,form{margin:0}menu,ol,ul{padding:0;list-style:none;list-style-image:none}main{display:block}a{color:inherit;text-decoration:none}a,button,input{-webkit-tap-highlight-color:transparent}img,svg{vertical-align:middle}button{background:transparent;overflow:visible}button,input,optgroup,select,textarea{margin:0}</style><style type="text/css" data-fela-rehydration="365" data-fela-type="KEYFRAME">@-webkit-keyframes k1{0%{transform:scale(1);opacity:1}70%{transform:scale(1.4);opacity:0}100%{opacity:0}}@-moz-keyframes k1{0%{transform:scale(1);opacity:1}70%{transform:scale(1.4);opacity:0}100%{opacity:0}}@keyframes k1{0%{transform:scale(1);opacity:1}70%{transform:scale(1.4);opacity:0}100%{opacity:0}}</style><style type="text/css" data-fela-rehydration="365" data-fela-type="RULE">.a{font-family:medium-content-sans-serif-font, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif}.b{font-weight:400}.c{background-color:rgba(255, 255, 255, 1)}.l{display:block}.m{position:fixed}.n{top:0}.o{left:0}.p{right:0}.q{z-index:500}.r{box-shadow:0 4px 12px 0 rgba(0, 0, 0, 0.05)}.s{transition:transform 300ms ease}.t{will-change:transform}.v{margin-left:auto}.w{margin-right:auto}.x{width:100%}.y{box-sizing:border-box}.aq{height:65px}.ar{display:flex}.as{align-items:center}.av{flex:1 0 auto}.aw{display:none}.ax{fill:rgba(0, 0, 0, 0.84)}.ba{flex:0 0 auto}.bb{color:inherit}.bc{fill:inherit}.bd{font-size:inherit}.be{border:inherit}.bf{font-family:inherit}.bg{letter-spacing:inherit}.bh{font-weight:inherit}.bi{padding:0}.bj{margin:0}.bk:hover{cursor:pointer}.bl:hover{color:rgba(0, 0, 0, 0.9)}.bm:hover{fill:rgba(0, 0, 0, 0.9)}.bn:focus{outline:none}.bo:disabled{cursor:default}.bp:disabled{color:rgba(0, 0, 0, 0.54)}.bq:disabled{fill:rgba(0, 0, 0, 0.54)}.br{font-family:medium-content-sans-serif-font, "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Geneva, Arial, sans-serif}.bs{font-style:normal}.bt{line-height:20px}.bu{font-size:15.8px}.bv{letter-spacing:0px}.bw{color:rgba(0, 0, 0, 0.54)}.bx{fill:rgba(0, 0, 0, 0.54)}.by{margin-left:16px}.bz{color:rgba(2, 158, 116, 1)}.ca{fill:rgba(3, 168, 124, 1)}.cb:hover{color:rgba(1, 143, 105, 1)}.cc:hover{fill:rgba(2, 158, 116, 1)}.cd:disabled{color:rgba(3, 168, 124, 0.5)}.ce:disabled{fill:rgba(3, 168, 124, 0.5)}.cf{padding:8px 16px}.cg{background:0}.ch{border-color:rgba(3, 168, 124, 1)}.ci:hover{border-color:rgba(2, 158, 116, 1)}.cj{border-radius:4px}.ck{border-width:1px}.cl{border-style:solid}.cm{display:inline-block}.cn{text-decoration:none}.co{margin-bottom:0px}.cq{padding-left:24px}.cr{padding-right:24px}.cs{max-width:728px}.ct{flex-direction:column}.cu{opacity:0}.cv{pointer-events:none}.cw{will-change:opacity}.cx{transition:opacity 200ms}.cy{width:131px}.cz{left:50%}.da{transform:translateX(-516px)}.db{top:calc(65px + 54px + 40px)}.dc{padding-top:28px}.dd{margin-bottom:19px}.de{margin-left:-5px}.df{margin-right:5px}.dg{position:relative}.dh{outline:0}.di{border:0}.dj{user-select:none}.dk{cursor:pointer}.dl>svg{pointer-events:none}.dm:active{border-style:none}.dn{-webkit-user-select:none}.do{fill:rgba(0, 0, 0, 0.76)}.dp:focus{fill:rgba(0, 0, 0, 0.54)}.dq:hover{fill:rgba(0, 0, 0, 0.54)}.dr{margin-top:5px}.ds button{text-align:left}.dt{font-weight:300}.du{font-size:16px}.dv{margin-top:40px}.dw{clear:both}.dx{justify-content:center}.ed{max-width:680px}.ee{flex-wrap:wrap}.ef{font-size:15px}.eg{margin-top:25px}.eh{list-style-type:none}.ei{margin-right:8px}.ej{margin-bottom:8px}.ek{border-radius:3px}.el{padding:5px 10px}.em{background:rgba(0, 0, 0, 0.05)}.en{line-height:22px}.eo{margin-top:15px}.ep{justify-content:space-between}.eq{margin-right:16px}.er{border:1px solid rgba(0, 0, 0, 0.1)}.es{border-radius:50%}.et{height:60px}.eu{transition:border-color 150ms ease}.ev{width:60px}.ew:hover{border-color:rgba(0, 0, 0, 0.54)}.ex::before{background: radial-gradient(circle, rgba(0, 0, 0, 0.84) 60%, transparent 70%)}.ey::before{border-radius:50%}.ez::before{content:""}.fa::before{display:block}.fb::before{z-index:0}.fc::before{left:0}.fd::before{height:100%}.fe::before{position:absolute}.ff::before{top:0}.fg::before{width:100%}.fh:hover::before{animation:k1 2000ms infinite cubic-bezier(.1, .12, .25, 1)}.fi:active{border-style:solid}.fj{background:rgba(255, 255, 255, 1)}.fk{transition:fill 200ms ease}.fl{z-index:2}.fm{height:100%}.fn{position:absolute}.fo{color:rgba(0, 0, 0, 0.84)}.fp{padding-right:8px}.fq{padding-top:32px}.fr{border-top:1px solid rgba(0, 0, 0, 0.1)}.fs{margin-bottom:25px}.ft{margin-bottom:32px}.fu{min-height:80px}.fz{height:80px}.ga{width:80px}.gb{padding-left:102px}.gd{text-transform:uppercase}.ge{letter-spacing:0.05em}.gf{margin-bottom:6px}.gg{font-weight:600}.gh{font-size:28px}.gi{line-height:36px}.gj{padding:4px 12px}.gk{border-color:rgba(0, 0, 0, 0.54)}.gl:hover{color:rgba(0, 0, 0, 0.97)}.gm:hover{fill:rgba(0, 0, 0, 0.97)}.gn:hover{border-color:rgba(0, 0, 0, 0.84)}.go:disabled{fill:rgba(0, 0, 0, 0.76)}.gp:disabled{border-color:rgba(0, 0, 0, 0.2)}.gq:disabled{cursor:inherit}.gr:disabled:hover{color:rgba(0, 0, 0, 0.54)}.gs:disabled:hover{fill:rgba(0, 0, 0, 0.76)}.gt:disabled:hover{border-color:rgba(0, 0, 0, 0.2)}.gu{max-width:555px}.gv{max-width:450px}.gw{font-size:18px}.gx{line-height:24px}.gz{padding-top:25px}.ha{padding:20px}.hb{border:1px solid rgba(3, 168, 124, 1)}.hc{text-align:center}.hd{margin-top:64px}.he{background-color:rgba(0, 0, 0, 0.02)}.hf{max-width:1032px}.hg{padding:60px 0}.hh{background-color:rgba(0, 0, 0, 0.9)}.hj{padding-bottom:48px}.hk{border-bottom:1px solid rgba(255, 255, 255, 0.54)}.hl{margin:0 -12px}.hm{margin:0 12px}.hn{flex:1 1 0}.ho{padding-bottom:12px}.hp:hover{color:rgba(255, 255, 255, 0.99)}.hq:hover{fill:rgba(255, 255, 255, 0.99)}.hr:disabled{color:rgba(255, 255, 255, 0.7)}.hs:disabled{fill:rgba(255, 255, 255, 0.7)}.ht{color:rgba(255, 255, 255, 0.98)}.hu{fill:rgba(255, 255, 255, 0.98)}.hv{text-align:inherit}.hw{font-size:21.6px}.hx{letter-spacing:-0.32px}.hy{color:rgba(255, 255, 255, 0.7)}.hz{fill:rgba(255, 255, 255, 0.7)}.ia{text-decoration:underline}.ib{padding-bottom:8px}.ic{padding-top:8px}.id{width:200px}.if:hover{text-decoration:underline}.ig{top:calc(100vh + 100px)}.ih{bottom:calc(100vh + 100px)}.ii{width:10px}.ij{word-break:break-word}.ik{word-wrap:break-word}.il:after{display:block}.im:after{content:""}.in:after{clear:both}.io{margin:0 auto}.ip{line-height:1.23}.iq{letter-spacing:0}.ir{font-family:medium-content-title-font, Georgia, Cambria, "Times New Roman", Times, serif}.is{font-size:40px}.iy{margin-bottom:-0.27em}.iz{line-height:48px}.ja{margin-top:32px}.jb{height:48px}.jc{width:48px}.jd{margin-left:12px}.je{margin-bottom:2px}.jg{overflow:hidden}.jh{max-height:20px}.ji{text-overflow:ellipsis}.jj{display:-webkit-box}.jk{-webkit-line-clamp:1}.jl{-webkit-box-orient:vertical}.jm{margin-left:8px}.jn{padding:0px 8px}.jo{line-height:18px}.ju{transition:opacity 100ms 400ms}.jv{transform:translateZ(0)}.jw{margin:auto}.jx{background-color:rgba(0, 0, 0, 0.05)}.jy{padding-bottom:40%}.jz{filter:blur(20px)}.ka{transform:scale(1.1)}.kb{visibility:visible}.kc{line-height:1.58}.kd{letter-spacing:-0.004em}.ke{font-family:medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif}.kp{margin-bottom:-0.46em}.kq{overflow-x:auto}.kr{line-height:1.18}.ks{letter-spacing:-0.022em}.kt{font-family:Menlo, Monaco, "Courier New", Courier, monospace}.ku{margin-top:-0.09em}.kv{margin-bottom:-0.09em}.kw{white-space:pre-wrap}.kx{font-weight:700}.ky{font-family:medium-content-slab-serif-font, Georgia, Cambria, "Times New Roman", Times, serif}.kz{border:none}.la{margin-top:30px}.lb:before{content:"..."}.lc:before{letter-spacing:0.6em}.ld:before{text-indent:0.6em}.le:before{font-style:italic}.lf:before{line-height:1.4}.lg{line-height:1.12}.lr{margin-bottom:-0.28em}.lx{background-repeat:repeat-x}.ly{background-image:linear-gradient(to right, rgba(0, 0, 0, 0.84) 100%, rgba(0, 0, 0, 0.84) 0);background-image:url('data:image/svg+xml;utf8,<svg preserveAspectRatio="none" viewBox="0 0 1 1" xmlns="http://www.w3.org/2000/svg"><line x1="0" y1="0" x2="1" y2="1" stroke="rgba(0, 0, 0, 0.84)" /></svg>')}.lz{background-size:1px 1px}.ma{background-position:0 1.05em;background-position:0 calc(1em + 1px)}.mb{list-style-type:disc}.mc{margin-left:30px}.md{padding-left:0px}.mj{max-width:1400px}.mk{padding-bottom:54.14285714285715%}.ml{font-style:italic}.nb{margin-bottom:-0.31em}</style><style type="text/css" data-fela-rehydration="365" data-fela-type="RULE" media="all and (min-width:1080px)">.d{display:none}.an{padding-left:24px}.ao{padding-right:24px}.ap{max-width:1080px}.ec{margin:0 24px}.ix{margin-top:0.78em}.jt{margin-top:56px}.kn{font-size:21px}.ko{margin-top:2em}.lp{font-size:34px}.lq{margin-top:1.25em}.lw{margin-top:0.86em}.mi{margin-top:1.05em}.mq{margin-top:1.91em}.mz{font-size:26px}.na{margin-top:1.72em}</style><style type="text/css" data-fela-rehydration="365" data-fela-type="RULE" media="all and (max-width:1079.98px)">.e{display:none}.ak{padding-left:24px}.al{padding-right:24px}.am{max-width:1080px}</style><style type="text/css" data-fela-rehydration="365" data-fela-type="RULE" media="all and (max-width:903.98px)">.f{display:none}.ah{padding-left:24px}.ai{padding-right:24px}.aj{max-width:904px}</style><style type="text/css" data-fela-rehydration="365" data-fela-type="RULE" media="all and (max-width:727.98px)">.g{display:none}.ae{padding-left:24px}.af{padding-right:24px}.ag{max-width:728px}.at{height:56px}.au{display:flex}.ay{margin-left:-5px}.az{display:block}.cp{margin-bottom:0px}.fv{margin-bottom:24px}.fw{align-items:center}.fx{width:102px}.fy{position:relative}.gc{padding-left:0}.gy{margin-top:24px}.hi{padding:32px 0}.ie{width:140px}</style><style type="text/css" data-fela-rehydration="365" data-fela-type="RULE" media="all and (max-width:551.98px)">.h{display:none}.z{padding-left:24px}.ab{padding-right:24px}.ac{max-width:552px}.dy{margin:0 24px}.it{margin-top:0.39em}.jf{margin-bottom:0px}.jp{margin-top:40px}.kf{font-size:18px}.kg{margin-top:1.56em}.lh{font-size:30px}.li{margin-top:0.93em}.ls{margin-top:0.67em}.me{margin-top:1.34em}.mm{margin-top:1.41em}.mr{font-size:24px}.ms{margin-top:1.23em}</style><style type="text/css" data-fela-rehydration="365" data-fela-type="RULE"media="all and (min-width:904px) and (max-width:1079.98px)">.i{display:none}.eb{margin:0 24px}.iw{margin-top:0.78em}.js{margin-top:56px}.kl{font-size:21px}.km{margin-top:2em}.ln{font-size:34px}.lo{margin-top:1.25em}.lv{margin-top:0.86em}.mh{margin-top:1.05em}.mp{margin-top:1.91em}.mx{font-size:26px}.my{margin-top:1.72em}</style><style type="text/css" data-fela-rehydration="365" data-fela-type="RULE"media="all and (min-width:728px) and (max-width:903.98px)">.j{display:none}.ea{margin:0 24px}.iv{margin-top:0.78em}.jr{margin-top:56px}.kj{font-size:21px}.kk{margin-top:2em}.ll{font-size:34px}.lm{margin-top:1.25em}.lu{margin-top:0.86em}.mg{margin-top:1.05em}.mo{margin-top:1.91em}.mv{font-size:26px}.mw{margin-top:1.72em}</style><style type="text/css" data-fela-rehydration="365" data-fela-type="RULE"media="all and (min-width:552px) and (max-width:727.98px)">.k{display:none}.dz{margin:0 24px}.iu{margin-top:0.39em}.jq{margin-top:40px}.kh{font-size:18px}.ki{margin-top:1.56em}.lj{font-size:30px}.lk{margin-top:0.93em}.lt{margin-top:0.67em}.mf{margin-top:1.34em}.mn{margin-top:1.41em}.mt{font-size:24px}.mu{margin-top:1.23em}</style><style type="text/css" data-fela-rehydration="365" data-fela-type="RULE" media="print">.u{display:none}</style></head><body><div id="root"><div class="a b c"><div class="d e f g h i j k"></div><nav class="l m n o p c q r s t u"><div class="branch-journeys-top"><section class="v w x y l z ab ac ae af ag ah ai aj ak al am an ao ap"><div class="aq ar as at au"><div class="l av q"><div class="ar as"><a href="/?source=post_page-----ac4ec8aee7d1----------------------"aria-label="Homepage"><div class="aw g"><svg height="22" width="112" viewBox="0 0 111.5 22" class="ax"><pathd="M56.3 19.5c0 .4 0 .5.3.7l1.5 1.4v.1h-6.5V19c-.7 1.8-2.4 3-4.3 3-3.3 0-5.8-2.6-5.8-7.5 0-4.5 2.6-7.6 6.3-7.6 1.6-.1 3.1.8 3.8 2.4V3.2c0-.3-.1-.6-.3-.7l-1.4-1.4V1l6.5-.8v19.3zm-4.8-.8V9.5c-.5-.6-1.2-.9-1.9-.9-1.6 0-3.1 1.4-3.1 5.7 0 4 1.3 5.4 3 5.4.8.1 1.6-.3 2-1zm9.1 3.1V9.4c0-.3-.1-.6-.3-.7l-1.4-1.5v-.1h6.5v12.5c0 .4 0 .5.3.7l1.4 1.4v.1h-6.5zm-.2-19.2C60.4 1.2 61.5 0 63 0c1.4 0 2.6 1.2 2.6 2.6S64.4 5.3 63 5.3a2.6 2.6 0 0 1-2.6-2.7zm22.5 16.9c0 .4 0 .5.3.7l1.5 1.4v.1h-6.5v-3.2c-.6 2-2.4 3.4-4.5 3.4-2.9 0-4.4-2.1-4.4-6.2 0-1.9 0-4.1.1-6.5 0-.3-.1-.5-.3-.7L67.7 7v.1H74v8c0 2.6.4 4.4 2 4.4.9-.1 1.7-.6 2.1-1.3V9.5c0-.3-.1-.6-.3-.7l-1.4-1.5v-.2h6.5v12.4zm22 2.3c0-.5.1-6.5.1-7.9 0-2.6-.4-4.5-2.2-4.5-.9 0-1.8.5-2.3 1.3.2.8.3 1.7.3 2.5 0 1.8-.1 4.2-.1 6.5 0 .3.1.5.3.7l1.5 1.4v.1H96c0-.4.1-6.5.1-7.9 0-2.7-.4-4.5-2.2-4.5-.9 0-1.7.5-2.2 1.3v9c0 .4 0 .5.3.7l1.4 1.4v.1h-6.5V9.5c0-.3-.1-.6-.3-.7l-1.4-1.5v-.2h6.5v3.1a4.6 4.6 0 0 1 4.6-3.4c2.2 0 3.6 1.2 4.2 3.5.7-2.1 2.7-3.6 4.9-3.5 2.9 0 4.5 2.2 4.5 6.2 0 1.9-.1 4.2-.1 6.5-.1.3.1.6.3.7l1.4 1.4v.1h-6.6zm-81.4-2l1.9 1.9v.1h-9.8v-.1l2-1.9c.2-.2.3-.4.3-.7V7.3c0-.5 0-1.2.1-1.8L11.4 22h-.1L4.5 6.8c-.1-.4-.2-.4-.3-.6v10c-.1.7 0 1.3.3 1.9l2.7 3.6v.1H0v-.1L2.7 18c.3-.6.4-1.3.3-1.9v-11c0-.5-.1-1.1-.5-1.5L.7 1.1V1h7l5.8 12.9L18.6 1h6.8v.1l-1.9 2.2c-.2.2-.3.5-.3.7v15.2c0 .2.1.5.3.6zm7.6-5.9c0 3.8 1.9 5.3 4.2 5.3 1.9.1 3.6-1 4.4-2.7h.1c-.8 3.7-3.1 5.5-6.5 5.5-3.7 0-7.2-2.2-7.2-7.4 0-5.5 3.5-7.6 7.3-7.6 3.1 0 6.4 1.5 6.4 6.2v.8h-8.7zm0-.8h4.3v-.8c0-3.9-.8-4.9-2-4.9-1.4.1-2.3 1.6-2.3 5.7z"></path></svg></div><div class="l ay az"><svg width="45" height="45" viewBox="0 0 45 45" class="ax"><pathd="M5 40V5h35v35H5zm8.56-12.63c0 .56-.03.69-.32 1.03L10.8 31.4v.4h6.97v-.4L15.3 28.4c-.29-.34-.34-.5-.34-1.03v-8.95l6.13 13.36h.71l5.26-13.36v10.64c0 .3 0 .35-.19.53l-1.85 1.8v.4h9.2v-.4l-1.83-1.8c-.18-.18-.2-.24-.2-.53V15.94c0-.3.02-.35.2-.53l1.82-1.8v-.4h-6.47l-4.62 11.55-5.2-11.54h-6.8v.4l2.15 2.63c.24.3.29.37.29.77v10.35z"></path></svg></div></a></div></div><div class="l ba q"><div class="ar as"><div class="ar g"><div><ahref="https://medium.com/membership?source=upgrade_membership---nav_full------------------------"class="bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq"><spanclass="br b bs bt bu bv l bw bx">Become a member</span></a></div><div class="by l"><span class="br b bs bt bu bv l bw bx"><ahref="https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40JakobUlbrich%2Fflag-attributes-in-android-how-to-use-them-ac4ec8aee7d1&source=post_page-----ac4ec8aee7d1---------------------nav_reg-"class="bz ca bd be bf bg bh bi bj bk cb cc bn bo cd ce">Sign in</a></span></div></div><div class="by l"><button class="cf cg bz ca ch cb cc ci bk cj br b bs bt bu bv ck cl y cm cn bn">Getstarted</button></div></div></div></div></section></div></nav><div class="co aq l cp at"></div><article><section class="cq cr v w x cs y ar ct"></section><span class="l"></span><div><div class="fn o ig ih ii cv"></div><section class="ij ik il im in"><div class="y io x cs cq cr"><div><div id="930d" class="ip iq fo bs ir b is it iu iv iw ix iy"><h1 class="ir b is iz fo">Flag Attributes in Android — How to Use Them</h1></div><div class="ja"><div class="as ar"><div><a href="/@JakobUlbrich?source=post_page-----ac4ec8aee7d1----------------------"><imgalt="Jakob Ulbrich" src="https://miro.medium.com/fit/c/96/96/1*50pfRWptJGHYxe-dQX_pMg.png"class="l es jb jc" width="48" height="48" /></a></div><div class="jd x l"><div class="ar"><div style="flex:1"><span class="br b bs bt bu bv l fo ax"><div class="je ar as jf"><span class="br dt du bt jg jh ji jj jk jl fo"><aclass="bb bc bd be bf bg bh bi bj bk if bn bo bp bq"href="/@JakobUlbrich?source=post_page-----ac4ec8aee7d1----------------------">JakobUlbrich</a></span><div class="jm l ba h"><buttonclass="jn fo ax cg gk gl gm gn bk bp go gp gq gr gs gt cj br b bs jo ef bv ck cl y cm cn bn">Follow</button></div></div></span></div></div><span class="br b bs bt bu bv l bw bx"><span class="br dt du bt jg jh ji jj jk jl bw"><div><a class="bb bc bd be bf bg bh bi bj bk if bn bo bp bq"href="/@JakobUlbrich/flag-attributes-in-android-how-to-use-them-ac4ec8aee7d1?source=post_page-----ac4ec8aee7d1----------------------">Aug8, 2016</a> </span></span></div></div></div></div></div><div class="dw x"><figure class="jp jq jr js jt dw x paragraph-image"><div class="jw l dg jx"><div class="jy l"><div class="cu ju fn n o fm x jg t jv"><imgsrc="https://miro.medium.com/max/60/1*0MdHo7i3dNPY9QwQ7dT0jQ.png?q=20"class="fn n o fm x jz ka kb" width="2400" height="960" /></div><img class="cu ju fn n o fm x fj"width="2400" height="960" /><noscript><imgsrc="https://miro.medium.com/max/4800/1*0MdHo7i3dNPY9QwQ7dT0jQ.png" class="fn n o fm x"width="2400" height="960" /></noscript></div></div></figure></div><div class="y io x cs cq cr"><p id="3674" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">I’m sure you have seen something like the following line very often while writing the XML attributes for your views or layouts.</p><preclass="jp jq jr js jt ha em kq"><span id="cf19" class="kr ks fo bs kt b du ku kv l kw">attribute="option1|option2"</span></pre><p id="541f" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">Notice the <strongclass="ke kx">|</strong> between the options. It is not only a fancy separator, it is a bitwiseoperator merging the two options into one single value.</p><p id="fe64" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">The following will explain whatexactly Bit Flags are, how to declare custom XML flag attributes and how to read and work with them inthe code.</p></div></section><hr class="ky dt gh kz la hc lb lc ld le lf" /><section class="ij ik il im in"><div class="y io x cs cq cr"><h1 id="c9ce" class="lg ks fo bs br gg lh li lj lk ll lm ln lo lp lq lr">What Are Bit Flags?</h1><p id="ea8a" class="kc kd fo bs ke b kf ls kh lt kj lu kl lv kn lw kp">Generally you can see Bit Flags asa number of boolean values stored in one single value.</p><p id="6368" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">In the binary system a bit has twostates:<strong class="ke kx">on</strong> and <strong class="ke kx">off</strong>. Now think about a rowof bits where every bit is an indicator for one of your options. When the option is set, the bit has thevalue 1. If not, it has the value 0.</p><preclass="jp jq jr js jt ha em kq"><span id="ff9e" class="kr ks fo bs kt b du ku kv l kw">110</span></pre><p id="eac5" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">We read this <strongclass="ke kx">from right to left</strong>.<br />The bit for our first option is <strongclass="ke kx">0</strong>. That means the option is <strong class="ke kx">not set</strong>. Whereas thebits for the second and third options are <strong class="ke kx">1</strong>. This means they are <strongclass="ke kx">set</strong>.</p><p id="b6da" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">Every integer value has arepresentation in the binary system and and vice versa.</p><preclass="jp jq jr js jt ha em kq"><span id="1b00" class="kr ks fo bs kt b du ku kv l kw">0 = <strong class="kt kx">0</strong>*2³ + <strong class="kt kx">0</strong>*2² + <strong class="kt kx">0</strong>*2¹ + <strong class="kt kx">0</strong>*2⁰ = 0000<br/>1 = <strong class="kt kx">0</strong>*2³ + <strong class="kt kx">0</strong>*2² + <strong class="kt kx">0</strong>*2¹ + <strong class="kt kx">1</strong>*2⁰ = 0001<br/>2 = <strong class="kt kx">0</strong>*2³ + <strong class="kt kx">0</strong>*2² + <strong class="kt kx">1</strong>*2¹ + <strong class="kt kx">0</strong>*2⁰ = 0010<br/>4 = <strong class="kt kx">0</strong>*2³ + <strong class="kt kx">1</strong>*2² + <strong class="kt kx">0</strong>*2¹ + <strong class="kt kx">0</strong>*2⁰ = 0100<br/>8 = <strong class="kt kx">1</strong>*2³ + <strong class="kt kx">0</strong>*2² + <strong class="kt kx">0</strong>*2¹ + <strong class="kt kx">0</strong>*2⁰ = 1000</span></pre><p id="2244" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">This means we can store our BitFlag value as a plain integer value. And we can use the bitwise operators (more about them later)directly on integer values.</p></div></section><hr class="ky dt gh kz la hc lb lc ld le lf" /><section class="ij ik il im in"><div class="y io x cs cq cr"><h1 id="98b3" class="lg ks fo bs br gg lh li lj lk ll lm ln lo lp lq lr">Declaring XML Flag Attributes</h1><p id="557e" class="kc kd fo bs ke b kf ls kh lt kj lu kl lv kn lw kp">Now, let’s assume that we want tocreate a custom view called <strong class="ke kx">MyView</strong> which can draw a border around itself,and that we want to specify on which sites (top, right, bottom, left) it should draw the borders usingan XML flag attribute.<br />I will <strong class="ke kx">not</strong> explain how to actually drawsomething, I will only explain how to work with flags.</p><p id="d1a0" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">We can declare XML flag attributeslike any other attribute. You can find an official documentation <ahref="https://developer.android.com/training/custom-views/create-view.html#customattr"class="bb cn lx ly lz ma" target="_blank">here</a>.</p><p id="3d1c" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">In our <strongclass="ke kx">values/attrs.xml</strong> file we declare the new attribute called <strongclass="ke kx">drawBorder</strong> for our view called <strong class="ke kx">MyView</strong>.</p><preclass="jp jq jr js jt ha em kq"><span id="6dda" class="kr ks fo bs kt b du ku kv l kw"><resources><br/><declare-styleable name="<strong class="kt kx">MyView</strong>"><br/><attr name="<strong class="kt kx">drawBorder</strong>"><br/><flag name="none" value="<strong class="kt kx">0</strong>" /><br/><flag name="top" value="<strong class="kt kx">1</strong>" /><br/><flag name="right" value="<strong class="kt kx">2</strong>" /><br/><flag name="bottom" value="<strong class="kt kx">4</strong>" /><br/><flag name="left" value="<strong class="kt kx">8</strong>" /><br/><flag name="all" value="<strong class="kt kx">15</strong>" /><br/></attr><br/>...<br/></declare-styleable><br/></resources></span></pre><p id="273e" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">Look exactly at the values we haveset for the options.<br />We essentially have 4 options (top, right, bottom, left). The other twooptions are used to specify that no option is set or that all options are set.<br />The values for the 4options are chosen, so that when you write them in the binary system, these two statements arefulfilled:</p><ul class=""><li id="dc67" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp mb mc md">Every value has exactlyone bit which is set to 1. All other bits are 0.</li><li id="e466" class="kc kd fo bs ke b kf me kh mf kj mg kl mh kn mi kp mb mc md">None of the values havethe same bit set to 1.</li></ul><figure class="jp jq jr js jt dw v w paragraph-image"><div class="v w mj"><div class="jw l dg jx"><div class="mk l"><div class="cu ju fn n o fm x jg t jv"><imgsrc="https://miro.medium.com/max/60/1*C40RkP6OklGFMd9vK-zYJQ.png?q=20"class="fn n o fm x jz ka kb" width="700" height="379" /></div><imgclass="cu ju fn n o fm x fj" width="700" height="379" /><noscript><imgsrc="https://miro.medium.com/max/1400/1*C40RkP6OklGFMd9vK-zYJQ.png" class="fn n o fm x"width="700" height="379" /></noscript></div></div></div></figure><p id="009e" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">The option <strongclass="ke kx"><em class="ml">none</em></strong> is used to indicate that none of the 4 options is set.And the option <strong class="ke kx"><em class="ml">all</em></strong> is the sum of the values of the 4options and indicates that all of the 4 options are set.</p><p id="4a0b" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">We can now use our custom attributelike this in our custom View:</p><preclass="jp jq jr js jt ha em kq"><span id="eb42" class="kr ks fo bs kt b du ku kv l kw"><my.package.name.MyView<br/>android:layout_width="match_parent"<br/>android:layout_height="wrap_content"<br/>app:drawBorder="bottom|top" /></span></pre></div></section><hr class="ky dt gh kz la hc lb lc ld le lf" /><section class="ij ik il im in"><div class="y io x cs cq cr"><h1 id="237a" class="lg ks fo bs br gg lh li lj lk ll lm ln lo lp lq lr">Read XML Flag Attributes</h1><p id="721e" class="kc kd fo bs ke b kf ls kh lt kj lu kl lv kn lw kp">Let’s start with the basic classfor our custom View. We should declare a constant for each of our options with an equal value asdeclared above. And a variable for the value that we get from reading the XML attribute. You can have alook about how to read attributes <ahref="https://developer.android.com/training/custom-views/create-view.html#applyattr"class="bb cn lx ly lz ma" target="_blank">here</a>.</p><preclass="jp jq jr js jt ha em kq"><span id="1653" class="kr ks fo bs kt b du ku kv l kw">public MyView extends View{</span><span id="8776" class="kr ks fo bs kt b du mm mn mo mp mq kv l kw"><strong class="kt kx">// Constants for the flags<br/>private static final int BORDER_NONE_DEFAULT = 0;<br/>private static final int BORDER_TOP = 1;<br/>private static final int BORDER_RIGHT = 2;<br/>private static final int BORDER_BOTTOM = 4;<br/>private static final int BORDER_LEFT = 8;</strong></span><span id="546c" class="kr ks fo bs kt b du mm mn mo mp mq kv l kw"><strong class="kt kx">// Variable for the current value<br/>private int mDrawBorder = BORDER_NONE_DEFAULT;</strong></span><span id="5cde" class="kr ks fo bs kt b du mm mn mo mp mq kv l kw">public MyView(Context context){<br/>// Delegate to next constructor<br/>this(context, null);<br/>}</span><span id="f925" class="kr ks fo bs kt b du mm mn mo mp mq kv l kw">public MyView(Context context, AttributeSet attrs){<br/>// Delegate to next constructor<br/>this(context, attrs, 0); <br/>}</span><span id="538b" class="kr ks fo bs kt b du mm mn mo mp mq kv l kw">public MyView(Context context, AttributeSet attrs,<br/>int defStyleAttr){<br/>super(context, attrs, defStyleAttr);</span><span id="6f1a" class="kr ks fo bs kt b du mm mn mo mp mq kv l kw"><strong class="kt kx">// Read attributes<br/>TypedArray a = context.getTheme().obtainStyledAttributes(<br/>attrs, R.styleable.MyView);</strong></span><span id="38b1" class="kr ks fo bs kt b du mm mn mo mp mq kv l kw"><strong class="kt kx">try{<br/>mDrawBorder = a.getInteger(<br/>R.styleable.MyView_drawBorder,<br/>BORDER_NONE_DEFAULT);<br/>} finally{<br/>a.recycle();<br/>}</strong><br/>}<br/>...<br/>}</span></pre><p id="6354" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">We have now assigned the valuespecified in the XML layout file to the <strong class="ke kx"><em class="ml">mDrawBorder</em></strong>variable. If the attribute does not exists in the layout the <strong class="ke kx"><emclass="ml">BORDER_NONE_DEFAULT</em></strong> value is used.</p></div></section><hr class="ky dt gh kz la hc lb lc ld le lf" /><section class="ij ik il im in"><div class="y io x cs cq cr"><h1 id="e1c8" class="lg ks fo bs br gg lh li lj lk ll lm ln lo lp lq lr">Working With Bit Flags</h1><p id="551a" class="kc kd fo bs ke b kf ls kh lt kj lu kl lv kn lw kp">Okay, how do we know which of ouroptions were set when we only get one value? That’s why we have defined the constants for our options.</p><p id="5a09" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">There are bitwise operators we canuse to check if an option is contained in the current <strong class="ke kx">mDrawBorder</strong> valueand to enable or disable or toggle an option. Let’s have a look at them:</p><preclass="jp jq jr js jt ha em kq"><span id="b2e4" class="kr ks fo bs kt b du ku kv l kw"><strong class="kt kx">| (bitwise logically or)</strong><br/>Example:100 | 001 = 101<br/>The result has a 1 on the position where at least one value has a 1.</span><span id="8458" class="kr ks fo bs kt b du mm mn mo mp mq kv l kw"><strong class="kt kx">& (bitwise logically and)</strong><br/>Example:100 & 101 = 100<br/>The result has a 1 on the position where both values have a 1.</span><span id="0115" class="kr ks fo bs kt b du mm mn mo mp mq kv l kw"><strong class="kt kx">~ (bitwise inverse)</strong><br/>Example:~100 = 011<br/>All bits get inverted.</span><span id="af80" class="kr ks fo bs kt b du mm mn mo mp mq kv l kw"><strong class="kt kx">^ (bitwise exclusive or)</strong><br/>Example:100^101 = 001<br/>The result has a 1 on the position where one value has a 1 whereas the other value has a 0.</span></pre><p id="c03f" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">Let’s assume we have used theattribute in the following two ways. Yes, technically they are correct, but they may look weird 😵.</p><preclass="jp jq jr js jt ha em kq"><span id="fd22" class="kr ks fo bs kt b du ku kv l kw"><strong class="kt kx">Example 1:</strong> app:drawBorder="none|top"</span></pre><p id="096d" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">In this first example the values of<strong class="ke kx"><em class="ml">none</em></strong> (0 = 000) and <strong class="ke kx"><emclass="ml">top</em></strong> (1 = 001) get combined by the logically or-operator <strongclass="ke kx">|</strong> to the value (001 = 1). The result still contains the flag value for <strongclass="ke kx"><em class="ml">top</em></strong> (which is 1). And also for <strong class="ke kx"><emclass="ml">none</em></strong> (which is 0) because every flag-set contains the 0-flag at every time.</p><preclass="jp jq jr js jt ha em kq"><span id="e78b" class="kr ks fo bs kt b du ku kv l kw"><strong class="kt kx">Example 2:</strong> app:drawBorder="bottom|all"</span></pre><p id="61e7" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">In this second example the valuesof <strong class="ke kx"><em class="ml">bottom</em></strong> (3 =0011) and <strong class="ke kx"><emclass="ml">all</em></strong> (15 = 1111) get combined to (1111 = 15). The result still contains theflag for <strong class="ke kx"><em class="ml">bottom</em></strong> since <strong class="ke kx"><emclass="ml">all</em></strong> enables the bits for all of the 4 options.</p><h2 id="3f0a" class="kr ks fo bs br gg mr ms mt mu mv mw mx my mz na nb">Check If a Flag is Contained inThe Set</h2><p id="914e" class="kc kd fo bs ke b kf ls kh lt kj lu kl lv kn lw kp">Now we need to check which optionis contained in the value of the <strong class="ke kx"><em class="ml">mDrawBorder</em></strong>variable. We do this by calling the following method:</p><preclass="jp jq jr js jt ha em kq"><span id="349d" class="kr ks fo bs kt b du ku kv l kw">private boolean containsFlag(int flagSet, int flag){<br/><strong class="kt kx">return (flagSet|flag) == flagSet;</strong><br/>}</span><span id="efc1" class="kr ks fo bs kt b du mm mn mo mp mq kv l kw">// Method call<br/>boolean drawBorderTop = containsFlag(mDrawBorder, BORDER_TOP);</span></pre><p id="a327" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">We try to add the flag to theexisting flag-set and if the value of the flag-set does not change, we can combine that the flag wasalready contained.</p><h2 id="0923" class="kr ks fo bs br gg mr ms mt mu mv mw mx my mz na nb">Add a Flag to The Set</h2><p id="4c67" class="kc kd fo bs ke b kf ls kh lt kj lu kl lv kn lw kp">Adding a flag is pretty simple. Weuse the same operator (The logically or-operator) like in the XML attributes:</p><preclass="jp jq jr js jt ha em kq"><span id="a87d" class="kr ks fo bs kt b du ku kv l kw">private int addFlag(int flagSet, int flag){<br/><strong class="kt kx">return flagSet|flag;</strong><br/>}</span><span id="0290" class="kr ks fo bs kt b du mm mn mo mp mq kv l kw">// Method call<br/>mDrawBorder = addFlag(mDrawBorder, BORDER_LEFT);</span></pre><p id="f35e" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">We do not need to check if theflag-set already contains the flag that we want to add. When it is already contained, the value simplystays the same.</p><h2 id="ee74" class="kr ks fo bs br gg mr ms mt mu mv mw mx my mz na nb">Toggle a Flag in The Set</h2><p id="d7c2" class="kc kd fo bs ke b kf ls kh lt kj lu kl lv kn lw kp">Toggling is also very simple.</p><preclass="jp jq jr js jt ha em kq"><span id="5c8b" class="kr ks fo bs kt b du ku kv l kw">private int toggleFlag(int flagSet, int flag){<br/><strong class="kt kx">return flagSet^flag;</strong><br/>}</span><span id="b8a6" class="kr ks fo bs kt b du mm mn mo mp mq kv l kw">// Method call<br/>mDrawBorder = toggleFlag(mDrawBorder, BORDER_LEFT);</span></pre><p id="fa65" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">Since the exclusive or-operator<strong class="ke kx">^</strong> only keeps the bits where both bits are different, it will remove thebits that are set in the flag when they are also already set in the flag-set. And it will add them ifthey are not set in the flag-set.</p><preclass="jp jq jr js jt ha em kq"><span id="2b34" class="kr ks fo bs kt b du ku kv l kw"><strong class="kt kx">Example 1:</strong> 110^010 = 100 (Binary)<br/>6 ^ 2=4(Decimal)</span><span id="cf5e" class="kr ks fo bs kt b du mm mn mo mp mq kv l kw"><strong class="kt kx">Example 2:</strong> 100^010 = 110 (Binary)<br/>4 ^ 2=6(Decimal)</span></pre><h2 id="fe39" class="kr ks fo bs br gg mr ms mt mu mv mw mx my mz na nb">Remove a Flag From the Set</h2><p id="5bfd" class="kc kd fo bs ke b kf ls kh lt kj lu kl lv kn lw kp">Removing a flag is a bit morecomplicated (we need two bitwise operators), but still easy.</p><preclass="jp jq jr js jt ha em kq"><span id="5f92" class="kr ks fo bs kt b du ku kv l kw">private int removeFlag(int flagSet, int flag){<br/><strong class="kt kx">return flagSet&(~flag);</strong><br/>}</span><span id="0e7a" class="kr ks fo bs kt b du mm mn mo mp mq kv l kw">// Method call<br/>mDrawBorder = removeFlag(mDrawBorder, BORDER_LEFT);</span></pre><p id="9031" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">First we invert the flag that wewant to remove. Then we combine this with the current flag-set. This means only where the bit in theflag was set, we now have a 0. And that’s why we also have a 0 at this position in the final result. Therest of the result is the same as in the original flag-set. Have a look at this example:</p><preclass="jp jq jr js jt ha em kq"><span id="dd43" class="kr ks fo bs kt b du ku kv l kw">110&(~010) = 110&101 = 100 (Binary)<br/> 6 &(~ 2 ) =6 & 5=4(Decimal)</span></pre></div></section><hr class="ky dt gh kz la hc lb lc ld le lf" /><section class="ij ik il im in"><div class="y io x cs cq cr"><p id="fde9" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">Bit Flags are also a goodalternative if you have a lot of boolean values and especially if you want to store them somehow.Instead of saving a lot of booleans you only need to save one integer value.</p><p id="a523" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">You should now be able to defineyour own flag attributes and work with them in your application. You should also be able to use BitFlags as an alternative to a number of booleans.<br />When you found this tutorial useful feel free tohit the 💚.</p></div></section><hr class="ky dt gh kz la hc lb lc ld le lf" /><section class="ij ik il im in"><div class="y io x cs cq cr"><p id="c1ec" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">If you have any suggestions on howto improve this tutorial or found a mistake, you can leave a private note at the specific position.</p><p id="5752" class="kc kd fo bs ke b kf kg kh ki kj kk kl km kn ko kp">Happy coding 💻.</p></div></section></div></article><div class="cu cv cw m cx cy cz da db e" data-test-id="post-sidebar"><div class="ar ct"><div class="dc dd de ar"><div class="ar as"><div class="df l dg"><ahref="https://medium.com/m/signin?operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40JakobUlbrich%2Fflag-attributes-in-android-how-to-use-them-ac4ec8aee7d1&source=post_sidebar-----ac4ec8aee7d1---------------------clap_sidebar-"class="bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq"><div class="bi dh di dj dk dl dm dn do dp dq"><svg width="29" height="29"><g fill-rule="evenodd"><pathd="M13.74 1l.76 2.97.76-2.97zM16.82 4.78l1.84-2.56-1.43-.47zM10.38 2.22l1.84 2.56-.41-3.03zM22.38 22.62a5.11 5.11 0 0 1-3.16 1.61l.49-.45c2.88-2.89 3.45-5.98 1.69-9.21l-1.1-1.94-.96-2.02c-.31-.67-.23-1.18.25-1.55a.84.84 0 0 1 .66-.16c.34.05.66.28.88.6l2.85 5.02c1.18 1.97 1.38 5.12-1.6 8.1M9.1 22.1l-5.02-5.02a1 1 0 0 1 .7-1.7 1 1 0 0 1 .72.3l2.6 2.6a.44.44 0 0 0 .63-.62L6.1 15.04l-1.75-1.75a1 1 0 1 1 1.41-1.41l4.15 4.15a.44.44 0 0 0 .63 0 .44.44 0 0 0 0-.62L6.4 11.26l-1.18-1.18a1 1 0 0 1 0-1.4 1.02 1.02 0 0 1 1.41 0l1.18 1.16L11.96 14a.44.44 0 0 0 .62 0 .44.44 0 0 0 0-.63L8.43 9.22a.99.99 0 0 1-.3-.7.99.99 0 0 1 .3-.7 1 1 0 0 1 1.41 0l7 6.98a.44.44 0 0 0 .7-.5l-1.35-2.85c-.31-.68-.23-1.19.25-1.56a.85.85 0 0 1 .66-.16c.34.06.66.28.88.6L20.63 15c1.57 2.88 1.07 5.54-1.55 8.16a5.62 5.62 0 0 1-5.06 1.65 9.35 9.35 0 0 1-4.93-2.72zM13 6.98l2.56 2.56c-.5.6-.56 1.41-.15 2.28l.26.56-4.25-4.25a.98.98 0 0 1-.12-.45 1 1 0 0 1 .29-.7 1.02 1.02 0 0 1 1.41 0zm8.89 2.06c-.38-.56-.9-.92-1.49-1.01a1.74 1.74 0 0 0-1.34.33c-.38.29-.61.65-.71 1.06a2.1 2.1 0 0 0-1.1-.56 1.78 1.78 0 0 0-.99.13l-2.64-2.64a1.88 1.88 0 0 0-2.65 0 1.86 1.86 0 0 0-.48.85 1.89 1.89 0 0 0-2.67-.01 1.87 1.87 0 0 0-.5.9c-.76-.75-2-.75-2.7-.04a1.88 1.88 0 0 0 0 2.66c-.3.12-.61.29-.87.55a1.88 1.88 0 0 0 0 2.66l.62.62a1.88 1.88 0 0 0-.9 3.16l5.01 5.02c1.6 1.6 3.52 2.64 5.4 2.96a7.16 7.16 0 0 0 1.18.1c1.03 0 2-.25 2.9-.7A5.9 5.9 0 0 0 23 23.24c3.34-3.34 3.08-6.93 1.74-9.17l-2.87-5.04z"></path></g></svg></div></a></div><div class="dr l"><div class="ds"><h4 class="br dt du bt bw"><button class="bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq">416</button></h4></div></div></div></div><div><div class="cm"><ahref="https://medium.com/m/signin?operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40JakobUlbrich%2Fflag-attributes-in-android-how-to-use-them-ac4ec8aee7d1&source=post_sidebar--------------------------bookmark_sidebar-"class="bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq"><svg width="25" height="25" viewBox="0 0 25 25"><pathd="M19 6a2 2 0 0 0-2-2H8a2 2 0 0 0-2 2v14.66h.01c.01.1.05.2.12.28a.5.5 0 0 0 .7.03l5.67-4.12 5.66 4.13a.5.5 0 0 0 .71-.03.5.5 0 0 0 .12-.29H19V6zm-6.84 9.97L7 19.64V6a1 1 0 0 1 1-1h9a1 1 0 0 1 1 1v13.64l-5.16-3.67a.49.49 0 0 0-.68 0z"fill-rule="evenodd"></path></svg></a></div></div></div></div><div><div class="dv dw ar ct dx"><div class="ar dx"><div class="dy dz ea eb ec ed x"><div class="ar ee"><div class="l"><h4 class="br dt ef bt bw">Thanks to</div></div><div class="ar as ee"></div><div class="eg l"><ul class="bi bj"><li class="cm eh ei ej"><a href="/tag/android" class="ek el cn bw l em en a b ef">Android</a></li><li class="cm eh ei ej"><a href="/tag/android-app-development"class="ek el cn bw l em en a b ef">Android App Development</a></li><li class="cm eh ei ej"><a href="/tag/android-development" class="ek el cn bw l em en a b ef">AndroidDevelopment</a></li><li class="cm eh ei ej"><a href="/tag/android-tutorials" class="ek el cn bw l em en a b ef">AndroidTutorials</a></li></ul></div><div class="eo ar ep u"><div class="ar as"><div class="eq l dg"><ahref="https://medium.com/m/signin?operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40JakobUlbrich%2Fflag-attributes-in-android-how-to-use-them-ac4ec8aee7d1&source=post_actions_footer-----ac4ec8aee7d1---------------------clap_footer-"class="bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq"><div class="c er es ar as et dg eu ev ew ex ey ez fa fb fc fd fe ff fg fh"><div class="bi dh di dj dk dl fi dn as fj es ar dx fk fl o fm fn n x do dp dq"><svg width="33"height="33" viewBox="0 0 33 33"><pathd="M28.86 17.34l-3.64-6.4c-.3-.43-.71-.73-1.16-.8a1.12 1.12 0 0 0-.9.21c-.62.5-.73 1.18-.32 2.06l1.22 2.6 1.4 2.45c2.23 4.09 1.51 8-2.15 11.66a9.6 9.6 0 0 1-.8.71 6.53 6.53 0 0 0 4.3-2.1c3.82-3.82 3.57-7.87 2.05-10.39zm-6.25 11.08c3.35-3.35 4-6.78 1.98-10.47L21.2 12c-.3-.43-.71-.72-1.16-.8a1.12 1.12 0 0 0-.9.22c-.62.49-.74 1.18-.32 2.06l1.72 3.63a.5.5 0 0 1-.81.57l-8.91-8.9a1.33 1.33 0 0 0-1.89 1.88l5.3 5.3a.5.5 0 0 1-.71.7l-5.3-5.3-1.49-1.49c-.5-.5-1.38-.5-1.88 0a1.34 1.34 0 0 0 0 1.89l1.49 1.5 5.3 5.28a.5.5 0 0 1-.36.86.5.5 0 0 1-.36-.15l-5.29-5.29a1.34 1.34 0 0 0-1.88 0 1.34 1.34 0 0 0 0 1.89l2.23 2.23L9.3 21.4a.5.5 0 0 1-.36.85.5.5 0 0 1-.35-.14l-3.32-3.33a1.33 1.33 0 0 0-1.89 0 1.32 1.32 0 0 0-.39.95c0 .35.14.69.4.94l6.39 6.4c3.53 3.53 8.86 5.3 12.82 1.35zM12.73 9.26l5.68 5.68-.49-1.04c-.52-1.1-.43-2.13.22-2.89l-3.3-3.3a1.34 1.34 0 0 0-1.88 0 1.33 1.33 0 0 0-.4.94c0 .22.07.42.17.61zm14.79 19.18a7.46 7.46 0 0 1-6.41 2.31 7.92 7.92 0 0 1-3.67.9c-3.05 0-6.12-1.63-8.36-3.88l-6.4-6.4A2.31 2.31 0 0 1 2 19.72a2.33 2.33 0 0 1 1.92-2.3l-.87-.87a2.34 2.34 0 0 1 0-3.3 2.33 2.33 0 0 1 1.24-.64l-.14-.14a2.34 2.34 0 0 1 0-3.3 2.39 2.39 0 0 1 3.3 0l.14.14a2.33 2.33 0 0 1 3.95-1.24l.09.09c.09-.42.29-.83.62-1.16a2.34 2.34 0 0 1 3.3 0l3.38 3.39a2.17 2.17 0 0 1 1.27-.17c.54.08 1.03.35 1.45.76.1-.55.41-1.03.9-1.42a2.12 2.12 0 0 1 1.67-.4 2.8 2.8 0 0 1 1.85 1.25l3.65 6.43c1.7 2.83 2.03 7.37-2.2 11.6zM13.22.48l-1.92.89 2.37 2.83-.45-3.72zm8.48.88L19.78.5l-.44 3.7 2.36-2.84zM16.5 3.3L15.48 0h2.04L16.5 3.3z"fill-rule="evenodd"></path></svg></div></div></a></div><div class="dr l"><div class="ds"><h4 class="br dt du bt fo"><button class="bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq">416claps</button></h4></div></div></div><div class="ar as"><div class="fp l ba g"><ahref="//medium.com/p/ac4ec8aee7d1/share/twitter?source=follow_footer--------------------------follow_footer-"class="bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq"><svg width="29" height="29" class="ax"><pathd="M22.05 7.54a4.47 4.47 0 0 0-3.3-1.46 4.53 4.53 0 0 0-4.53 4.53c0 .35.04.7.08 1.05A12.9 12.9 0 0 1 5 6.89a5.1 5.1 0 0 0-.65 2.26c.03 1.6.83 2.99 2.02 3.79a4.3 4.3 0 0 1-2.02-.57v.08a4.55 4.55 0 0 0 3.63 4.44c-.4.08-.8.13-1.21.16l-.81-.08a4.54 4.54 0 0 0 4.2 3.15 9.56 9.56 0 0 1-5.66 1.94l-1.05-.08c2 1.27 4.38 2.02 6.94 2.02 8.3 0 12.86-6.9 12.84-12.85.02-.24 0-.43 0-.65a8.68 8.68 0 0 0 2.26-2.34c-.82.38-1.7.62-2.6.72a4.37 4.37 0 0 0 1.95-2.51c-.84.53-1.81.9-2.83 1.13z"></path></svg></a></div><div class="fp l ba g"><ahref="//medium.com/p/ac4ec8aee7d1/share/facebook?source=follow_footer--------------------------follow_footer-"class="bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq"><svg width="29" height="29" class="ax"><pathd="M23.2 5H5.8a.8.8 0 0 0-.8.8V23.2c0 .44.35.8.8.8h9.3v-7.13h-2.38V13.9h2.38v-2.38c0-2.45 1.55-3.66 3.74-3.66 1.05 0 1.95.08 2.2.11v2.57h-1.5c-1.2 0-1.48.57-1.48 1.4v1.96h2.97l-.6 2.97h-2.37l.05 7.12h5.1a.8.8 0 0 0 .79-.8V5.8a.8.8 0 0 0-.8-.79"></path></svg></a></div><div class="fp aw az"><div class="cm"><button class="bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq"><svg width="25"height="25" class="ax"><g fill-rule="evenodd"><pathd="M15.6 5a.42.42 0 0 0 .17-.3.42.42 0 0 0-.12-.33l-2.8-2.79a.5.5 0 0 0-.7 0l-2.8 2.8a.4.4 0 0 0-.1.32c0 .12.07.23.16.3h.02a.45.45 0 0 0 .57-.04l2-2V10c0 .28.23.5.5.5s.5-.22.5-.5V2.93l2.02 2.02c.08.07.18.12.3.13.11.01.21-.02.3-.08v.01"></path><pathd="M18 7h-1.5a.5.5 0 0 0 0 1h1.6c.5 0 .9.4.9.9v10.2c0 .5-.4.9-.9.9H6.9a.9.9 0 0 1-.9-.9V8.9c0-.5.4-.9.9-.9h1.6a.5.5 0 0 0 .35-.15A.5.5 0 0 0 9 7.5a.5.5 0 0 0-.15-.35A.5.5 0 0 0 8.5 7H7a2 2 0 0 0-2 2v10c0 1.1.9 2 2 2h11a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2"></path></g></svg></button></div></div><div class="fp l ba"><div><div class="cm"><ahref="https://medium.com/m/signin?operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40JakobUlbrich%2Fflag-attributes-in-android-how-to-use-them-ac4ec8aee7d1&source=post_actions_footer--------------------------bookmark_sidebar-"class="bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq"><svg width="25" height="25"viewBox="0 0 25 25"><pathd="M19 6a2 2 0 0 0-2-2H8a2 2 0 0 0-2 2v14.66h.01c.01.1.05.2.12.28a.5.5 0 0 0 .7.03l5.67-4.12 5.66 4.13a.5.5 0 0 0 .71-.03.5.5 0 0 0 .12-.29H19V6zm-6.84 9.97L7 19.64V6a1 1 0 0 1 1-1h9a1 1 0 0 1 1 1v13.64l-5.16-3.67a.49.49 0 0 0-.68 0z"fill-rule="evenodd"></path></svg></a></div></div></div><div class="cm"><div class="l ba"><button class="bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq"><svg width="25"height="25" viewBox="-480.5 272.5 21 21" class="ax"><pathd="M-463 284.6c.9 0 1.6-.7 1.6-1.6s-.7-1.6-1.6-1.6-1.6.7-1.6 1.6.7 1.6 1.6 1.6zm0 .9c-1.4 0-2.5-1.1-2.5-2.5s1.1-2.5 2.5-2.5 2.5 1.1 2.5 2.5-1.1 2.5-2.5 2.5zm-7-.9c.9 0 1.6-.7 1.6-1.6s-.7-1.6-1.6-1.6-1.6.7-1.6 1.6.7 1.6 1.6 1.6zm0 .9c-1.4 0-2.5-1.1-2.5-2.5s1.1-2.5 2.5-2.5 2.5 1.1 2.5 2.5-1.1 2.5-2.5 2.5zm-7-.9c.9 0 1.6-.7 1.6-1.6s-.7-1.6-1.6-1.6-1.6.7-1.6 1.6.7 1.6 1.6 1.6zm0 .9c-1.4 0-2.5-1.1-2.5-2.5s1.1-2.5 2.5-2.5 2.5 1.1 2.5 2.5-1.1 2.5-2.5 2.5z"></path></svg></button></div></div></div></div><div class="fq fr fs eg l u"><div class="ft fu l dg"><span class="l fv au fw"><div class="l fn fx fy"><ahref="/@JakobUlbrich?source=follow_footer--------------------------follow_footer-"><imgalt="Jakob Ulbrich" src="https://miro.medium.com/fit/c/160/160/1*50pfRWptJGHYxe-dQX_pMg.png"class="l es fz ga" width="80" height="80" /></a></div><span class="l"><div class="gb l gc"><p class="br dt ef bt bw gd ge">Written by</p></div><div class="gb gf ar gc"><div class="x ar as ep"><h2 class="br gg gh gi fo"><a class="bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq"href="/@JakobUlbrich?source=follow_footer--------------------------follow_footer-">JakobUlbrich</a></h2><div class="l g"><buttonclass="gj fo ax cg gk gl gm gn bk bp go gp gq gr gs gt cj br b bs bt bu bv ck cl y cm cn bn">Follow</button></div></div></div></span></span><div class="gb gu l gc az"><div class="gv l"><h4 class="br dt gw gx bw"></h4></div><div class="aw gy az"><buttonclass="gj fo ax cg gk gl gm gn bk bp go gp gq gr gs gt cj br b bs bt bu bv ck cl y cm cn bn">Follow</button></div></div></div></div><div class="gz fr l u"><ahref="https://medium.com/p/ac4ec8aee7d1/responses/show?source=follow_footer--------------------------follow_footer-"class="bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq"><div class="ha hb ek l hc az"><span class="bz">See responses (5)</span></div></a></div></div></div><div class="hd l he u"><div class="ar dx"><div class="dy dz ea eb ec hf x"></div></div></div></div></div><div class="hg l hh hi"><section class="v w x y l z ab ac ae af ag ah ai aj ak al am an ao ap"><div class="hj hk ft ar ep g"><div class="hl ar ep"><div class="hm l hn"><div class="ho l"><ahref="https://medium.com/about?autoplay=1&source=post_page-----ac4ec8aee7d1----------------------"class="bb bc bd be bf bg bh bi bj bk hp hq bn bo hr hs"><h4 class="ht hu hv br gg bs gx hw hx l">Discover</a></div><span class="br b bs bt bu bv l hy hz">Welcome to a place where words matter. Onhref="https://medium.com/about?autoplay=1&source=post_page-----ac4ec8aee7d1----------------------"class="bb bc bd be bf bg bh bi bj bk bn bo hr hs ia">Watch</a></span></div><div class="hm l hn"><div class="ib l"><ahref="https://medium.com/topics?source=post_page-----ac4ec8aee7d1----------------------"class="bb bc bd be bf bg bh bi bj bk hp hq bn bo hr hs"><h4 class="ht hu hv br gg bs gx hw hx l">Make</a></div><span class="br b bs bt bu bv l hy hz">Follow all the topics you care about, and we’lldeliver the best stories for you to your homepage and inbox.class="bb bc bd be bf bg bh bi bj bk bn bo hr hs ia">Explore</a></span></div><div class="hm l hn"><div class="ho l"><ahref="https://medium.com/membership?source=post_page-----ac4ec8aee7d1----------------------"class="bb bc bd be bf bg bh bi bj bk hp hq bn bo hr hs"><h4 class="ht hu hv br gg bs gx hw hx l">Become a member</h4></a></div><span class="br b bs bt bu bv l hy hz">Get unlimited access to the best stories onhref="https://medium.com/membership?source=post_page-----ac4ec8aee7d1----------------------"class="bb bc bd be bf bg bh bi bj bk bn bo hr hs ia">Upgrade</a></span></div></div></div><div class="ar as ep"><a href="https://medium.com/?source=post_page-----ac4ec8aee7d1----------------------"class="bb bc bd be bf bg bh bi bj bk hp hq bn bo hr hs"><svg height="22" width="112"viewBox="0 0 111.5 22" class="hu"><pathd="M56.3 19.5c0 .4 0 .5.3.7l1.5 1.4v.1h-6.5V19c-.7 1.8-2.4 3-4.3 3-3.3 0-5.8-2.6-5.8-7.5 0-4.5 2.6-7.6 6.3-7.6 1.6-.1 3.1.8 3.8 2.4V3.2c0-.3-.1-.6-.3-.7l-1.4-1.4V1l6.5-.8v19.3zm-4.8-.8V9.5c-.5-.6-1.2-.9-1.9-.9-1.6 0-3.1 1.4-3.1 5.7 0 4 1.3 5.4 3 5.4.8.1 1.6-.3 2-1zm9.1 3.1V9.4c0-.3-.1-.6-.3-.7l-1.4-1.5v-.1h6.5v12.5c0 .4 0 .5.3.7l1.4 1.4v.1h-6.5zm-.2-19.2C60.4 1.2 61.5 0 63 0c1.4 0 2.6 1.2 2.6 2.6S64.4 5.3 63 5.3a2.6 2.6 0 0 1-2.6-2.7zm22.5 16.9c0 .4 0 .5.3.7l1.5 1.4v.1h-6.5v-3.2c-.6 2-2.4 3.4-4.5 3.4-2.9 0-4.4-2.1-4.4-6.2 0-1.9 0-4.1.1-6.5 0-.3-.1-.5-.3-.7L67.7 7v.1H74v8c0 2.6.4 4.4 2 4.4.9-.1 1.7-.6 2.1-1.3V9.5c0-.3-.1-.6-.3-.7l-1.4-1.5v-.2h6.5v12.4zm22 2.3c0-.5.1-6.5.1-7.9 0-2.6-.4-4.5-2.2-4.5-.9 0-1.8.5-2.3 1.3.2.8.3 1.7.3 2.5 0 1.8-.1 4.2-.1 6.5 0 .3.1.5.3.7l1.5 1.4v.1H96c0-.4.1-6.5.1-7.9 0-2.7-.4-4.5-2.2-4.5-.9 0-1.7.5-2.2 1.3v9c0 .4 0 .5.3.7l1.4 1.4v.1h-6.5V9.5c0-.3-.1-.6-.3-.7l-1.4-1.5v-.2h6.5v3.1a4.6 4.6 0 0 1 4.6-3.4c2.2 0 3.6 1.2 4.2 3.5.7-2.1 2.7-3.6 4.9-3.5 2.9 0 4.5 2.2 4.5 6.2 0 1.9-.1 4.2-.1 6.5-.1.3.1.6.3.7l1.4 1.4v.1h-6.6zm-81.4-2l1.9 1.9v.1h-9.8v-.1l2-1.9c.2-.2.3-.4.3-.7V7.3c0-.5 0-1.2.1-1.8L11.4 22h-.1L4.5 6.8c-.1-.4-.2-.4-.3-.6v10c-.1.7 0 1.3.3 1.9l2.7 3.6v.1H0v-.1L2.7 18c.3-.6.4-1.3.3-1.9v-11c0-.5-.1-1.1-.5-1.5L.7 1.1V1h7l5.8 12.9L18.6 1h6.8v.1l-1.9 2.2c-.2.2-.3.5-.3.7v15.2c0 .2.1.5.3.6zm7.6-5.9c0 3.8 1.9 5.3 4.2 5.3 1.9.1 3.6-1 4.4-2.7h.1c-.8 3.7-3.1 5.5-6.5 5.5-3.7 0-7.2-2.2-7.2-7.4 0-5.5 3.5-7.6 7.3-7.6 3.1 0 6.4 1.5 6.4 6.2v.8h-8.7zm0-.8h4.3v-.8c0-3.9-.8-4.9-2-4.9-1.4.1-2.3 1.6-2.3 5.7z"></path></svg></a><span class="br b bs bt bu bv l hy hz"><div class="ic id ar ep ie au"><ahref="https://medium.com/about?autoplay=1&source=post_page-----ac4ec8aee7d1----------------------"class="bb bc bd be bf bg bh bi bj bk if bn bo hr hs">About</a><ahref="https://help.medium.com/?source=post_page-----ac4ec8aee7d1----------------------"class="bb bc bd be bf bg bh bi bj bk if bn bo hr hs">Help</a><aclass="bb bc bd be bf bg bh bi bj bk if bn bo hr hs"href="/policy/9db0094a1e0f?source=post_page-----ac4ec8aee7d1----------------------">Legal</a></div></span></div></section></div></div></div></body></html> |