Android Buttons

Introduction to Android Buttons

Buttons किसी भी एप्लीकेशन के लिए बहुत important element होते है। Button को click करने पर button कोई action perform करता है। इसके लिए आपको button की coding करनी होती है।

हर button का एक नाम होता है। Android buttons के लिए आप नाम की जगह कोई icon भी यूज़ कर सकते है। Android buttons का एक साथ नाम भी हो सकता है और icon भी।

Android buttons editable होते है। Android buttons को styling बनाया जा सकता है। इसके लिए आप different styles और themes यूज़ कर सकते है।

Button पर click होते ही onClick event occur होता है। हम इस event को handle करने के लिए coding करते है।

Steps to Create Android Buttons

  1. सबसे पहले आप activity_main.xml file में button add करते है।
  2. इसके बाद आप MainActivity.java file में button के click event की coding करते है। ये coding आप 2 तरह से कर सकते है।

Creating Android Buttons

Adding Button to XML File

Button का यूजर इंटरफ़ेस XML file में क्रिएट किया जाता है। इसके लिए आप <Button> element यूज़ करते है। <Button> एलिमेंट के कुछ attributes होते है, जो निचे दिए जा रहे है।

  • Android : layout_width - इस attribute के द्वारा आप button की width set कर सकते है।
  • Android : layout_height - इस attribute से आप button की height सेट करते है।
  • Android : text = “string” - इस attribute के द्वारा आप button का नाम देते है।
  • Android : src = “@drawable/button_icon” - इस attribute के द्वारा आप button के icon को सेट करते है।

Example:

<Button
Android:layout_width="width of button"
Android:layout_height="height of button"
Android:text="name of button"
Android:src="icon of button" />

Handling onClick Event

onClick event आप 2 तरह से handle कर सकते है। पहले तरीके में आप <Button> element का android:onClick attribute यूज़ करते है। इस तरीके में आप activity class में एक method क्रिएट करते है। ये method button के क्लिक होने पर कॉल होता है। इस method का नाम आप android:onClick attribute में पास करते है।

Example:

In XML file:

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

In your activity class

public void yourMethod(View view)
{
    // perform your task here
}

जब यूज़र button पर click करता है तो onClick attribute में define किया हुआ method कॉल हो जाता है।

दूसरे तरीके में आप XML यूज़ नहीं करते है। ये तरीका run time में events handle करने के लिए उपयोगी होता है। इस तरीके में आप एक button object क्रिएट करते है। और उस button object पर setOnClickListener() मेथड कॉल करते है और onClickListener सेट करते है।

यदि आप run time में event handle करना चाहते है तो ये तरीका यूज़ कर सकते है। XML file के द्वारा button क्रिएट करना सबसे easy होता है।

Designing Android Buttons

अलग अलग devices अलग अलग styles यूज़ करते है। इसलिए आपके buttons अलग अलग devices पर अलग अलग styles में शो होते है। आप अपने buttons के style को control कर सकते है। इसके लिए आप पूरी एप्लीकेशन पर एक theme apply करते है।

Android आपको android:theme attribute provide करता है। इस attribute को आप element में apply करते है। ये element AndroidManifest.xml फाइल में declared होता है।

यदि आप buttons को individually configure करना चाहते है तो आप android:background attribute यूज़ कर सकते है। इस attribute में आप कोई image भी पास कर सकते है और कोई कलर भी पास कर सकते है।