user1869103
user1869103

Reputation:

In android, how can I send a string to a menu item method?

I'm working on an android app where the user presses a button, a text is generated, then there is a menu option to share/export the text.

Here are the relevant code parts (for clarity, I don't paste the entire code, but if you need anything just let me know)

FloatingActionButton button = (FloatingActionButton) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        String savedText = SomeMethod(); //the string used here is obtained through another method
        TextView resp = (TextView) findViewById(R.id.TextOutput);
        resp.setText(savedText);
        }
    });

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
        if (id == R.id.action_export) {
            exportPage();
            return true;
        }
        return super.onOptionsItemSelected(item);
}

public void exportPage() {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, savedText);
        sendIntent.setType("text/plain");
        startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}

My question:

How can I actually send the string generated from the SomeMethod method (i.e. savedText, see comment in line 5 of the code) to the exportPage method? As it stands, the code gets the value for the savedText string from the initialized string value, not the value generated by the SomeMethod method.

What I have tried: Looking for answers to similar questions, I discovered that one way is the one described here: How to pass a string from one method to another method in Java

I attempted to modify my code accordingly, like this:

TextView resp = (TextView) findViewById(R.id.TextOutput);
resp.setText(savedText);
exportPage(); // <<< attempting to send the string to the exportPage method

public void exportPage() {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, this.savedText); //<<< modified
        sendIntent.setType("text/plain");
        startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}

But this gives me the error that the savedText string is not initialized. I attempted to initialize it, but now it again uses that global value instead of the modified one.

At this point I thought that maybe I was working with a local variable instead of an instance one (I'm a real beginner, so I'm still learning these aspects), and tried the other way:

TextView resp = (TextView) findViewById(R.id.TextOutput);
resp.setText(savedText);
exportPage(savedText); // <<< attempting to send the string to the exportPage method

public void exportPage(String savedText) { //<<<< modified
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, savedText); 
        sendIntent.setType("text/plain");
        startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}

But now there is another error. On the part of the code where the menu item is selected:

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
        if (id == R.id.action_export) {
            exportPage();
            return true;
        }
        return super.onOptionsItemSelected(item);

I get an error because the exportPage method expects a String. I tried to modify this with exportPage(savedText); but of course now it doesn't recognize the string savedText and wants me to initialize it (doing that, I go back to the original problem, getting the global string value instead of the generated one).

As I said, I'm a beginner, so I'm sure this is a very simple error on my behalf, but I just can't figure it out. Any ideas?

Upvotes: 1

Views: 107

Answers (3)

AlexTa
AlexTa

Reputation: 5251

Declare resp TextView as global variable, then just get text value directly from TextView:

private TextView resp = null;

FloatingActionButton button = (FloatingActionButton) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String savedText = SomeMethod(); //the string used here is obtained through another method
        resp = (TextView) findViewById(R.id.TextOutput);
        resp.setText(savedText);
    }
});

public void exportPage() {
    String savedText = this.resp != null ? this.resp.getText().toString() : "";
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, savedText);
    sendIntent.setType("text/plain");
    startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}

Upvotes: 0

Pratik Vyas
Pratik Vyas

Reputation: 654

Hello Try this if it may help,

1) call the method from the onClick method itself

2) Add the parameter to the method for getting the value

FloatingActionButton button = (FloatingActionButton) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        String savedText = SomeMethod(); //the string used here is obtained through another method
        TextView resp = (TextView) findViewById(R.id.TextOutput);
        resp.setText(savedText);
        //Call method exportPage so that it can get value on the go 
        exportPage(String savedText);
        }
    });

public void exportPage(String savedText) {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, savedText);
        sendIntent.setType("text/plain");
        startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}

Upvotes: 0

MageNative
MageNative

Reputation: 702

use this

public void exportPage() { //<<<< modified
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, resp.getText().tostring()); 
        sendIntent.setType("text/plain");
        startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}

Upvotes: 1

Related Questions