Hiding Title bar

તમે Activity ની મથાળાની પટ્ટી (action bar) ને કાં તો manifest/styles માં app ને NoActionBar નું theme આપીને છુપાવી શકો, કાં તો Java માં getSupportActionBar().hide() કે requestWindowFeature(Window.FEATURE_NO_TITLE) બોલાવીને, અને code વાળી રીત setContentView પહેલાં ચાલવી જ જોઈએ.

7 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

એ પટ્ટી જે તમારું મથાળું ખાઈ જાય છે

FestConnect Mobile મોટા સ્વાગતના પાટિયાથી ખૂલે છે, પણ ટોચે મૂળભૂત ભૂખરી મથાળાની પટ્ટી (action bar) બેસે છે જે app નું નામ દેખાડે છે, જગ્યા ચોરે છે, અને તમારી રચના સાથે ટકરાય છે. Splash કે પૂરા પડદાના form માટે તમને એ જોઈતી નથી.

એને છુપાવવાની બે સ્વચ્છ રીત છે: theme બદલવું (જાહેરાત આધારિત, ભલામણપાત્ર) કે Java માં એક લીટી બોલાવવી (આજ્ઞા આધારિત). બંને પરીક્ષામાં પુછાય છે, અને Java વાળીમાં સમયનો એવો નિયમ છે જે લોકોને પછાડે છે.

Theory

દુકાનનું પાટિયું કાઢી નાખો

Action bar એ દુકાનની બારી ઉપર જડેલા પાટિયા જેવી છે: રસ્તો શોધવા કામની, પણ ક્યારેક તમને દેખાડવા માટે આખી બારી જોઈએ. તમે કાં તો પાટિયા વગર આવતું દુકાનનું નમૂનારૂપ મંગાવી શકો (NoActionBar નું theme પસંદ કરો) કાં તો પછીથી પાટિયું ઉખાડી શકો (code માં છુપાવો). એ જ ખાલી ટોચ, ત્યાં પહોંચવાની બે રીત.

Follow along

મથાળાની પટ્ટી છુપાવવાની બે રીત

  1. Theme વાળી રીત (ભલામણપાત્ર) res/values/themes.xml માં Theme.MaterialComponents.DayNight.NoActionBar જેવું માતાપિતા વાપરો, કે manifest માં <activity> પર android:theme ને NoActionBar વાળા theme પર ગોઠવો.
  2. Code વાળી રીત: action bar છુપાવો onCreate માં, setContentView પછી: if (getSupportActionBar() != null) getSupportActionBar().hide();
  3. Code વાળી રીત: મથાળા વગરનું લક્ષણ requestWindowFeature(Window.FEATURE_NO_TITLE); આ setContentView(...) ની પહેલાં જ આવવું જોઈએ.
  4. એક પસંદ કરો કાયમી દેખાવ માટે theme વાળી રીત સૌથી સ્વચ્છ છે; ચાલતી વખતે બદલવા માટે code કામનો છે.

Practical

Code વાળી રીત, ક્રમમાં

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // MUST be before setContentView, or it throws
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_main);

    // alternative: hide the AppCompat action bar (after setContentView)
    if (getSupportActionBar() != null) {
        getSupportActionBar().hide();
    }
}

Think first

ક્રમનો નિયમ કેમ?

requestWindowFeature(Window.FEATURE_NO_TITLE) એ setContentView() પહેલાં બોલાવવું જ પડે. જો તમે એને પછી બોલાવો તો શું ખોટું થાય, અને ક્રમ કેમ મહત્ત્વનો છે?

Show the answer

setContentView પછી બોલાવતાં એ ભૂલ ફેંકે છે (સામગ્રીનું view ફુલાયા પછી window નાં લક્ષણો પહેલેથી બંધ થઈ ગયાં હોય છે). Window નાં લક્ષણો window પોતાનું શણગાર અને સામગ્રી બાંધે એ પહેલાં માંગવાં પડે, એટલે મથાળા વગરની નિશાની પહેલાં ગોઠવવી પડે. Theme વાળી રીત આ સમયની ઝંઝટ સાવ ટાળે છે, અને એ પણ એક કારણ છે કે એ પસંદ કરાય છે. જો તમે code વાળું લક્ષણ વાપરો, તો એ દૃશ્ય ગોઠવણની પહેલી લીટી છે, setContentView ની આગળ.

