Menu

Related Topics

HTML CSS JavaScript PHP Node.js React.js C C++ Go Rust Swift Python Java Excel MS Word PowerPoint
Search

Dialogs, Toast, Popup & Fragments in Android in Hindi

Android में Dialogs, Toasts, Popups और Fragments का उपयोग यूजर इंटरफेस (UI) को बेहतर बनाने और यूजर इंटरैक्शन को बढ़ाने के लिए किया जाता है। आइए इन सभी को विस्तार से समझते हैं।

1. Dialogs in Android

Types of Dialogs:

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

Features of Toast:

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

Types of Popup Windows:

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

Features of Fragments:

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
History Online Quiz in Hindi 10 pages



Comments Total: 0 comments

📰 Latest Articles

Total: 0 Articles

Featured Products

0 Products 0 Loaded 0 Remaining

All Products Loaded!

You've reached the end of our collection.

Contact Us Form