Vladimir
Vladimir

Reputation: 77

pass string from one method to another android

I'm new to android development, and starting to learn some things... I want to know how to implement one thing that I know is very basic, but I cannot find a way to do it with my actual knowledge of Android programming. Been trying for hours now and no results after all! I found lots of questions here on stack overflow with people asking the same thing, I've tried a lot of them, but no success after all!

I wrote the following code, with 2 methods (vo2maxCalculo and shareIt), the "share it" method should get the value from the MSG1 variable and insert this value into the "shareBody" string, so the user can share it via email, etc..

How can I do that?

Thank you very much!


        public void vo2maxCalculo(View v) {
        float minutos2;
        double calculo2;

        EditText minutos1 = (EditText) findViewById(R.id.minutos);
        TextView resultado5 = (TextView) findViewById(R.id.vo2max12Resultado);


        minutos2 = Float.parseFloat(minutos1.getText().toString());

        calculo2 = 33 + 0.17 * (minutos2 - 1955) / 15;
        String msg1 = String.format("%.2f", calculo2);
        resultado5.setText(msg1);


    }

    public void shareIt(View v) {

        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        String shareBody = String.format("The result is:", msg1);
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Final Result");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
        startActivity(Intent.createChooser(sharingIntent, "Share via"));

    }

Upvotes: 2

Views: 3064

Answers (5)

Vladimir
Vladimir

Reputation: 77

Finally, with the valuable help of you all the problem is solved and the working code is:

String msg1; //Like this

public void vo2maxCalculo(View v) {
float minutos2;
double calculo2;

EditText minutos1 = (EditText) findViewById(R.id.minutos);
TextView resultado5 = (TextView) findViewById(R.id.vo2max12Resultado);


minutos2 = Float.parseFloat(minutos1.getText().toString());

calculo2 = 33 + 0.17 * (minutos2 - 1955) / 15;
msg1 = String.format("%.2f", calculo2); //Set it in here like this
resultado5.setText(msg1);

}

public void shareIt(View v) {

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");

//Can still use it in here as long as the other method got called first
String shareBody = String.format("The result is: %s", msg1);

sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Final Result");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));

}

Thanks to everyone that spent a valuable amount of time in helping me solve the problem!

Upvotes: 0

Yarh
Yarh

Reputation: 4607

The propper wat is to use global variable ( as mentioned before) and updated this variable inside vo2maxCalculo after it was called.

Another way - you can extract text from resultado5 using `resultado5.getText().toString();

You did not mentioned but you may unsuccesfully tring to implement onClick behaviour? In this case add in layout xml file android:onClick="vo2maxCalculo" and android:onClick="shareIt" If those method are in other activities, you should pass intent when create activity or save data in singleTon ( usefull only for big havie single objects, like bitmaps)

Upvotes: 0

SuNnY_sYeD
SuNnY_sYeD

Reputation: 513

Create a variable out side both the scopes like this

String msg1; //Like this
public void vo2maxCalculo(View v) {
    float minutos2;
    double calculo2;

    EditText minutos1 = (EditText) findViewById(R.id.minutos);
    TextView resultado5 = (TextView) findViewById(R.id.vo2max12Resultado);


    minutos2 = Float.parseFloat(minutos1.getText().toString());

    calculo2 = 33 + 0.17 * (minutos2 - 1955) / 15;
    msg1 = String.format("%.2f", calculo2); //Set it in here like this
    resultado5.setText(msg1);


}

public void shareIt(View v) {

    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");

    //Can still use it in here as long as the other method got called first
    String shareBody = String.format("The result is: %s", msg1);

    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Final Result");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    startActivity(Intent.createChooser(sharingIntent, "Share via"));

}

Upvotes: 2

Kaushik
Kaushik

Reputation: 6162

Try like this

Move all initialization of all widgets in onCreate(...) of your Activity

minutos1 = (EditText) findViewById(R.id.minutos);//declare it globally at class level
resultado5 = (TextView) findViewById(R.id.vo2max12Resultado);//declare it globally at class level

You can create a separate class named Utils and create a static method for calculation in Utils class and call that in both onClick event(by this way you can call this method from any class)

public static String calculate(float minutos2) {
    double calculo2 = 33 + 0.17 * (minutos2 - 1955) / 15;
    String msg1 = String.format("%.2f", calculo2);
    return msg1;
}

in onClick of those Button do like this

public void vo2maxCalculo(View v) {
float minutos2;
String msg = minutos1.getText().toString().trim();
if(msg != null && !msg.equalsIgnoreCase(""))
    minutos2 = Float.parseFloat(msg);
else
    minutos2 = 0.00;

String msg1 = Utils.calculate(minutos2);
resultado5.setText(msg1);
}

public void shareIt(View v) {
    float minutos2;
    String msg = minutos1.getText().toString().trim();
    if(msg != null && !msg.equalsIgnoreCase(""))
        minutos2 = Float.parseFloat(msg);
    else
        minutos2 = 0.00;

    String msg1 = Utils.calculate(minutos2);
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    String shareBody = String.format("The result is:", msg1);
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Final Result");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    startActivity(Intent.createChooser(sharingIntent, "Share via"));
}

Upvotes: 0

Rohit5k2
Rohit5k2

Reputation: 18112

If I understand it correctly then this is the solution. Also I am sure in almost every programming language this is done like this.

Both methods appear to be click listeners: Use this only if they are not

public String vo2maxCalculo(View v) { // changed here
    float minutos2;
    double calculo2;

    EditText minutos1 = (EditText) findViewById(R.id.minutos);
    TextView resultado5 = (TextView) findViewById(R.id.vo2max12Resultado);


    minutos2 = Float.parseFloat(minutos1.getText().toString());

    calculo2 = 33 + 0.17 * (minutos2 - 1955) / 15;
    String msg1 = String.format("%.2f", calculo2);
    resultado5.setText(msg1);

    return msg1; // added this

}

public void shareIt(View v) {

    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    //String shareBody = String.format("The result is:", vo2maxCalculo(v)); // Why String.format???
    String shareBody = "The result is:" + vo2maxCalculo(v); // Replace this
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Final Result");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    startActivity(Intent.createChooser(sharingIntent, "Share via"));

}

Both methods appear to be click listeners: Use this if they really are

String msg1 = ""; // Added this
public void vo2maxCalculo(View v) {
    float minutos2;
    double calculo2;

    EditText minutos1 = (EditText) findViewById(R.id.minutos);
    TextView resultado5 = (TextView) findViewById(R.id.vo2max12Resultado);

    minutos2 = Float.parseFloat(minutos1.getText().toString());

    calculo2 = 33 + 0.17 * (minutos2 - 1955) / 15;
    msg1 = String.format("%.2f", calculo2); // Edited this
    resultado5.setText(msg1);
}

And no change in the other method

Upvotes: 1

Related Questions