Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android में फुल स्क्रीन कस्टम डायलॉग कैसे बनाएं?


उद्देश्य

यह उदाहरण दर्शाता है कि Android में फुल स्क्रीन कस्टम डायलॉग कैसे बनाएं।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।

चरण 2: लेआउट XML जोड़ें

निम्न कोड को res/layout/activity_main.xml में जोड़ें:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center"
    android:orientation="vertical">
    <Button
        android:id="@+id/customDialog"
        android:text="Custom Dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

ऊपर के कोड में, हमने एक बटन लिया है। जब यूजर इस पर क्लिक करता है, तो यह कस्टम डायलॉग दिखाएगा।

चरण 3: MainActivity जावा कोड जोड़ें

निम्न कोड को src/MainActivity.java में जोड़ें:

package com.example.andy.myapplication;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.customDialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Rect displayRectangle = new Rect();
                Window window = MainActivity.this.getWindow();
                window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
                
                final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.CustomAlertDialog);
                ViewGroup viewGroup = findViewById(android.R.id.content);
                View dialogView = LayoutInflater.from(v.getContext()).inflate(R.layout.customview, viewGroup, false);
                
                dialogView.setMinimumWidth((int)(displayRectangle.width() * 1f));
                dialogView.setMinimumHeight((int)(displayRectangle.height() * 1f));
                
                builder.setView(dialogView);
                final AlertDialog alertDialog = builder.create();
                
                Button buttonOk = dialogView.findViewById(R.id.buttonOk);
                buttonOk.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.dismiss();
                    }
                });
                
                alertDialog.show();
            }
        });
    }
}

कस्टम व्यू लेआउट (customview.xml)

कस्टम डायलॉग दिखाने के लिए हमने एक व्यू को कस्टम व्यू के रूप में इन्फ्लेट किया है। res/layout/customview.xml बनाएं और निम्न कोड जोड़ें:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Success"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla eu erat tincidunt lacus fermentum rutrum."
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
        <Button
            android:id="@+id/buttonOk"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="15dp"
            android:background="@color/colorPrimary"
            android:text="Ok"
            android:textColor="#FFF" />
    </LinearLayout>
</LinearLayout>

कस्टम थीम (res/styles.xml)

ऊपर के कोड में, हमने अलर्ट डायलॉग के लिए कस्टम थीम इम्प्लीमेंट किया है:

final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.CustomAlertDialog);

अब res/styles.xml में एक टैग बनाएं:

<style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:windowBackground">@drawable/popup_background</item>
</style>

ड्रॉएबल बैकग्राउंड (drawable/popup_background.xml)

ऊपर के कोड में हमने बैकग्राउंड के रूप में popup_background एड किया है, इसलिए drawable फोल्डर में popup_background.xml बनाएं:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF" />
    <corners android:radius="6dp" />
</shape>

फुल स्क्रीन डायलॉग कोड समझाया गया

Rect displayRectangle = new Rect();
Window window = MainActivity.this.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.CustomAlertDialog);
ViewGroup viewGroup = findViewById(android.R.id.content);
View dialogView = LayoutInflater.from(v.getContext()).inflate(R.layout.customview, viewGroup, false);
dialogView.setMinimumWidth((int)(displayRectangle.width() * 1f));
dialogView.setMinimumHeight((int)(displayRectangle.height() * 1f));
builder.setView(dialogView);
final AlertDialog alertDialog = builder.create();
Button buttonOk = dialogView.findViewById(R.id.buttonOk);
buttonOk.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        alertDialog.dismiss();
    }
});
alertDialog.show();

ऐप चलाएं

अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिफ़ॉल्ट स्क्रीन देखें:

Android में फुल स्क्रीन कस्टम डायलॉग कैसे बनाएं?

ऊपर के परिणाम में प्रारंभिक स्क्रीन दिखाई गई है। अब बटन पर क्लिक करें, यह फुल स्क्रीन कस्टम डायलॉग खोलेगा:

Android में फुल स्क्रीन कस्टम डायलॉग कैसे बनाएं?

महत्वपूर्ण नोट्स

  • getWindowVisibleDisplayFrame() से स्क्रीन डाइमेंशन्स प्राप्त होते हैं।
  • setMinimumWidth/Height() से डायलॉग को फुल स्क्रीन बनाया जाता है।
  • कस्टम थीम से डिफ़ॉल्ट डायलॉग मार्जिन्स और पैडिंग हटाए जाते हैं।
  • आधुनिक Android के लिए DialogFragment या BottomSheetDialog उपयोग करने पर विचार करें।
  1. एंड्रॉइड में डायलॉग की स्क्रीन पोजीशन कैसे बदलें? एंड्रॉइड में डायलॉग की स्क्रीन पोजीशन कैसे बदलें?

    एंड्रॉइड में AlertDialog की पोजीशन सेट करना यह उदाहरण दिखाता है कि WindowManager.LayoutParams और Gravity का उपयोग करके डायलॉग को स्क्रीन पर किसी भी स्थान पर कैसे प्रदर्शित किया जाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं। चरण 2: लेआउट XML (खा

  1. Android में अलर्ट डायलॉग को स्क्रीन का 50% आकार कैसे बनाएं? Android में अलर्ट डायलॉग को स्क्रीन का 50% आकार कैसे बनाएं?

    Android में अलर्ट डायलॉग की चौड़ाई/ऊंचाई कस्टमाइज करने का तरीका डिफ़ॉल्ट रूप से अलर्ट डायलॉग स्क्रीन की चौड़ाई के 80-90% तक होता है। यह उदाहरण दिखाता है कि WindowManager.LayoutParams का उपयोग करके डायलॉग को स्क्रीन के 50% चौड़ाई और ऊंचाई तक कैसे सीमित किया जाए। चरण 1: नया प्रोजेक्ट बनाएं Android St

  1. Android में GridLayout को स्क्रीन साइज़ में फिट कैसे करें? Android में GridLayout को स्क्रीन साइज़ में फिट कैसे करें?

    यह उदाहरण दर्शाता है कि GridLayout को प्रोग्रामेटिकली डायनामिक कॉलम/रो काउंट के साथ स्क्रीन साइज़ में फिट कैसे किया जाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project। चरण 2: लेआउट XML res/layout/activity_main.xml: <?xml version=1.0 encoding=utf-8?> <GridLayout