Android FrameLayout – Learn with Simple Example

Android FrameLayout को केवल तब Use किया जाता है, जब हमें केवल एक Single View जैसे कि Image, Video आदि को Display करना होता है अथवा Tabbled View Create करना होता है जिसके विभिन्‍न Tabs को FrameLayout द्वारा Display किया जाता है।

FrameLayout एक सबसे सरल प्रकार का Layout है क्‍योंकि केवल इसी एक Layout में Add किए जाने वाले सभी Child UI View Controls एक दूसरे के Top पर यानी एक Stack के रूप में Place होते हैं और जो UI View Control सबसे अन्‍त में Add किया जाता है, वही सबसे Top पर दिखाई देता है।

साथ ही इस FrameLayout में यद्धपि हम जितने चाहें उतने UI View Controls Add कर सकते हैं, लेकिन सभी UI View Controls एक दूसरे को Overlap करते हुए Top-Left Corner में ही Place होते हैं। इसलिए सामान्‍यत: इस Layout को तभी Use किया जाता है, जब हमें किसी एक Single UI View Control को ही Display करना होता है और इसी वजह से सामान्‍यत: इसे किसी न किसी अन्‍य Layout ViewGroup Container के अन्‍दर Child Layout की तरह ही Use किया जाता है। हालांकि हम इसे Independent Layout की तरह भी Use कर सकते हैं।

चूंकि हम एक FrameLayout में Multiple UI View Controls को Add कर सकते हैं लेकिन इसके एक समय पर केवल एक ही UI View Control को Display करने की क्षमता के कारण यदि हमें Dynamically Different UI View Controls को Display करना हो, तो इस तरह की जरूरत को पूरा करने के लिए हमें Backend Java File को Use करना पड़ता है और उसमें Appropriate Java Codes के माध्‍यम से FrameLayout के Content को Dynamically Change/Modify करना पड़ता है।

FrameLayout का प्रयोग Screen के एक Specific Area को किसी एक Single Item को Display करने हेतु Block करने के लिए किया जाता है। इसलिए इसका प्रयोग तभी करना चाहिए जब किसी Single Child View Control के माध्‍यम से किसी Specific Type के Data या Content को Display करना हो क्‍योंकि FrameLayout को इस तरह से Organize करना ताकि वो बिना अन्‍य Child View Controls को Overlap किए हुए Different Screen Size वाले Devices के लिए जरूरत के अनुसार Automatically Scal-Up / Scale-Down यानी Adjust हो जाए, बहुत मुश्किल होता है।

हालांकि हम एक ही FrameLayout Container में Multiple Child Views को Add कर सकते हैं और उन्‍हें एक दूसरे पर Overlap होने से रोकने के लिए android:layout_gravity या android:foregroundGravity Attribute का प्रयोग करते हुए Different Child Views को FrameLayout के Different Sides/Edges व Corners में Assign कर सकते हैं। ये दोनों Attributes समान काम ही करते हैं लेकिन android:foregroundGravity उस Asset की Sides/Edges के साथ UI Views को Associate करता है, जिसे हम android:foreground Attribute में Specify करते हैं, जबकि android:layout_gravity Attribute, FrameLayout की Sides/Edges के साथ UI Views को Associate करता है।

चूंकि सभी Child Views Stacks के रूप में Draw होते हैं, इसलिए एक दूसरे के ऊपर Draw होते हैं। ऐसे में FrameLayout का Size, उसमें Contained विभिन्‍न Child Views में से सबसे बड़ी Size वाले Child View की Size + Padding के बराबर हो जाता है, फिर चाहे वह Largest UI View Control, Container में Visible हो या Invisible हो, इस बात से FrameLayout की Sizing पर कोई प्रभाव नहीं पड़ता।

जबकि जो Views GONE Mark किए जाने की वजह से Invisible हो गए हैं, FrameLayout की Sizeing के लिए उनका प्रयोग केवल उसी स्थिति में हो सकता है जबकि उसके लिए setConsiderGoneChildrenWhenMeasuring() Method को true Set किया गया हो।

