Home > Mobile >  How to define setOnClickListner in a popup view(Android Studio)?
How to define setOnClickListner in a popup view(Android Studio)?

Time:09-17

So, I have made a button which on clicking opens a popup window with two buttons inside it, but now I am not able to assign setOnClickListner to those two buttons inside the OnCreateMethod as I did for all the other buttons in the main window (If I do then the app Stops responding) as till that time the buttons are not actually there(I think, that's the reason), So could you please help me assign on click listner to those buttons.

Here is the code for the popupWindow excution:

 public void onButtonShowPopupWindowClick(View view) {

    // inflate the layout of the popup window
    LayoutInflater inflater = (LayoutInflater)
            getSystemService(LAYOUT_INFLATER_SERVICE);
    View popupView = inflater.inflate(R.layout.confirm_number_popup, null);

    hideSoftKeyboard(Register.this);

    // create the popup window
    int width = LinearLayout.LayoutParams.WRAP_CONTENT;
    int height = LinearLayout.LayoutParams.WRAP_CONTENT;
    boolean focusable = true; // lets taps outside the popup also dismiss it
    final PopupWindow popupWindow = new PopupWindow(popupView, width, height, false);

    // show the popup window
    // which view you pass in doesn't matter, it is only used for the window token
    popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

    // dismiss the popup window when touched
    popupView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            popupWindow.dismiss();
            return true;
        }
    });
}

Here is the layout file for the popup view:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/white">

<Button
    android:id="@ id/btn_edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/green_shade_1"
    android:text="@string/edit"
    android:textSize="16sp"
    android:layout_below="@id/txt_edit"
    android:layout_alignParentStart="true"
    android:layout_marginStart="20dp"
    android:background="@color/white"
    style="?android:attr/borderlessButtonStyle"/>

<Button
    android:id="@ id/btn_Ok"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/green_shade_1"
    android:text="@string/ok"
    android:textSize="16sp"
    android:layout_below="@id/txt_edit"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="20dp"
    android:background="@color/white"
    style="?android:attr/borderlessButtonStyle"/>
    
</RelativeLayout>

Please tell me if you are not able to understand the question or if any else code is needed.
Thank You!

Edit: I have already tried adding this in my OnCreate but it doesen't work:

Button btnEdit = findViewById(R.id.btn_edit);

btnEdit.setOnClickListener(v -> {
        //The function
    });

CodePudding user response:

use

Button btn1 = popupView.findViewById(R.id.btn_edit);
btn1.setOnClickListener{}
  • Related