I'm new to android development. I know this one may repeated question but I didn't understood how i implement these methods. I want to show TextView when result not empty, I have researched and find some method like textview.setVisibility(View.INVISIBLE); OR textview.setVisibility(View.GONE); I'm trying so far but did not get any result.
I have tried this but it is not working properly. Please guide me where i'm wrong.
String Msg = json1.getString("msg");
resultView.setText("Error :" + Msg);
resultView.setVisibility(View.GONE);
Try this
String str = json1.getString("msg");
if (!str.isEmpty()) {
rersultView.setVisibility(View.VISIBLE);
resultView.setText("Error :" + Msg);
} else {
rersultView.setVisibility(View.GONE);
}
Similiar to ULHAS answer, but You can do also:
String jsonString = json1.getString("msg");
if(!jsonString.isEmpty()){
rersultView.setVisibility(View.VISIBLE);
resultView.setText("Error :" + Msg);
}else{
rersultView.setVisibility(View.GONE);
}
I think you have to do something like these:
if(Msg.equalsIgnoreCase("")) { // OR if(Msg.equals(""))
resultView.setVisibility(View.GONE);
} else{
resultView.setVisibility(View.VISIBLE);
resultView.setText("Error :" + Msg);
}
Try it may help you.
Related
I am trying to make a sign in page for an android app. My team leader has decided to make this with a webservice.
Whenever a uses logs in, a request is sent, with 3 possible responses:
0: Wrong Password
20: Wrong username
otherwise: a UUID
I am trying to validate the results given back by the webservice like this:
String resultString = result.toString();
if (resultString.equals("20")) {
Toast.makeText(getBaseContext(), "Het ingevulde emailadres klopt niet!", Toast.LENGTH_LONG).show();
return;
} else if (resultString.equals("0")) {
Toast.makeText(getBaseContext(), "Het ingevulde wachtwoord klopt niet!", Toast.LENGTH_LONG).show();
return;
} else {
Toast.makeText(getBaseContext(), "Debug, klopt", Toast.LENGTH_LONG).show();
return;
}
Seems like basic code to me. However, this code always shows the bottom statement, so it lets the first 2 pass as false.
For debugging purposes, am also returning the resultString to my console. (removed that line in the sample). There I can very obviously see that the result given back is in fact 20.
How can it be that such simple code does not do what I want it to do?
Thanks.
The code seems to be ok... Maybe a white space in the result?
Try trimming the resultString.
which es 20, result or resultString?
As an advice, it is usually better do "".equals(object) because otherwise, if the object is null it will throw NullpointerExceptcion
Regards
Response is not exactly the string you are comparing which are "0" and "20". but the response is
0: Wrong Password
20: Wrong username
otherwise: a UUID
if you want to compare the string then it should be
Options : 1
String resultString = result.toString();
if (resultString.equals("20: Wrong username")) {
// code
} else if (resultString.equals("0: Wrong Password")) {
// code
} else {
// code
}
Option : 2
Instead of eqauls you should use contains
String resultString = result.toString();
if (resultString.contains("20")) {
// code
} else if (resultString.contains("0")) {
// code
} else {
// code
}
I agree with all of the above - the code does look good.
Try using String.compareTo and look at the int returned value to check for hidden differences.
Also, try printing String.toCharArray + String.length to identify extra characters.
If you are taking the complete string returned from web service without extracting the code (0 or 20) then you need to replace 'equals' with 'contains'.
I am creating a op-out app. I am using Incomming SMS Broadcast Receiver, I need to check if the incoming message contains a specific keyword:
i.e
From: 656565451
msn: I want to op-out news
code .i.e - "this does not work"
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
//check if user message has this keywork
String keyWord_code = "op-out";
Pattern pattern = Pattern.compile("op-out");
if (pattern == keyWord_code ) {
//Do the op-out function here
}
else {
//Send message to user
}
I see that you are trying to use Regex?
Well, you can try something a little simpler first before going on to RegEx:
if( message.contains(keyWord_code) ){
// do something
}else{
// do something else
}
I don't know if you have problems with the receiver or what. If you just want to know if the msg body has the word "op-out" you just need the next code:
if(message.contains(keyWord_code) {
//...
} else {
//...
}
I am working with google + getting friends. I can successfully log in and get my credentials. But when I use mPlusClient.loadPeople(this, "me");, the return of
#Override
public void onPeopleLoaded(ConnectionResult status, PersonBuffer personBuffer, String nextPageToken) {
switch (status.getErrorCode()) {
case ConnectionResult.SUCCESS:
try {
int count = personBuffer.getCount();
Log.e("", "count : " + count);
for (int i = 0; i < count; i++) {
Log.e("NAME", "" + personBuffer.get(i).getDisplayName());
}
} finally {
personBuffer.close();
}
break;
case ConnectionResult.SIGN_IN_REQUIRED:
mPlusClient.disconnect();
mPlusClient.connect();
break;
default:
Log.e("TAG", "Error when listing people: " + status);
break;
}
}
is only the details of the logged in user. Now what I what to achieve is to get the list of my friends. I tried using
mPlusClient.loadPeople(MainActivity.this, Person.Collection.Visible);
but it says "Collection cannot be resolved or is not a field".
Any help would be highly appreciated. Thank you.
Looks like you're after the PlusClient.loadVisiblePeople method.
You can see an example of the response using Web API explorer.
This link somehow helped me to feftch the members of my circle in google plus.
mPlusClient.loadVisiblePeople(this, null);
solved the problem. People.Collection.VISIBLE is not already working. I do not know why.
I am currently have a problem with a buzztouch android app I created the error from google play is
NullPointerException
in BT_viewUtilities.updateBackgroundColorsForScreen()
I have narrowed it down to the following code, does anyone see any kind of error in the code. If you need something else please ask, this would fix quite a few apps. Thank you
//updates a screens background colors and image...
public static void updateBackgroundColorsForScreen(final Activity theActivity, final BT_item theScreenData){
BT_debugger.showIt(objectName + ":updateBackgroundColorsForScreen with nickname: \"" + theScreenData.getItemNickname() + "\"");
Either theScreenData or BT_debugger is null.
I have no idea what your code is doing but the fix is simple:
//updates a screens background colors and image...
public static void updateBackgroundColorsForScreen(final Activity theActivity, final BT_item theScreenData){
if(BT_debugger != null && theScreenData != null){
BT_debugger.showIt(objectName + ":updateBackgroundColorsForScreen with nickname: \"" + theScreenData.getItemNickname() + "\"");
} else {
Log.e("YourApp", "Warning null var, command not completed");
}
}
To debug the error you could do:
//updates a screens background colors and image...
public static void updateBackgroundColorsForScreen(final Activity theActivity, final BT_item theScreenData){
if(BT_debugger != null){
if(theScreenData != null){
BT_debugger.showIt(objectName + ":updateBackgroundColorsForScreen with nickname: \"" + theScreenData.getItemNickname() + "\"");
} else {
Log.e("YourApp", "theScreenData was null, command not completed");
}
} else {
Log.e("YourApp", "BT_debugger was null, command not completed");
}
}
I think this is the line that causes null point exception-theScreenData.getItemNickname()
I'm making an application that is posting some information to your facebook wall using facebook sdk for android. This works, but I can't seem to get new lines on the posts. I have tried \n but it doesent work. Any suggestions?
Here is my code:
Bundle parameters = new Bundle();
String temp = "";
for (int i = 0; i < mArrayAdapter.getCount(); i++){
temp = temp + mArrayAdapter.getItem(i) + "\n"; // Not working
}
parameters.putString("message", temp);
mFacebook.dialog(this, "stream.publish", parameters, new DialogListener());
Thanks,
James Ford
Hi James i have tried that before, even with html code but i think thats not possible.
The reason, Facebook must have a control to avoid blank spaces or line break on his posts.
New lines are not allowed in stream posts (the fact that you may have seen them in the past are bugs on facebook).
make use of JSON to post on facebook..
how? look here
STEPS :-
Step 1 :- At this link u will came to know how to use JSON for setting text to TEXTVIEW.
Step 2 :- May be this is not u r looking for :) assign the text of textview to some string
using GetText().ToString
Step 3 :- use this string to post to the facebook.
Step 4 :- I did the same after spending lot of time in googling and finally got the result
by using this trick. u can see my post that i posted during test here
Step 5 :- set the visibilty of this text box to gone using
tv.setVisibility(View.GONE)
And u r done with your posting to facebook..
let the facebook and textview handle how they manage spaces and new line character :D
Some Coding work for newbies like me...
I am posting it on click of button
1)
tv= (TextView)findViewById(R.id.tv);
click=(Button)findViewById(R.id.btn1);
click.setOnClickListener(mthdpost);
2) add on click event to this button
private View.OnClickListener mthdpost=new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
String json = "{"
+ " \"name\": \"myName\", "
+ " \"message\": [\"myMessage1\",\"myMessage2\"],"
+ " \"place\": \"myPlace\", "
+ " \"date\": \"thisDate\" "
+ "}";
/* Create a JSON object and parse the required values */
JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
String name = object.getString("name");
String place = object.getString("place");
String date = object.getString("date");
JSONArray message = object.getJSONArray("message");
String MessageToPost= null;
tv.setText("Name: "+ name +"\n\n");
tv.append("Place: "+ place +"\n\n");
tv.append("Date: "+ date +"\n\n");
/*JSONObject attachment = new JSONObject();
attachment.put("Name: ","\n\n");
attachment.put("Place: ","\n\n");
attachment.put("Date: ","\n\n");*/
for(int i=0;i<message.length();i++)
{
tv.append("Message: "+ message.getString(i) +"\n\n");
//attachment.put("Message: ","\n\n");
}
MessageToPost=tv.getText().toString();
postToWall(MessageToPost);// called the method having logic to post on wall and sending the textview text to to post as message
} catch (JSONException e)
{e.printStackTrace();
}
catch(Exception ex)
{ex.printStackTrace();}
}
};
3) method for posting the message
public void postToWall(String msg){
Log.d("Tests", "Testing graph API wall post");
try {
String response = facebook.request("me");
Bundle parameters = new Bundle();
//parameters.putString("message", msg.toString());
parameters.putString("message", msg);
parameters.putString("description", "test test test");
response = facebook.request("me/feed", parameters,
"POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
Log.v("Error", "Blank response");
}
} catch(Exception e) {
e.printStackTrace();
}
}
HOPE IT WILL HELP :)
This worked for me:
StringBuilder messageData = new StringBuilder(title).append('\n')
.append('\n').append(message).append('\n').append('\n')
.append(description);
// Message
postParams.putString("message", messageData.toString());
This works:
Use this: <center></center>
Instead of a br or a newline, etc. You can only do one in a row (ie. you can't increase the spacing).