How to create a Twitter share button in Android? - 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.

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.

Related

Display nested data from JSON in textview android

I think this problem is trivial, but I am not advanced in programming. I have problem with displaying Json data to TextView
My Json file is like
{
"person":{
"firstName":"John",
"lastName":"Jankow",
"nick":"Jayjay"
}
...
}
another data from Json like address, id etc. displaying without problem, when I use
((TextView) findViewById(R.id.person)).setText("Person: " + json.optString("person"));
I get string "person":{
"firstName":"John",
"lastName":"Jankow",
"nick":"Jayjay"
}"
I've tried to do this using
((TextView) findViewById(R.id.person)).setText("Person: " + json.optString("firstName") + json.optString("lastName"));
but the result is empty TextView.
Question ==> Is there any method to handle this data and display correctly like substring or something like this?
do this
JSONObject person = json.getJSONObject("person");
((TextView) findViewById(R.id.person)).setText("Person: " + person.getString("firstName") + person.getString("lastName"));
Not entirely sure if this helps but see this.
JSONObject person = jsonObj.getJSONObject("person");
String firstName = person.getString("firstName");
String lastName = person.getString("lastName");
Use strings in an awesome way.
textView.setText(firstName);

Android String from Html or direct initialise with ∢ getting wrong String like []

I have tried to parse Html code in String with correct Html code code to get ∢ this String but not able to get that String. also i have assign direct sign ∢ but its return [] String.
code.
if (btn_tag.equals("op_sangle") {
str_text = " " + Html.fromHtml("∢";
//or
// str_text = " ∢";
}
here str_text = [] gets its wrong.

How to get Text written after "," in MultiAutoCompleteTextView?

I am developing an application that uses MultiAutoCompleteTextView for showing hints in the drop down list.In this application I retrieve the value written in the MultiAutoCompleteTextView by using
multitextview.getText();
and then query this value to server to recieve JSON response which is shown as suggestions in the drop down list.
If a user types Mu and then Selects music from the list and then types box for another suggestion the content in the MultiAutoCompleteTextView becomes Music,box and now the value for querying to the server is Music,box instead of this I want to select only box.
My question is how to retrieve text written after "," in MultiAutoCompleteTextView?
Can this be achieved using getText()?
I solved this issue
String intermediate_text=multitextview.getText().toString();
String final_string=intermediate_text.substring(intermediate_text.lastIndexOf(",")+1);
I'm sure there are several ways to get around this. One way to do it would be:
String textToQuerryServer = null;
String str = multitextview.getText().toString(); // i.e "music, box" or "any, thing, you , want";
Pattern p = Pattern.compile(".*,\\s*(.*)");
Matcher m = p.matcher(str);
if (m.find()) {
textToQuerryServer = m.group(1);
System.out.println("Pattern found: "+ textToQuerryServer);
}else {
textToQuerryServer = str;
System.out.println("No pattern: "+ textToQuerryServer);
}

Android Populating a Email Address Using a String

Moving on from my original question below:
Android Sending an email using a list of email addresses
I have tried to populate the email address line by using a string. When writing the email the message section is populated with the answer given by clicking on a spinner, the code for this is shown as "%1$s". So if I have a spinner that has 4 different countries in the string for the main email I put:
Country: %1$s
And it will show in the final email to be sent as:
Country: UK
For example.
When I put this string into the email Address instead of it originally looking like:
home%1$s#gmail.com
And coming out in the final email as:
homeUK#gmail.com
It stays as home%1$s#gmail.com!
Is there a reason for this? Is it something that cannot be added to the address line?
I have attached my code below and would very much appreciate someones help. I have been pulling my hair out with this one. The rest of the app is complete!
Main Activity
public void sendFeedbackMessage(String subject, String message) {
Intent messageIntent = new Intent(android.content.Intent.ACTION_SEND);
String aEmailList[] = { getResources().getString(R.string.emailaddress_format) };
String bEmailList[] = { ("country#gmail.com") };
messageIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
messageIntent.putExtra(Intent.EXTRA_CC, bEmailList);
//email.putExtra(Intent.EXTRA_BCC, new String[]{to});
messageIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
messageIntent.setType("plain/text");
messageIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
startActivity(messageIntent);
}
}
And the relevant String
<string
name="emailaddress_format">country%1$s#gmail.com</string>
If any more code is needed to see what I am trying to do then please let me know and I will add it. I didnt want to overload everyone with the whole code and then try and explain where I am struggling.
Hopefully someone can help me and i'm making sense.
Many Thanks
Not sure on the exact answer to the particular problem you're having, but an easier way to accomplish what it seems like you're trying to accomplish would be:
Spinner yourSpinner = (Spinner)findViewById(R.id.yourSpinner);
String country = yourSpinner.getSelectedItem().toString();
String aEmailList[] = { "country" + country + "#gmail.com" };
This will get the country the person has selected from the spinner and convert it to a String, which you can then place in the EXTRA_EMAIL as you're doing already.

How to know whether liked the post or not without doing searching in the list

I tried to use the fql query to get json response,I used this code for doing this
String postid=jsonObject.getString("id");
String query = "SELECT likes.user_likes FROM stream WHERE post_id = \'" + postid + "'";
Bundle params = new Bundle();
params.putString("method", "fql.query");
params.putString("query", query);
String fqlResponse = Utility.mFacebook.request(params);
System.out.println(fqlResponse);
But I am getting null pointer exception at Utility.mFacebook.request(params);
I am using the default class in Github
I personally find the Facebook Graph API a little inadequate at times. For example, in your current requirement, if you need to use just the Graph API, you will need to first, get a list of all Users who have liked a particular post. After you have a list, you will then have to check if the User ID matches that of the logged in User and then, based on the result, run a function or something.
Instead, FQL offers a simpler function. In my app, for the exact same function, I exclusively make use of FQL.
You can fire a simple query. For example:
SELECT likes.user_likes FROM stream WHERE post_id ='XXXXXXXXXXXXX_XXXXXXXXX'
Replace the X's with the Post's ID. Make sure you surround the Post ID with 'YOUR_POST_ID'
Try it like this in the Graph API Explorer: fql?q=SELECT likes.user_likes FROM stream WHERE post_id = 'ENTER_YOUR_POST_ID'
After running the query, you should get a result that looks this this:
{
"data": [
{
"likes": {
"user_likes": true
}
}
]
}
If the User likes the Post, the field user_likes will be true and false if the User has not liked the Post.
You can parse the result like this (pseudo code):
if (JOLikeStatus.has("likes")) {
JSONObject optLikes = JOLikeStatus.optJSONObject("likes");
if (optLikes.has("user_likes")) {
String getUserLikes = optLikes.getString("user_likes");
if (getUserLikes.equals("true")) {
String getLikeStatus = "true";
} else if (getUserLikes.equals("false")) {
String getLikeStatus = "false";
}
} else {
String getLikeStatus = null;
}
} else {
String getLikeStatus = null;
}
EDIT 2: To get the number (count) of total likes, modify the earlier query like this:
fql?q=SELECT likes.user_likes, likes.count FROM stream WHERE post_id = 'ENTER_YOUR_POST_ID'

Categories

Resources