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

Android में कस्टम डायलॉग व्यू एक्शन के साथ कस्टम डायलॉग कैसे बनाएं?

कस्टम डायलॉग व्यू एक्शन के साथ कस्टम डायलॉग

यह उदाहरण दर्शाता है कि Android में कस्टम डायलॉग व्यू एक्शन के साथ कस्टम डायलॉग कैसे बनाया जाता है।

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

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

चरण 2: 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: src/MainActivity.java में कोड जोड़ें

package com.example.andy.myapplication;

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.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) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                ViewGroup viewGroup = findViewById(android.R.id.content);
                View dialogView = LayoutInflater.from(v.getContext()).inflate(R.layout.customview, viewGroup, false);
                Button buttonOk = dialogView.findViewById(R.id.buttonOk);
                builder.setView(dialogView);
                final AlertDialog alertDialog = builder.create();
                buttonOk.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.dismiss();
                    }
                });
                alertDialog.show();
            }
        });
    }
}

चरण 4: 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>

ऐप चलाएं

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

Android में कस्टम डायलॉग व्यू एक्शन के साथ कस्टम डायलॉग कैसे बनाएं?

अब बटन पर क्लिक करें, यह कस्टम डायलॉग खोलेगा:

Android में कस्टम डायलॉग व्यू एक्शन के साथ कस्टम डायलॉग कैसे बनाएं?

अब OK बटन पर क्लिक करें, यह अलर्ट डायलॉग को बंद कर देगा।

  1. एंड्रॉइड में बटन कैसे हटाएं या अदृश्य करें? एंड्रॉइड में बटन कैसे हटाएं या अदृश्य करें?

    View.VISIBLE, INVISIBLE, GONE के साथ बटन विजिबिलिटी कंट्रोल यह उदाहरण दर्शाता है कि setVisibility() का उपयोग करके बटन को अदृश्य (INVISIBLE) या पूरी तरह से हटाएं (GONE) कैसे करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट में दो बटन res/layout/activity_mai

  1. Android में कस्टम लिस्ट व्यू में सर्च फंक्शनैलिटी कैसे जोड़ें? Android में कस्टम लिस्ट व्यू में सर्च फंक्शनैलिटी कैसे जोड़ें?

    Android में कस्टम लिस्ट व्यू में सर्च फीचर इम्प्लीमेंट करना यह ट्यूटोरियल दिखाता है कि कैसे एक कस्टम लिस्ट व्यू में सर्च फंक्शनैलिटी जोड़ी जाती है, जिससे यूजर लिस्ट आइटम्स को रियल-टाइम में फ़िल्टर कर सकते हैं। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाए

  1. Android में कस्टम AlertDialog View कैसे इम्प्लीमेंट करें? Android में कस्टम AlertDialog View कैसे इम्प्लीमेंट करें?

    Android में कस्टम AlertDialog इम्प्लीमेंट करना यह ट्यूटोरियल दिखाता है कि कैसे कस्टम लेआउट के साथ AlertDialog बनाया जाता है, जिसमें कस्टम टाइटल, आइकन, मैसेज, और बटन स्टाइलिंग शामिल है। चरण 1: प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: मेन लेआउट में बटन res/layout/activity_main.