Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Twitter share button in Android?

I am creating an android app wherein i want to add Twitter share button , on clicking the Twitter button will share something (Title + url) on twitter. I’m almost there, but I have a problem with the link. When I give two parameters with "&", it won’t work. Please let me know if anyone has a suitable answer which i can implement. Answers will be really appreciated.

//Full URL: http://www.mywebsite.com/index.php?option=com_content&catid=40&id=12546&view=article**

String title = "Hello title";
String catid = "40";
String id = "12546";

Uri.Builder b = Uri.parse("http://www.mywebsite.com/").buildUpon();
    b.path("index.php");
    b.appendQueryParameter("option=com_content&catid", catid);
    b.appendQueryParameter("&id", id);
    b.appendQueryParameter("&view=article", null);
    b.build();

String url = b.build().toString(); 

String tweetUrl = "https://twitter.com/intent/tweet?text=" + title + "&url=" + url;

Uri uri = Uri.parse(tweetUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));

Output: => “http://www.mywebsite.com/index.php?option=com_content&catid=40”

but the => “&id=12546&view=article” is missing.

like image 413
Osman Avatar asked Oct 22 '22 20:10

Osman


1 Answers

You should not provide the ampersand (&) inside the first parameter of the "appendQueryParameter", it will be added for you automatically. Use it as:

b.appendQueryParameter("id", id); // instead of ("*&*id", id);
b.appendQueryParameter("view=article", null); // instead of ("*&*view=article", null);

And the output is going to be the one you expect

https://twitter.com/intent/tweet?text=Hello title&url=http://www.mywebsite.com/index.php?option%3Dcom_content%26catid=40&id=12546&view%3Darticle=null

See here an example of usage.

like image 74
Eduardo Avatar answered Oct 27 '22 12:10

Eduardo