Search code examples
androidbuttononclicklistenerbuttonclickcustomdialog

Button is not working inside custom dialog which is appeared after long press of a button


I have opened a custom dialog inside longPress of a button, Inside that custom dialog i have two buttons and one edit text. I want to change the name of the button which i have longpressed with the name which i get from the edit text inside the custom dialog, the buttons are not working.

public void initializeLPButtons(Button[] btns, int[] rArrays) {

    for(i=0; i<btns.length; i++) {

        btns[i] = (Button) findViewById(rArrays[i]);
        btns[i].setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {

                renameBtn();
                return true;
            }
        });
    }
}

public void renameBtn() {

    final AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
    dialog.setView(R.layout.dialog_renamebtn);
    dialog.setTitle("Rename Button...");
    dialog.setMessage("Rename The Button To:");

    LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
    final View dialogView = inflater.inflate(R.layout.dialog_renamebtn, null);

    renameEt = (EditText) dialogView.findViewById(R.id.renameEt);
    renameBtn = (Button) dialogView.findViewById(R.id.renameBtn);
    cnclRenameBtn = (Button) dialogView.findViewById(R.id.cnlRenameBtn);

    cnclRenameBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
    });

    renameBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            renameBtnTxt();
        }
    });

    dialog.show();
}

public void renameBtnTxt() {

    if (renameEt.getText().toString().length() > 0) {
        btns[i].setText(renameEt.getText().toString());
    }else {
        Toast.makeText(MainActivity.this, "Please choose a name.",
                Toast.LENGTH_SHORT).show();
    }
}

I dont think my code has problems. Please if it is something with java help me find it.


Solution

  • Create your dialog like this:

    private void CreateGetPhoneNumDialog() {
            Dialog PhoneNumberDialog = new Dialog(getActivity());
            PhoneNumberDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            PhoneNumberDialog.setContentView(R.layout.mobile_number_pop_message);
            PhoneNumberDialog.setCancelable(false);
    }
    

    Get edittext and button's reference after creating the dialog

    etDialogPhoneNum = (EditText) PhoneNumberDialog.findViewById(R.id.et_DialogCustomerPhone);
    btnGetPhoneNum = (Button) PhoneNumberDialog.findViewById(R.id.btn_DialogGetPhoneNum);
    

    Then set on click listener on dialog's button

    btnGetPhoneNum.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               // Do your work here
           }
    });
    

    mobile_number_pop_message.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="300dp"
        android:background="@color/White"
        android:layout_height="wrap_content"
        >
    
         <EditText
            android:layout_width="match_parent"
            android:layout_height="53dp"
            android:id="@+id/et_DialogCustomerPhone"
            android:inputType="phone"
            android:background="@null"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:maxLength="9"
            android:hint="512345678"
            android:gravity="start|center_vertical"
            android:layout_gravity="start|center_vertical"
        />
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="@string/str_UpdatePhoneNumber"
            android:textAllCaps="true"
            android:layout_gravity="center"
            android:id="@+id/btn_DialogGetPhoneNum" 
        />
    
    </LinearLayout>
    

    Edit:

    And show the dialog like this:

    PhoneNumberDialog.show();