Dialogs, Toast, Popup & Fragments in Android in Hindi
Android में Dialogs, Toasts, Popups और Fragments का उपयोग यूजर इंटरफेस (UI) को बेहतर बनाने और यूजर इंटरैक्शन को बढ़ाने के लिए किया जाता है। आइए इन सभी को विस्तार से समझते हैं।
1. Dialogs in Android
- Dialogs एक छोटा UI Popup Window होता है, जो यूजर से इनपुट लेने या कोई सूचना देने के लिए उपयोग किया जाता है।
- यह बैकग्राउंड स्क्रीन के ऊपर दिखाई देता है और यूजर को इंटरैक्शन के लिए बाध्य करता है।
Types of Dialogs:
- AlertDialog - User को notification या confirmation दिखाने के लिए।
- ProgressDialog - एक loading screen दिखाने के लिए।
- DatePickerDialog - User से Date इनपुट लेने के लिए।
- TimePickerDialog - User से Time इनपुट लेने के लिए।
- Custom Dialog - अपने अनुसार design किया गया dialog.
Implementation of AlertDialog
Step 1: Open AlertDialog on Button Click
Button btnAlert = findViewById(R.id.btn_alert);
btnAlert.setOnClickListener(v -> {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Exit Confirmation")
.setMessage("Are you sure you want to exit?")
.setPositiveButton("Yes", (dialog, which) -> finish())
.setNegativeButton("No", (dialog, which) -> dialog.dismiss())
.show();
});
2. Toast in Android
- Toast एक छोटा सा Popup Message होता है, जो कुछ सेकंड के लिए स्क्रीन पर दिखता है और फिर अपने आप गायब हो जाता है।
- इसका उपयोग Quick Notifications देने के लिए किया जाता है, जैसे "Message Sent", "Form Submitted", "No Internet Connection" आदि।
Features of Toast:
- Auto-Dismiss (कुछ सेकंड में गायब हो जाता है)।
- No User Interaction Required (यूजर को कोई बटन क्लिक नहीं करना पड़ता)।
- Custom Positioning (Bottom, Center, Top)।
- Custom Design & Styling किया जा सकता है।
Implementation of Toast
Button btnToast = findViewById(R.id.btn_toast);
btnToast.setOnClickListener(v -> {
Toast.makeText(getApplicationContext(), "This is a Toast Message!", Toast.LENGTH_SHORT).show();
});
3. Popup in Android
- Popup Window एक छोटा सा Floating Window होता है, जो स्क्रीन के किसी भी हिस्से में दिखाई दे सकता है।
- इसे Context Menu, Tooltips, Custom Message Box, Drop-downs आदि के रूप में उपयोग किया जाता है।
Types of Popup Windows:
- Popup Menu - यह एक छोटा सा Menu List दिखाता है।
- Popup Window - यह एक custom popup बनाता है जिसमें TextView, Buttons, Images आदि हो सकते हैं।
Implementation of Popup Menu
Step 1: Define Menu in res/menu/popup_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item1" android:title="Option 1"/>
<item android:id="@+id/item2" android:title="Option 2"/>
</menu>
Step 2: Show Popup Menu on Button Click
Button btnPopup = findViewById(R.id.btn_popup);
btnPopup.setOnClickListener(v -> {
PopupMenu popup = new PopupMenu(this, btnPopup);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(item -> {
Toast.makeText(this, "Selected: " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
});
popup.show();
});
4. Fragments in Android
- Fragments एक Activity के छोटे-छोटे हिस्से (Modular UI Components) होते हैं।
- ये एक तरह के Reusable UI Components होते हैं, जिन्हें Multiple Activities में Use किया जा सकता है।
- जब कोई Activity Destroy होती है, तब भी Fragments Manage & Retain हो सकते हैं।
Features of Fragments:
- Multiple Fragments in One Activity.
- Dynamic UI Changes.
- Better Memory Management.
- Lifecycle Independent of Activity.
Implementation of Fragments
Step 1: Create a Fragment Class
public class MyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout, container, false);
}
}
Step 2: Define Fragment Layout (res/layout/fragment_layout.xml)
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello from Fragment!"/>
Step 3: Load Fragment in Activity
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_container, new MyFragment());
transaction.commit();
Step 4: Add a Fragment Container in Activity XML
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Page Views
0
Visit ID
Not tracked yet
Visit Time
-
Time on Page
0 seconds
Request
Leave a Comment
INITIALIZING...
INITIALIZING...
Quizzes in Hindi
GK Online Quiz in Hindi
10 pages
HTML Online Quiz in Hindi
10 pages
CSS Online Quiz in Hindi
10 pages
JavaScript Online Quiz in Hindi
10 pages
CCC Online Quiz in Hindi
10 pages
Comments Total: 0 comments