@MrLumuss

Как вывести отдельный layout в виде всплывающего окна?

Привет всем. Есть код layot'a (всплывающего окна), как мне его вывести в activity_main.xml при нажатии на кнопку?
  • Вопрос задан
  • 289 просмотров
Решения вопроса 1
@AlexVWill
Сначала нарисовать Layout и зазвать его policy_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="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>


Второй 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>

Потом где то в коде Main Activity написать
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();
    }

По этому коду сначала выскакивает одно сообщение, потом, при нажатии на Ок - второе, потом оно закрывается.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы