0

Good afternoon, I am clicking inside the popupview, and it is closing, the behavior I hope is that the click event will only be triggered by clicking outside, but as you can see the popup is occupying the entire screen, is that the problem?

as you can see the popup is occupying the entire screen, with a transparent background, but if I click inside the popup it closes popup

how could I center the popup using ConstraintLayout?

Code popup

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:background="#80AAAAAA"
android:layout_margin="40dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
    android:layout_margin="20dp"
    android:id="@+id/pop_up_enviar_ocorrencias_card_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:cardCornerRadius="10dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    android:padding="20dp"
    app:layout_constraintHorizontal_bias="0.0">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/pop_up_ocorrencias_constraint_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="8dp">
        <TextView
            android:id="@+id/pop_up_ocorrencias_txt_cracha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Marcações"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />
        <ListView
            android:id="@+id/pop_up_enviar_ocorrencias_list_marcacoes"
            android:layout_width="0dp"
            tools:listitem="@layout/item_marcacao"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="@id/pop_up_ocorrencias_txt_cracha"
            app:layout_constraintTop_toBottomOf="@id/pop_up_ocorrencias_txt_cracha"/>
        <TextView
            android:id="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Ocorrências"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_list_marcacoes"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_list_marcacoes" />
        <TextView
            android:id="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias_api"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias" />
        <TextView
            android:id="@+id/pop_up_enviar_ocorrencias_txt_motivo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Motivo"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias_api"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias_api" />
        <EditText
            android:id="@+id/pop_up_enviar_ocorrencias_edt_motivos_api"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Motivo"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_txt_motivo"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_txt_motivo" />

        <TextView
            android:id="@+id/pop_up_enviar_ocorrencias_txt_descricao"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Descrição"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_edt_motivos_api"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_edt_motivos_api" />
        <EditText
            android:id="@+id/pop_up_enviar_ocorrencias_edt_descricao_api"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Descrição da ocorrência"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_txt_descricao"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_txt_descricao" />

        <TextView
            android:id="@+id/pop_up_enviar_ocorrencias_txt_foto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Foto"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_edt_descricao_api"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_edt_descricao_api" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/pop_up_enviar_ocorrencias_constraint_foto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_txt_foto"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_txt_foto">

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/pop_up_enviar_ocorrencias_txt_selecione_imagem"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="Selecione uma imagem"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/pop_up_enviar_ocorrencias_image_selecione_imagem"
                app:layout_constraintStart_toStartOf="parent" />

            <ImageView
                android:id="@+id/pop_up_enviar_ocorrencias_image_selecione_imagem"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@drawable/ic_cselect_image"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/pop_up_enviar_ocorrencias_constraint_foto_vizualizar_foto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_constraint_foto"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_constraint_foto">

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/pop_up_enviar_ocorrencias_txt_vizualizar_foto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:breakStrategy="high_quality"
                android:text="Vizualizar Foto"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent" />

            <ImageView
                android:id="@+id/pop_up_enviar_ocorrencias_image_vizualizar_foto"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@drawable/ic_arrow_down"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/pop_up_enviar_ocorrencias_enviar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Enviar"
            android:background="@drawable/button_round"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_constraint_foto_vizualizar_foto"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_constraint_foto_vizualizar_foto" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

Code my class popup

public class PopUpOcorrencia {

//referencia https://medium.com/@evanbishop/popupwindow-in-android-tutorial-6e5a18f49cc7
@RequiresApi(api = Build.VERSION_CODES.Q)
@SuppressLint("ClickableViewAccessibility")
public void showPopupWindow(final View view){
    //Crie você mesmo um objeto View através do inflater
    LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popupView = inflater.inflate(R.layout.pop_up_enviar_ocorrencia, null);

    // Especifique o comprimento e a largura por meio de constantes
    int width = ConstraintLayout.LayoutParams.MATCH_PARENT;
    int height = ConstraintLayout.LayoutParams.MATCH_PARENT;

    // Tornar itens inativos fora da PopupWindow
    boolean focusable = true;

    // Crie uma janela com nossos parâmetros
    final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);

    // Defina a localização da janela na tela
    popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

    popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#80AAAAAA")));
    popupWindow.setFocusable(true);
    popupWindow.setOutsideTouchable(true);

    //popupWindow.setTouchModal(true);

    // Inicialize os elementos de nossa janela, instale o manipulador
    TextView test2 = popupView.findViewById(R.id.pop_up_enviar_ocorrencias_txt_ocorrencias_api);
    test2.setText("Teste");

    Button buttonEdit = popupView.findViewById(R.id.pop_up_enviar_ocorrencias_enviar);
    buttonEdit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //As an example, display the message
            Toast.makeText(view.getContext(), "Wow, popup action button", Toast.LENGTH_SHORT).show();

        }
    });

    // Manipulador para clicar na zona inativa da janela
    popupView.setOnTouchListener((View v, MotionEvent event) -> {

        //Close the window when clicked
        popupWindow.dismiss();
        return true;
    });
  }
}
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53

0 Answers0