Pass String from onClickListener insde onCreate to outside onCreate

sorry but I have some till now I don’t understand, I am working in a small App were I need to pass a String from setOnClickListener inside the onCreate then pass this variable to outside the onCreate in the same Activity, the String is finalDeRec1 ….please find the code

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rectangular_duct);
//Balance code

 mbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double airFlowCal = Double.parseDouble(airFlow.getText().toString());
                double widthRec = Double.parseDouble(recWidthMm.getText().toString());
                double HeightRec = Double.parseDouble(recHeightMm.getText().toString());
                double finalDe = calculateDe(widthRec, HeightRec);
                String finalDeRec1 = String.valueOf((String.format("%.2f",finalDe)));
//Balance code


        });

    }
//outside onCreate 
private void createPDF() {
        printRec.setOnClickListener(new View.OnClickListener()  {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View v) {
                PdfDocument myPdfDocument = new PdfDocument();
//Balance Code
   });
    }
}

Please advise

Answer

Why not creating a global variable outside of the oncreate method and put the value inside it.

String value = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rectangular_duct);

    mbutton.setOnClickListener(new View.OnClickListener() {
                                   @Override
                                   public void onClick(View v) {
                                       double airFlowCal = Double.parseDouble(airFlow.getText().toString());
                                       double widthRec = Double.parseDouble(recWidthMm.getText().toString());
                                       double HeightRec = Double.parseDouble(recHeightMm.getText().toString());
                                       double finalDe = calculateDe(widthRec, HeightRec);
                                       String finalDeRec1 = String.valueOf((String.format("%.2f",finalDe)));
                                       value = finalDeRec1;

                                   });

                               }
    private void createPDF() {
        printRec.setOnClickListener(new View.OnClickListener()  {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View v) {
                PdfDocument myPdfDocument = new PdfDocument();

                String yourStringIsHere = value;
            });
        }
    }

just add a String outside of the onCreate method, and assign it inside your oncCickListener. then use it outside of onCreate method.

Leave a Reply

Your email address will not be published. Required fields are marked *