Android Layouts

Introduction to Android Layouts

जैसा की मैने आपको पहले बताया जब भी आप views क्रिएट करते है तो वो उसी order में शो होते है जिसमे आपने उन्हें क्रिएट किया है एक के बाद दूसरा। कई बार ऐसा हो सकता है की आप किसी view को screen पर एक particular position पर place करना चाहते है। ऐसा आप android layouts की मदद से कर सकते है।

Layouts यूजर इंटरफ़ेस के लिए structure प्रोवाइड करते है उस structure के according आप अपने elements स्क्रीन पर मैनेज कर सकते है। Android system कई तरह के layouts provide करता है। आपकी एप्लीकेशन के लिए जो layout आपको ठीक लगे आप उसे यूज़ कर सकते है।

Android में layouts आप 2 तरह से define कर सकते है या तो आप एक XML फाइल के द्वारा ऐसा कर सकते है या run time में view और view groups के objects क्रिएट करके ऐसा कर सकते है।

आप जो चाहे वो तरीका अपना सकते है या आप दोनों तरीके भी अपना सकते है। आप चाहे तो XML के द्वारा क्रिएट किये हुए layouts को run time में चेंज भी कर सकते है। यदि आप layouts XML फाइल के द्वारा डिक्लेअर करते है तो आप अपनी एप्लीकेशन के view को और एप्लीकेशन के behavior को अलग अलग control कर सकते है।

Attributes of Android Layouts

नीचे कुछ common attributes दिए जा रहे है जो सभी views के साथ यूज़ होते है।

  • android:id - इस attribute के द्वारा आप view की id define करते है जो view को uniquely identify करने के काम आती है।
  • android:layout_width - इस attribute के द्वारा आप view की screen पर width define करते है।
  • android:layout_height - इस attribute के द्वारा आप view की स्क्रीन पर height define करते है।

Types of Android Layouts

Linear layout

Linear layout एक ऐसा layout है जिसमे सारे elements एक ही direction में होते है या तो vertical या horizontal. आप layout का direction android:orientation attribute के द्वारा define कर सकते है।

यदि layout का direction vertical है तो एक column में एक ही एलिमेंट आएगा। और यदि layout का direction horizontal है तो एक row में एक ही element आएगा।

यदि आप चाहे तो किसी एक particular element को priority वैल्यू भी दे सकते है। इसे layout weight कहते है। ऐसा आप android:layout_weight attribute डिफाइन करके कर सकते है। जिस element का जितना weight होता है वो उतना ही space screen पर cover करता है।

यदि किसी element का weight बाकि elements से अधिक है तो सभी elements के set होने के बाद जो remaining space होगा वो element cover करेगा।

Linear layout क्रिएट करने के लिए आपको XML file में <LinearLayout> element declare करना होगा। इस element के android:layout_width, android:layout_height और android:orientation 3 attributes होते है। इन तीनो attributes के लिए values provide करके आप layout को set कर सकते है।

<LinearLayout xmlns:android="android schema address"  
android:layout_width="width of layout"
android:layout_height="height of layout"
android:orientation="layout orientation">

<Button
android:layout_width="width of button"
android:layout_height="height of button"
android:text="name of button" />

<Button
android:layout_width="width of button"
android:layout_height="height of button"
android:text="name of button" />

</LinearLayout>

Relative Layout

Relative layout view elements को relative positions पर display करता है। हर view को आप दूसरे elements के relation में position कर सकते है। किसी भी नए view की position आप existing view के relation में define करते है। जैसे की आप existing element के right में नए element को set करना चाहते है या left में। इस प्रकार आप किसी भी element को screen पर कँही भी place कर सकते है।

Relative layout views को position करने के लिए कुछ attributes provide करता है, जिनके द्वारा आप अपने view को position कर सकते है। आइये इन attributes के बारे में जानने का प्रयास करते है -

android:layout_alignParentTop

यदि किसी view element में इस attribute की value true है तो वो view आप की स्क्रीन में सबसे top पर show होगा।

android:layout_centralVertical   

यदि किसी view element में इस attribute की वैल्यू true है तो वो view आप की स्क्रीन में vertically center में show होगा।

android:layout_below

ये attribute आपके view element को उस view के नीचे position करता है जो आपने resource id के द्वारा define किया है।

android:layout_toRightOf

ये attribute आपके view element को उस view element के right में place करता है जो आपने resource id के द्वारा define किया है।

यँहा पर ये कुछ उदाहरण दिए गए है ये attributes बहुत सारे है। आप जो चाहे अपनी आवश्यकता के अनुसार इस्तेमाल कर सकते है।

<RelativeLayout xmlns:android="android schema address"  
android:layout_width="width of layout"
android:layout_height="height of layout"
android:paddingLeft="in dp"
android:paddingRight="in dp">

<Button
android:layout_width="width of button"
android:layout_height="height of button"
android:layout_below="id of other view element"
andorid:text="name of button" />

<Button
android:layout_width="width of button"
android:layout_height="height of button"
android:layout_centralVertical="id of other view element"
android:text="name of button" />

</RelativeLayout>

ListView

ListView एक view group होता है जो items की list show करता है जिन्हे आप scroll कर सकते है। ये layout run time में सभी views को मैनेज कर सकता है। इस layout में एक ही column होता है और row बहुत सी होती है। यानी आप एक row में एक ही view position कर सकते है।

Run time में आप adapter के द्वारा किसी array या database से item को pull करते है वो adapter उस item को view convert करके list के आखिर में add कर देता है।

GridView

GridView एक view group होता है तो items को two dimensional grid में शो करता है। इस grid को आप scroll कर सकते है। इस layout में items rows और columns की form में show होते है। GridView में items run time पर automatically insert हो जाते है।