I want to check if user with email authentication has a phone number set in user profile.
I try if(user.getPhoneNumber() == null{
//do this
}
but it is not working for me... So i want to know what is default value of it
or how can i make it in another way. Thank you :)
user.getPhoneNumber() returns a String. The default value is an empty String which is not null. Use either
if(TextUtils.isEmpty(user.getPhoneNumber())){
//do this
}
or
if("".equals(user.getPhoneNumber())){
//do this
}
Related
I use the CountryCodePicker from the CountryCodePickerProject by hbb20.
I get the User's data from the DataBase, and sometimes for whatever reason the User has "" (empty string) saved as their country.
I use this Picker to edit their country, and if the User does not change his country then I get India "IN" as the selectedCCPCountry since the defaultCCPCountry automatically returns India if no country was set.
How can I set the selectedCCPCountry or some other flag to null or "" so that in this case I can know not to change the User's data?
Thanks!
So I created an issue for this on GitHub and got a quick response from the author:
It seems like your primary use case is to select country without phone
number validation, did I get that right? if so, you should try Android
Country Picker (https://github.com/hbb20/AndroidCountryPicker). It has out
of box support for non selection and much more. Please let me know if that
works for you.
Here is my response and my temporary workaround:
As I don't want to undo all the code I have already, I made a temporary workaround.
mCountryChangedByUser is false and turns true only when a country is selected inside the CountryCodePicker.
mCountryCCP.setOnCountryChangeListener(new CountryCodePicker.OnCountryChangeListener() {
#Override
public void onCountrySelected() {
mCountryChangedByUser = true;
mCountryTextTV.setText(mCountryCCP.getSelectedCountryName());
mUser.setCountry(mCountryCCP.getSelectedCountryNameCode());
}
});
If the user did not change his country then just return the original country in my saveUser() function.
if (mCountryChangedByUser) {
userUpdate.setCountry(mCountryCCP.getSelectedCountryNameCode() );
} else {
userUpdate.setCountry(mUser.getCountry());
}
Maybe next time I'll use the AndroidCountryPicker.
Thanks!
I want the user to type in his or her Facebook account-link (don't have a better solution atm).
Now, when the user clicks the edittext it is supposed to say : "www.facebook.com/". Now the cursor is supposed to be at the END of the edittext (after the "/") and the user is not supposed to delete the first letters so that the "www.facebook.com/" stays exactly where it is. This will have the user to ONLY type in his or her facebook name and therefore connect the profile.
Is there a way of doing this?
Thank you :)
You can do that by using the event "TextChanged" and in your code validate if the size is big than your string, like that:
if (((EditText)sender).Text.Length >= 17)
{
((EditText)sender).Text = e.NewTextValue;
}
else
{
((EditText)sender).Text = "www.facebook.com/";
}
So if the value is bigger than your string you will replace that for the value, if not you just set the value with your string
I have three fields in my form
1] Email address
2] Phone number
3] Web address
I know how to give validation for all, but i have diff requirement
What i want is if all fields are null no validation is applicable(no need to check)
if 1 field is empty and rest are filed check for those filed only not empty filed
I don't know what to do with this thing,
need to check for null/empty if null no need to to check for validation if field is not null/empty then check for that field only not other field
You can do something like this
if (!emailAddressEditText.getText().toString().matches(""){
//Apply your validation
}
if (!phoneNumberEditText.getText().toString().matches("")){
//Apply your validation
}
if (!webAddressEditText.getText().toString().matches("")){
//Apply your validation
}
I hope this helps you
In my onCreate() I have:
if(sharedPref.getString("name",null)==""){EditText.setText("Something");}
else{EditText.setText(sharedPref.getString("name",null));}
And then in my onStop() I have:
sharedPrefEditor.putString("name",EditText.getText().toString());
The EditText shows only the hint when I first install and run it. It does seems to display the correct text when it's started later, however.
Don't use == to compare content of Strings. Use equals() instead :
if(sharedPref.getString("name",null).equals("")){
EditText.setText("Something");
}
Why should I use equals instead of ==
First of all you are not using correctly the shared preferences. You are typing null as second parameter which is the value you will get if the key you typed as first parameter is not defined. This is not a problem itself but then in your if sentence you are comparing it to an empty string.
So first of all use equals instead of ==and then if you want to receive an empty string if the key is not defined type an empty string as second parameter of SharedPreferences.gerString method.
Hope it helps ;)
If "Something" is to be used if preference not set, then:
EditText.setText(sharedPref.getString("name","Something"));
getString() will automatically return "Something" if preference is not set. No need of extra code.
String strName = sharedPref.getString("name","");
if(!strName.isEmpty()){
EditText.setText(strName);
}else{
EditText.setText("Something");
}
The operator == can not be used to compare strings in Java.
if(string1.equals(string2){
}
Also, you set a value to SharedPreferences, but you never commit the changes.
sharedPrefEditor.commit();
OnCreate
String prefsVal = sharedPref.getString("name", null);
if(prefsVal.equals(null)){ //If default value was returned
EditText.setText("Something");
}else{
EditText.setText(prefsVal);
}
OR
The following will turn your posted code into a single line solution. The second parameter passed to SharedPref is the default value to be returned if no vale was found for the given key.
EditText.setText(sharedPref.getString("name", "Something");
OnStop
sharedPrefEditor.putString("name", EditText.getText().toString());
sharedPrefEditoy.commit();
I am working on android. i want to know that whether this is possible to check that
following key,value pair has created or not ?
String filename=getIntent().getExtras().getString("FILE_NAME");
textView_file_name=(TextView)findViewById(R.id.TextView_fileName);
textView_file_name.setText(filename);
before my first programing statement, means before getting the value of FILE_NAME, i want to check whether this key-value pair has build or not ? because i am getting null pointer exception in the case if it is not done before these statement. So i want to know that is any code so i can check intent.putExtras().getString("FILE_NAME"); has done or not.
You can store in a String variable if you send String data as below and print in cosole to check it:
Bundle b=intent.getExtras();
String str=b.getString("FILE_NAME"); //IF YOU HAVE STRING AS VALUE THEN USE getString() else use accordingly.
System.out.println(str);