Theory
Bar जो आपका header खा रहा है
FestConnect Mobile एक बड़े welcome banner से खुलता है, पर top के आर-पार default grey title bar (action bar) बैठा है जो app का नाम दिखाता है, space चुराता हुआ और आपके design से टकराता हुआ। एक splash या एक full-screen form के लिए, आप चाहते हैं यह चला जाए।
इसे hide करने के दो साफ़ तरीक़े हैं: theme बदलिए (declarative, recommended) या Java में एक line call कीजिए (imperative)। दोनों examinable हैं, और Java वाले में एक timing rule है जो लोगों को trip कराता है।
Theory
Shop का signboard हटाइए
Action bar एक shop की window के ऊपर fixed bolt किए एक signboard जैसा है: wayfinding के लिए useful, पर कभी-कभी आप पूरी window एक display के लिए चाहते हैं। आप या तो एक shopfront model order कर सकते हैं जो board के बिना आती है (एक NoActionBar theme चुनिए) या बाद में board unscrew कर सकते हैं (code में इसे hide कीजिए)। Same खाली top, वहाँ पहुँचने के दो तरीक़े।
Follow along
Title bar hide करने के दो तरीक़े
- Theme तरीक़ा (recommended) res/values/themes.xml में Theme.MaterialComponents.DayNight.NoActionBar जैसा parent इस्तेमाल कीजिए, या manifest में <activity> पर android:theme को एक NoActionBar theme set कीजिए।
- Code तरीक़ा: action bar hide कीजिए onCreate में, setContentView के बाद: if (getSupportActionBar() != null) getSupportActionBar().hide();
- Code तरीक़ा: no-title feature requestWindowFeature(Window.FEATURE_NO_TITLE); यह setContentView(...) से PEHLE आना ज़रूरी है।
- एक चुनिए एक permanent look के लिए theme approach सबसे साफ़ है; code runtime पर toggle करने में handy है।
Practical
Code तरीक़ा, order में
@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
Ordering rule क्यों?
requestWindowFeature(Window.FEATURE_NO_TITLE) को setContentView() से पहले call करना ज़रूरी है। अगर आप इसे बाद में call करें तो क्या ग़लत होता है, और order क्यों matter करता है?
Show the answer
setContentView के बाद call होने पर, यह एक error throw करता है (window की features एक बार content view inflate होने पर lock हो जाती हैं)। Window features को window अपना decor और content बनाने से पहले request करना ज़रूरी है, तो no-title flag पहले set होना है। Theme approach इस timing को पूरी तरह avoid करता है, जो इसे preferred होने का एक कारण है। अगर आप code feature call इस्तेमाल करते हैं, यह visual setup की पहली line है, setContentView से आगे।
Quiz
एक FestConnect Activity से permanently title bar हटाने का सबसे साफ़, recommended तरीक़ा कौन सा है?
- इसे themes.xml या manifest में एक NoActionBar theme दीजिए
- setContentView के बाद requestWindowFeature call कीजिए
- AndroidManifest.xml delete कीजिए
- Layout से TextView हटाइए
Show the answer
इसे themes.xml या manifest में एक NoActionBar theme दीजिए
एक NoActionBar theme इस्तेमाल करना (themes.xml में या manifest में android:theme के ज़रिए) title bar को हमेशा के लिए हटाने का साफ़, declarative, recommended तरीक़ा है (A)। setContentView के बाद requestWindowFeature call करना actually crash करता है (B)। Manifest delete करना पूरी app तोड़ देता है (C), और TextView हटाना आपका content मिटाता है, bar नहीं (D)। Permanent look के लिए theme; runtime पर toggle के लिए code।
Watch out
Title-bar traps
1. setContentView के बाद requestWindowFeature: throw करता है। यह पहले आना ज़रूरी है।
2. getSupportActionBar() null है: अगर theme में पहले से कोई action bar नहीं है, hide करने को कुछ नहीं है, तो hide() call करने से पहले एक null check से guard कीजिए।
3. Title bar को status bar से confuse करना: action/title bar app का नाम दिखाता है; status bar clock/battery दिखाता है। एक hide करना दूसरे को hide नहीं करता (उसे अलग flags चाहिए)।
Theory
अगला: कौन सी तरफ़ ऊपर?
एक साफ़ top के साथ, अगला screen decision है orientation: FestConnect form portrait से lock होना चाहिए, या landscape में rotate होना चाहिए? अगला lesson manifest में screen orientation set करता है और configuration-change behaviour explain करता है (एक rotating screen आपकी Activity को क्यों restart कर सकती है), एक और practical widget-era topic।
Summary
Key takeaways
- Title/action bar दो तरीक़ों से hide कीजिए: एक NoActionBar theme (recommended) या Java code।
- Theme: themes.xml में एक *.NoActionBar parent set कीजिए या manifest में <activity> पर android:theme।
- Code: getSupportActionBar().hide(), या requestWindowFeature(Window.FEATURE_NO_TITLE) जो setContentView से PEHLE चलना ज़रूरी है।
- getSupportActionBar() null हो सकता है (अगर theme में कोई action bar नहीं), तो hide() से पहले null-check कीजिए।
- Title/action bar status bar नहीं है; एक hide करना दूसरे को hide नहीं करता।
- Memory hook: बिना board वाला shopfront order कीजिए, या इसे बाद में unscrew कीजिए।