Quiz

FestConnect ની Activity માંથી મથાળાની પટ્ટી કાયમ કાઢી નાખવાની સૌથી સ્વચ્છ, ભલામણપાત્ર રીત કઈ છે?

  1. એને themes.xml કે manifest માં NoActionBar નું theme આપો
  2. setContentView પછી requestWindowFeature બોલાવો
  3. AndroidManifest.xml કાઢી નાખો
  4. Layout માંથી TextView કાઢી નાખો
Show the answer

એને themes.xml કે manifest માં NoActionBar નું theme આપો

NoActionBar નું theme વાપરવું (themes.xml માં કે manifest માં android:theme દ્વારા) એ મથાળાની પટ્ટી કાયમ કાઢવાની સ્વચ્છ, જાહેરાત આધારિત, ભલામણપાત્ર રીત છે (A). setContentView પછી requestWindowFeature બોલાવવાથી ખરેખર app તૂટે છે (B). Manifest કાઢી નાખવાથી આખી app તૂટે છે (C), અને TextView કાઢવાથી તમારી સામગ્રી જાય છે, પટ્ટી નહીં (D). કાયમી દેખાવ માટે theme; ચાલતી વખતે બદલવા માટે code.

Watch out

મથાળાની પટ્ટીના ફાંદા

1. setContentView પછી requestWindowFeature: એ ભૂલ ફેંકે છે. એ પહેલાં આવવું જ જોઈએ.

2. getSupportActionBar() એ null છે: જો theme માં પહેલેથી action bar ન હોય, તો છુપાવવા જેવું કશું નથી, એટલે hide() બોલાવતાં પહેલાં null ની ચકાસણી રાખો.

3. મથાળાની પટ્ટીને status ની પટ્ટી સાથે ગૂંચવવી: action/મથાળાની પટ્ટી app નું નામ દેખાડે છે; status ની પટ્ટી ઘડિયાળ/battery દેખાડે છે. એકને છુપાવવાથી બીજી છુપાતી નથી (એના માટે અલગ નિશાનીઓ જોઈએ).

Theory

આગળ: કઈ બાજુ ઉપર?

સ્વચ્છ ટોચ સાથે, પડદાનો પછીનો નિર્ણય છે દિશા: FestConnect નું form portrait માં બંધાયેલું રહેવું જોઈએ, કે landscape માં ફરવું જોઈએ? હવે પછીનો પાઠ manifest માં પડદાની દિશા ગોઠવે છે અને ગોઠવણ બદલાવાનું વર્તન સમજાવે છે (ફરતો પડદો તમારી Activity ને કેમ ફરી શરૂ કરી શકે છે), જે widget ના યુગનો બીજો વ્યવહારુ વિષય છે.

Summary

Key takeaways

  • મથાળાની/action ની પટ્ટી બે રીતે છુપાવો: NoActionBar નું theme (ભલામણપાત્ર) કે Java નો code.
  • Theme: themes.xml માં *.NoActionBar વાળું માતાપિતા ગોઠવો કે manifest માં <activity> પર android:theme.
  • Code: getSupportActionBar().hide(), કે requestWindowFeature(Window.FEATURE_NO_TITLE) જે setContentView ની પહેલાં ચાલવું જોઈએ.
  • getSupportActionBar() એ null હોઈ શકે (જો theme માં action bar ન હોય), એટલે hide() પહેલાં null ની ચકાસણી કરો.
  • મથાળાની/action ની પટ્ટી એ status ની પટ્ટી નથી; એકને છુપાવવાથી બીજી છુપાતી નથી.
  • Memory hook: પાટિયા વગરની દુકાન મંગાવો, કે પછીથી એને ઉખાડો.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Android Widgets (UI)

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Hiding Title bar · Mobile Application Development - 1 (option B) · Gri-Learn