Show an AlertDialog with the result of a mathematical operation

0

I am making an application where there is only one main Activity and it shows different Fragments with mathematical operations.

I would like the result to be shown in an AlertDialog and not in a TextView that is how I have it. I leave the code to see if anyone can lend me a hand. Thanks.

CODE:

public class AnguloFragment extends Fragment {

    EditText edtText1, edtText2;
    Button ebtn1;
    TextView txtResultado;



    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.angulo_fragment, container, false);


        edtText1 = (EditText) rootView.findViewById(R.id.editText1);
        edtText2 = (EditText) rootView.findViewById(R.id.editText2);
        ebtn1 = (Button) rootView.findViewById(R.id.btn1);
        txtResultado = (TextView) rootView.findViewById(R.id.textResultado);
        ebtn1.setOnClickListener(new View.OnClickListener() {



            @Override
            public void onClick(View v) {

                double dR = Double.parseDouble(edtText1.getText().toString());
                double aN = Double.parseDouble(edtText2.getText().toString());
                double aN2 = aN * 2.0 * Math.PI/360.0;
                double aN3 = Math.cos(aN2);
                double dC = dR * aN3;
                //redondea resultado mostrando solo 2 decimales
                DecimalFormat df = new DecimalFormat("0.00");
                String sPi = df.format(dC);

                txtResultado.setText("DISTANCIA CORREGIDA "+sPi+" metros" );



            }


        });


        return rootView;

    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getActivity().setTitle("Angulo de situacion");

    }





}
    
asked by Victor 20.02.2017 в 16:37
source

2 answers

0

I put the code with which I got it:

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Resultado");  //define el título
            builder.setMessage("Distancia corregida: "+ spi);

            builder.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

            builder.show();
    
answered by 23.02.2017 / 19:28
source
0

Basically:

new AlertDialog.Builder(context)
.setTitle("Respuesta")
.setMessage("El resultado es...")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { 
        // acá lo que tiene que hacer al hacer click en OK, fijate que podés personalizar el mensaje cambiando lo que dice aca: android.R.string.yes
    }
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { 
        // acá lo que tiene que hacer al hacer click en NO, fijate que podés personalizar el mensaje cambiando lo que dice aca: android.R.string.yes
    }
})
.show();

You can explore the options, there is also a neutral button, etc. If you want to define a custom layout, you can also. And note that all methods are optional, only with new AlertDialog.Builder(context).show() a Dialog is displayed.

This code goes where you want to display the Dialog. In your case, instead of txtResultado.setText("DISTANCIA CORREGIDA "+sPi+" metros" );

    
answered by 20.02.2017 в 18:26