इसके साथ ही जब हम XML Layout Mode में होते हैं, तब FrameLayout की Sizing के लिए उसके सभी Child View Controls की Measuring को Use किया जाए या केवल VISIBLE / INVISIBLE UI View Controls की Measuring को, इस बात को तय करने के लिए हम android:measureAllChildren Attribute को Set कर सकते हैं।

इसका Default मान false होता है। इसलिए Android System द्वारा Default रूप से FrameLayout की Sizing तय करने के लिए केवल VISIBLE / INVISIBLE UI View Controls की Sizing को ही Calculate किया जाता है। लेकिन जब हम इसका मान true Set कर सकते हैं, तो उस स्थिति में सभी Child View Controls की Sizing के आधार पर सबसे Largest UI View Control की Sizing + Padding जितनी बड़ी Size का FrameLayout बनता है, फिर चाहे Largest UI View Control Visible हो या Invisible, इस बात से कोई फर्क नहीं पड़ता।

FrameLayout का प्रयोग करते हुए हम निम्‍नानुसार एक Simple सा Layout Create कर सकते हैं और android:layout_gravity Attribute के माध्‍यम से विभिन्‍न Child Views की Location को Specify कर सकते हैं-

<?xml version="1.0" encoding="utf-8"?>
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:id="@+id/flLogin"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

<Button
 android:id="@+id/btnCenter"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:text="Center" />

<Button
 android:id="@+id/btnHCenter"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:text="H - Center" />

<Button
 android:id="@+id/btnVCenter"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_vertical"
 android:text="V - Center" />

<Button
 android:id="@+id/btnTopLeft"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="top|left"
 android:text="Top|Start or Top|Left" />

<Button
 android:id="@+id/btnBottomRight"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="bottom|right"
 android:text="Bottom|End or Bottom|Right" />

<Button
 android:id="@+id/btnTopRight"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="top|right"
 android:text="Top|End or Top|Right" />

<Button
 android:id="@+id/btnBottomLeft"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="bottom|left"
 android:text="Bottom|Start or Bottom|Left" />

</FrameLayout>

और जब हम इस Layout Activity को Run करते हैं, तो हमें मिलने वाला Resultant Output निम्‍नानुसार होता है-

Android FrameLayout - Simple Example - ITeBooks in Hindi

इस प्रकार से हम FrameLayout का प्रयोग करते हुए भी अपनी किसी Specific प्रकार की जरूरत को पूरा करने के लिए Appropriate Layout Create कर सकते हैं। चूंकि, Tabbed Layout बनाने के लिए हमें FrameLayout की जरूरत Compulsory रूप से पड़ती है, इसलिए जब हम Tabbed View Create करेंगे, तब FrameLayout को Practically किस तरह से Use किया जाता है, इस विषय पर विस्‍तृत चर्चा करेंगे।

Android GridLayout - Learn with Simple Example
Android ConstraintLayout

Android in Hindi - BccFalna.comये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook Android in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी है, तो निश्चित रूप से ये EBook भी आपके लिए काफी उपयोगी साबित होगी।

Android in Hindi | Page: 628 | Format: PDF

BUY NOW GET DEMO REVIEWS

MidRangeHub Finance Auto Loans Bank Business Health Insurance Insurance Investment Law Lif Insurance Loan Stocks how to endorse a check chase sapphire travel insurance chase sapphire travel delay when are property taxes due Tower Loans how to sell stocks on cash app Voided Check Examples Personal Finance Books Collateral Loans how to sell stocks on cashapp how do you sell your stocks on cash app how to sell stock on cash app joint account sofi joint account ally joint account capital one joint account best bank for joint account chase joint account cyber insurance coverage silverfort free cyber insurance coverage silverfort monjouro savings card Money6x Real Estate