android-intentandroid-intent-chooser

How to pass value string from API in Intent Chooser?


I want to make a news url share feature that gets from the API.

I'm using the intent chooser to run the share feature in my app, but I'm having trouble entering the url of the article obtained from the API into the intent value?

This is my code

        share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "Baca selengkapnya");
            sendIntent.setType("text/plain");
            Intent shareIntent =  Intent.createChooser(sendIntent, null);
            startActivity(shareIntent);
        }
    });

And this

url.setText((article.getUrl()));

Solution

  • You just need to add (article.getUrl()) to your code. Example:

            share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "Baca selengkapnya" + (article.getUrl()));
            sendIntent.setType("text/plain");
            Intent shareIntent =  Intent.createChooser(sendIntent, null);
            startActivity(shareIntent);
        }
    });