M
M
MrLumuss2022-01-24 12:21:50
Kotlin
MrLumuss, 2022-01-24 12:21:50

How to display a separate layout as a popup window?

Hi all. There is a layot'a (popup window) code, how can I display it in activity_main.xml when the button is clicked?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexVWill, 2022-01-26
@MrLumuss

First, draw a Layout and call it policy_popup (for this example, or whatever you want)

spoiler
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/scrollView4"
        android:layout_width="wrap_content"
        android:layout_height="250dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/donotshow"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/linearLayout4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/policy_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginLeft="8dp"
                android:text="@string/policy_disclosure_ru"
                android:textColor="#000000"
                android:textSize="16sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>

    <CheckBox
        android:id="@+id/donotshow"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="Больше не показывать"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Second data_popup
spoiler
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/scrollView4"
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/linearLayout4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/data_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginLeft="8dp"
                android:text="@string/data_disclosure_ru"
                android:textColor="#000000"
                android:textSize="16sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

Then somewhere in the Main Activity code write
spoiler
public void policytextshow(){
        LayoutInflater li = LayoutInflater.from(this);
        View promptsView = li.inflate(R.layout.policy_popup, null);
        //Make AlertDialog
        AlertDialog.Builder mDialogBuilder = new AlertDialog.Builder(this);
        //Настраиваем .xml для нашего AlertDialog:
        mDialogBuilder.setView(promptsView);
        //Настраиваем отображение поля для ввода текста в открытом диалоге:
        //Настраиваем сообщение в диалоговом окне:
        CheckBox hidebox = (CheckBox) promptsView.findViewById(R.id.donotshow);
        mDialogBuilder
                .setCancelable(false)
                .setIcon(R.drawable.shield1)
                .setTitle("Политика информации")
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                if (hidebox.isChecked()) {
                                    SharedPreferences.Editor editor = mSettings.edit();
                                    editor.putString(APP_PREFERENCES_SHOWPOLICY, "1");
                                    editor.apply();
                                }
                                dialog.cancel();
                                // if policy alert dialog closed - work with contacts list
                                contacts();
                            }
                        });
        //Создаем AlertDialog:
        AlertDialog alertDialog = mDialogBuilder.create();
        //и отображаем его:
        alertDialog.show();

        LayoutInflater li1 = LayoutInflater.from(this);
        View promptsView1 = li1.inflate(R.layout.data_popup, null);
        //Make AlertDialog
        AlertDialog.Builder mDialogBuilder1 = new AlertDialog.Builder(this);
        //Настраиваем .xml для нашего AlertDialog:
        mDialogBuilder1.setView(promptsView1);
        //Настраиваем отображение поля для ввода текста в открытом диалоге:
        //Настраиваем сообщение в диалоговом окне:
        mDialogBuilder1
                .setCancelable(false)
                .setIcon(R.drawable.shield1)
                .setTitle("Используемые данные")
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog1,int id) {
                                if (hidebox.isChecked()) {
                                    SharedPreferences.Editor editor = mSettings.edit();
                                    editor.putString(APP_PREFERENCES_SHOWPOLICY, "1");
                                    editor.apply();
                                }
                                dialog1.cancel();
                            }
                        });
        //Создаем AlertDialog:
        AlertDialog alertDialog1 = mDialogBuilder1.create();
        //и отображаем его:
        alertDialog1.show();
    }

According to this code, first one message pops up, then, when you click on OK, the second one, then it closes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question