Welcome to

w3study.github.io




Topics

Dialog

Toast

Popup

Fragments

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

java

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

java

Button btnToast = findViewById(R.id.btn_toast);
btnToast.setOnClickListener(v -> {
    Toast.makeText(getApplicationContext(), "This is a Toast Message!", Toast.LENGTH_SHORT).show();
});

Types of Popup Windows:

Implementation of Popup Menu

Step 1: Define Menu in res/menu/popup_menu.xml

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

java

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

java

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)

xml

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello from Fragment!"/>

Step 3: Load Fragment in Activity

java
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

    

xml

<FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="wrap_content"/>


  • Development of Android Applications Notes in Hindi


  • Request:

    हैलो दोस्तों! उम्मीद करता हूं आपको हमारा यह content/post पसंद आया होगा। अगर आपको हमारा ये content/post पसंद आई हो तो अपने दोस्तों के पास भी share करे। और अगर आपको कोई problem या कोई specific content हिन्दी में चाहिए है तो आप हमें नीचे दिए गए Email या whatsapp number के जरिए बता सकते है।

    अगर आप CCC/diploma/polytechnic/MCA/BCA etc कर रहे है तो ये website स्पेशली आपके लिए ही है, जो student हिंदी में पढ़ाई करते है।

    Contact Us

    Email: deepanshuranjan8057@gmail.com

    Whatsapp: +91 8057754706



    Follow Us

    Facebook Logo    Instagram Logo