Search code examples
androidbuttonandroid-alertdialogonclicklistenerlayout-inflater

Trouble with OnClick , LayoutInflater, and AlertDialog


I have a onClick event that opens and AlertDialog which is inflated with a LayoutInflater. I can click a button in the inflated AlertDialog and and an equation is solved. The problem is this OnClick is unresponsive, as in Nothing happens. Does anyone know why?

EDIT Now the screen goes dark and I can tell I am no longer on the same activity because the screen is unresponsive but the View is still the original page. Push back button makes the screen bright again and responsive.

lay1.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v)        
        {                     

            LayoutInflater myLayout = LayoutInflater.from(context);
            final View dialogView = myLayout.inflate(R.layout.alerthelp, null);
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);


            final AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialogBuilder.setView(dialogView);
            Button button1 = (Button) dialogView.findViewById(R.id.button1);
            button1.setOnClickListener(new OnClickListener(){


            public void onClick(View v) {
                TextView stax1=(TextView)alertDialog.findViewById(R.id.salestax);
                String sax1 = stax1.getText().toString();
                double sx1 = Double.parseDouble(sax1);

                TextView textviewlay1 =(TextView)alertDialog.findViewById(R.id.m1ab);
                String stringl1 = textviewlay1.getText().toString();
                 Double doubl1 = Double.parseDouble(stringl1);  

                TextView textviewp1 = (TextView)alertDialog.findViewById(R.id.inputPrice);
                String stringp1 = textviewp1.getText().toString();
                Double intp1 = Double.parseDouble(stringp1);

                double resultl1 = intp1 - (doubl1*sx1);
                int precision21t = 100; //keep 4 digits
                resultl1= Math.floor(resultl1 * precision21t +.5)/precision21t;
                textViewtotalproduct.setText(String.valueOf(resultl1));     
}); 

 alertDialog.show();
        return true;    




        }});

Solution

  • You are returning false, which means that the long click didn't happen. So return true in order for your long click to work.

    You also need to call alertDialog.show(); for it to open, answer to your second question.