Creating a new user in database - android

I am having an issue creating a new user with parse.com.. My code seems correct but for some reason whenever i enter a new user, it does not register.. Can someone help me with this issue?
final EditText newuser = (EditText) this.findViewById(R.id.newuser);
final EditText newuserpassword = (EditText) this.findViewById(R.id.newuserpassword);
final EditText EmailAddress = (EditText) this.findViewById(R.id.user_email);
TextView neighbourView = new TextView(this);
Button button_test2;
button_test2 = (Button) this.findViewById(R.id.button);
button_test2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View s) {
final String newusersname = newuser.getText().toString();
final String newpasswoord = newuserpassword.getText().toString();
final String newusermail = EmailAddress.getText().toString();
ParseUser user = new ParseUser();
user.setUsername(String.valueOf(newusersname));
user.setPassword(String.valueOf(newpasswoord));
user.setPassword(String.valueOf(newusermail));
user.signUpInBackground(new SignUpCallback() {
#Override
public void done( com.parse.ParseException e) {
if (e == null) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
} else {
// Sign up didn't succeed. Look at the ParseException
// to figure out what went wrong
Toast.makeText(getApplicationContext(), "choose a differnt username or password", Toast.LENGTH_LONG).show();
}
//if e != null, something went wrong
}
});
}
});
My XML is
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="New User Name"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:singleLine="true"
android:inputType="textPersonName"
android:id="#+id/newuser" />
<!-- Password Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="#string/password"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:singleLine="true"
android:password="true"
android:inputType="textPassword"
android:id="#+id/newuserpassword"
android:layout_gravity="center_horizontal" />
<!-- Email Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="E-Mail"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:singleLine="true"
android:inputType="textEmailAddress"
android:id="#+id/user_email" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done"
android:id="#+id/button"
android:layout_gravity="center_horizontal" />
And here is my ParseApplication method
public class ParseApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
Parse.enableLocalDatastore(this);
Parse.initialize(this, "xxxxxx ", "xxxx");
//...Rest of the Parse initializations.
}
Any assistance would be greatly appreciated.

You might not have logged out from your previous session. Add a log out logic and check.
Replace this
final String newusersname = newuser.getText().toString();
final String newpasswoord = newuserpassword.getText().toString();
final String newusermail = EmailAddress.getText().toString();
ParseUser user = new ParseUser();
user.setUsername(String.valueOf(newusersname));
user.setPassword(String.valueOf(newpasswoord));
user.setPassword(String.valueOf(newusermail));
with this
final String newusersname = newuser.getText().toString();
final String newpasswoord = newuserpassword.getText().toString();
final String newusermail = EmailAddress.getText().toString();
if(ParseUser.getCurrentUser()!=null)
{
ParseUser.getCurrentUser().logOut();
}
ParseUser user = new ParseUser();
user.setUsername(String.valueOf(newusersname));
user.setPassword(String.valueOf(newpasswoord));
user.setPassword(String.valueOf(newusermail));

try this saveInBackground() method that save the user.
ParseUser user = new ParseUser();
user.setUsername(String.valueOf(newusersname));
user.setPassword(String.valueOf(newpasswoord));
user.setPassword(String.valueOf(newusermail));
user.saveInBackground();

I'm New to this but I think the issue maybe that you trying to assign 2 passwords,
for my understanding its a field you cant duplicate and granted email has a separate field already built inside the user object
user.setUsername(String.valueOf(newusersname));
user.setPassword(String.valueOf(newpasswoord));
user.setEmail(String.valueOf(newusermail));

Related

TextEdit validation overlays show password icon

I have a password TextEdit field that im currently doing validation on and it displays an icon at the end of the TextEdit field to toggle the actual text. When someone enterted in a incorrect password or the passwords dont match it displays an error icon indicating that there was an error with the text entered and this error icon goes right underneath the "show text" icon for the TextEdit field. How do I move either the error icon from validation, or how do I move the "show text" icon?
app displaying both error icon and show text icon
RegisterActivity
public class RegisterActivity extends AppCompatActivity {
private static final String TAG = "RegisterActivity";
#InjectView(R.id.input_name) EditText _nameText;
#InjectView(R.id.input_email) EditText _emailText;
#InjectView(R.id.input_password) EditText _passwordText;
#InjectView(R.id.input_confirmPassword) EditText _confirmPasswordText;
#InjectView(R.id.btn_signup) Button _signupButton;
#InjectView(R.id.link_login) TextView _loginLink;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
ButterKnife.inject(this);
_signupButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
signup();
}
});
_loginLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Finish the registration screen and return to the Login activity
finish();
}
});
}
public void signup() {
Log.d(TAG, "Begin Signup process...");
if (!validate()) {
onSignupFailed();
return;
}
_signupButton.setEnabled(false);
final ProgressDialog signupProgressDialog = new ProgressDialog(RegisterActivity.this,
R.style.Theme_IAPTheme);
signupProgressDialog.setIndeterminate(true);
signupProgressDialog.setMessage("Creating Account...");
signupProgressDialog.show();
String name = _nameText.getText().toString();
String email = _emailText.getText().toString();
String password = _passwordText.getText().toString();
String confirmPassword = _confirmPasswordText.getText().toString();
// TODO: Implement your own signup logic here.
Response.Listener<String> responseListener = new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
Log.i("tagconvertstr", "["+response+"]");
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
onSignupSuccess();
} else {
onSignupFailed();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(name, email, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
/*new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
// On complete call either onSignupSuccess or onSignupFailed
// depending on success
onSignupSuccess();
// onSignupFailed();
progressDialog.dismiss();
}
}, 3000);*/
}
public void onSignupSuccess() {
Toast.makeText(getBaseContext(), "Signup Successful", Toast.LENGTH_LONG).show();
_signupButton.setEnabled(true);
setResult(RESULT_OK, null);
finish();
}
public void onSignupFailed() {
Toast.makeText(getBaseContext(), "Signup Failed", Toast.LENGTH_LONG).show();
_signupButton.setEnabled(true);
}
public boolean validate() {
boolean valid = true;
boolean psisequal;
String name = _nameText.getText().toString();
String email = _emailText.getText().toString();
String password = _passwordText.getText().toString();
String confirmPassword = _confirmPasswordText.getText().toString();
if (name.isEmpty() || name.length() < 3) {
_nameText.setError("at least 3 characters");
valid = false;
} else {
_nameText.setError(null);
}
if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
_emailText.setError("enter a valid email address");
valid = false;
} else {
_emailText.setError(null);
}
if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
_passwordText.setError("between 4 and 10 alphanumeric characters");
valid = false;
}else {
_passwordText.setError(null);
}
if (password.equals(confirmPassword)){
_confirmPasswordText.setError(null);
psisequal = true;
}else {
_confirmPasswordText.setError("passwords do not match");
valid = false;
psisequal = false;
}
return valid;
}
}
activity_register
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
android:background="#color/black">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="56dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<ImageView android:src="#drawable/logo"
android:layout_width="wrap_content"
android:layout_height="72dp"
android:layout_marginBottom="24dp"
android:layout_gravity="center_horizontal" />
<!-- Name Label -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textColor="#ffffff"
android:textColorHint="#ffffff">
<EditText android:id="#+id/input_name"
android:theme="#style/MyEditTextTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:hint="Trainer Name (Gamer Tag)" />
</android.support.design.widget.TextInputLayout>
<!-- Email Label -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textColor="#ffffff"
android:textColorHint="#ffffff">
<EditText android:id="#+id/input_email"
android:theme="#style/MyEditTextTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="E-Mail Address" />
</android.support.design.widget.TextInputLayout>
<!-- Password Label -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textColor="#ffffff"
android:textColorHint="#ffffff">
<EditText android:id="#+id/input_password"
android:theme="#style/MyEditTextTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Password"/>
</android.support.design.widget.TextInputLayout>
<!-- Confirm Password Label -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textColor="#ffffff"
android:textColorHint="#ffffff">
<EditText android:id="#+id/input_confirmPassword"
android:theme="#style/MyEditTextTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Confirm Password"/>
</android.support.design.widget.TextInputLayout>
<!-- Signup Button -->
<android.support.v7.widget.AppCompatButton
android:id="#+id/btn_signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginBottom="24dp"
android:padding="12dp"
android:text="Create Account"/>
<TextView android:id="#+id/link_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="Already a member? Login"
android:textColor="#ffffff"
android:gravity="center"
android:textSize="16dip"/>
</LinearLayout>
</ScrollView>
Strings
<resources xmlns:android="http://schemas.android.com/tools">
<string name="app_name">The World of Go</string>
<string name="title_activity_maps">Map</string>
<!--CODE FOR BUTTON OVERLAY-->
<string name="Popular"></string>
<string name="AZ"></string>
<string name="Category"></string>
<string name="NearBy"></string>
<color name="bg_color">#ffffff</color>
<color name="black">#222222</color>
<color name="white">#ffffff</color>
<style name="MyEditTextTheme">
<item name="colorControlNormal">#ffffff</item>
<item name="colorControlActivated">#ffffff</item>
<item name="colorControlHighlight">#ffffff</item>
<item name="colorAccent">#android:color/white</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textColorHint">#ffffff</item> />
</style>
<string name="type_prompt">Choose a Type</string>
<string-array name="type_arrays">
<item>Pokestop</item>
<item>Gym</item>
</string-array>
</resources>
Try these methods to apply validation check:
private TextInputLayout text_email,text_pass, text_confirm_pass;
private EditText email,pass,confirm_pass;
text_pass = (TextInputLayout) findViewById(R.id.input_layout_password);
email = (EditText) findViewById(R.id.email);
private boolean validatePassword() {
if (pass.getText().toString().trim().isEmpty()) {
pass.setError(getString(R.string.err_msg_pass));
requestFocus(text_pass);
return false;
}else if(pass.getText().toString().length()<8) {
pass.setError(getString(R.string.err_msg_pass_length));
requestFocus(text_pass);
return false;
}else {
text_pass.setErrorEnabled(false);
}
return true;
}
To get focus :
private void requestFocus(View view) {
if (view.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
Here is my solution
login xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/grey"
android:fillViewport="true">
<LinearLayout
android:id="#+id/login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="5dp"
android:text="LOGIN"
android:textSize="25sp" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:textColor="#ffffff"
android:textColorHint="#ffffff">
<EditText
android:id="#+id/etUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Trainer Name (Gamer Tag)"
android:inputType="textCapWords"
android:padding="10dp"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:textColor="#ffffff"
android:textColorHint="#ffffff">
<EditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="#drawable/ic_visibility_off_white_24dp"
android:hint="Password"
android:inputType="textPassword"
android:padding="10dp"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/btnSignIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Sign In"
android:textColor="#android:color/white" />
</LinearLayout>
</RelativeLayout>
Login class
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnLogin;
private EditText etUserName, etPassword;
private String userName, password;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initValues();
}
private void initValues() {
try {
btnLogin = (Button) findViewById(R.id.btnSignIn);
btnLogin.setOnClickListener(this);
etUserName = (EditText) findViewById(R.id.etUserName);
etPassword = (EditText) findViewById(R.id.etPassword);
etPassword.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (event.getRawX() >= (etPassword.getRight() - etPassword.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
if (etPassword.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
etPassword.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD);
etPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_visibility_off_white_24dp, 0);
etPassword.setSelection(etPassword.getText().length());
} else {
etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
etPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_visibility_white_24dp, 0);
}
return true;
}
}
return false;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.btnSignIn:
validateCredentials();
onLogin();
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void validateCredentials() {
try {
userName = etUserName.getText().toString();
password = etPassword.getText().toString();
if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(password)) {
etUserName.setError("enter a valid username");
etPassword.setError("enter a correct password");
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void onLogin() {
//your login implementation
}
}
Screenshots
So here is what I had to do to fix this. I disabled the standard text view of the textEdit by doing the following:
app:passwordToggleEnabled="false"
This will disable the view of the icon to toggle the password visibility. Then What I did was to add an imageView and added my own eye icon to the app:
<ImageView
android:id="#+id/imageView_registerPasswordVisibility"
android:layout_width="24dp"
android:layout_height="24dp"
android:clickable="true"
android:src="#drawable/eye"
android:layout_row="4"
android:layout_column="1"
android:foregroundGravity="center_vertical"
android:layout_gravity="center_vertical" />
I converted my layout to a grid layout and put this image view in the next column to align it to the end of the textEdit, the reason behind this is cause we now show the icon AFTER the textEdit instead of inside the textEdit which where the error validation icon will appear. Doing this will fix the conflict, I handled the imageView to do the same thing as passwordToggle. So here is the entire code for all of that which will also show how to set errors properly on the TextInputLayout if that is what you are using. If not then set errors as you would normally (can be found in standard android documentation):
RegisterActivity.java
public class RegisterActivity extends AppCompatActivity {
//Setup global variables for all of the user interface items.
#InjectView(R.id.wrapper_registerPassword)
TextInputLayout _registerPasswordWrapper; /*this is only needed for when you use TextInputLayout that gives us the ability to have animated hints by default. If you are ONLY using editText then you will not need the code for this wrapper.*/
#InjectView(R.id.editText_registerPasswordInput)
EditText _registerPasswordInput; /*This is to access text that was typed in the editText*/
#InjectView(R.id.imageView_registerPasswordVisibility)
ImageView _registerPasswordVisibility; /*This is used for our image that we will be adding listeners for to do speacial features when the image is pressed. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
ButterKnife.inject(this);
/*Set a listener for the password view image to display the text inside the password text
field for when the image is pressed.*/
_registerPasswordVisibility.setOnTouchListener(mPasswordVisibleTouchListener);
}
/*Listener for the toggle password icon.*/
private View.OnTouchListener mPasswordVisibleTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final boolean isOutsideView = event.getX() < 0 ||
event.getX() > v.getWidth() ||
event.getY() < 0 ||
event.getY() > v.getHeight();
// change input type will reset cursor position, so we want to save it
final int cursor = _registerPasswordInput.getSelectionStart();
if (isOutsideView || MotionEvent.ACTION_UP == event.getAction())
/*This will make the field display dots as a password text field should look like.*/
_registerPasswordInput.setInputType( InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD);
else
/*This will make the text in the password text field visibile while we are pressing down on our image.*/
_registerPasswordInput.setInputType( InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
_registerPasswordInput.setSelection(cursor);
return true;
}
};
public boolean validate() {
boolean valid = true;
//Obtain user's entered in credentials to validate them.
String password = _registerPasswordInput.getText().toString();
//Password validation.
if (password.isEmpty() || password.length() < 8 || password.length() > 30) {
_registerPasswordWrapper.setError("Please provide a stronger password.\n" +
"•Password must be the following:\n" +
"•At least 8 characters");
_registerPasswordWrapper.setErrorEnabled(true);
valid = false;
}else {
_registerPasswordWrapper.setError(null);
}
return valid;
}
}
activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android.support.design="http://schemas.android.com/tools"
android:fitsSystemWindows="true"
android:background="#color/black">
<!-- Start of Grid Layout -->
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="56dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:orientation="vertical">
<!-- Password Text Field -->
<android.support.design.widget.TextInputLayout
android:id="#+id/wrapper_registerPassword"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textColor="#ffffff"
app:passwordToggleEnabled="false"
android:textColorHint="#ffffff"
android:layout_column="0"
android:layout_row="4"
android:layout_gravity="fill_horizontal">
<android.support.design.widget.TextInputEditText
android:id="#+id/editText_registerPasswordInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="30"
android:inputType="textPassword"
android:hint="Password" />
</android.support.design.widget.TextInputLayout>
<!-- Password Toggle Icon for Password field -->
<ImageView
android:id="#+id/imageView_registerPasswordVisibility"
android:layout_width="24dp"
android:layout_height="24dp"
android:clickable="true"
android:src="#drawable/eye"
android:layout_row="4"
android:layout_column="1"
android:foregroundGravity="center_vertical"
android:layout_gravity="center_vertical" />
</GridLayout>
</ScrollView>
I hope this helps out some people, if anyone has anymore questions please let me know and ill help out to the best of my abilities!
If you're looking to only have one icon displayed this could do the trick.
Change the PasswordVisibilityToggleTintMode property.
You can hide the password toggle icon via:
setPasswordVisibilityToggleTintMode(PorterDuff.Mode.CLEAR)
Make it visible again via:
setPasswordVisibilityToggleTintMode(PorterDuff.Mode.MULTIPLY)
Please use app:endIconDrawable="#null" for TextInputLayout in your xml.
First, you need to wrap your edit text in an TextInputLayout.
Then, you should set the errorIconDrawable to null when you set the text error to something, so it won't override the toggle password icon.
Like this:
if (hasError) {
textInputLayout.error = "Error text"
textInputLayout.errorIconDrawable = null
}
It is easier to use:
TextInputLayout.isEndIconVisible
you can show or hide the Eye icon by setting it true or false.
for example when there is an error do this to hide the eye icon so the error icon is visible:
passwordTextInputLayout.isEndIconVisible = false
and then when the user starts typing again show it like this:
passTextView.afterTextChanged {
passwordTextInputLayout.isEndIconVisible = true
}

Android studio: Error on sending Variable to Mysql after Toast

Hello I am new to Android, but reading as much as possible;
I create an APP for entering Trip Reports from Trucking. I have the form corrected, but using VAL1-VAL2, then trying to send that using "registerAPI" is giving me failures.
So created RegisterAPI class of
public interface RegisterAPI {
#FormUrlEncoded
#POST("/insert.php")
public void insertUser(
#Field("trucksID") String trucksID,
#Field("tripReportNumber") String tripReportNumber,
#Field("enteredDate") String enteredDate,
#Field("emptyMilage") String emptyMilage,
#Field("loadedMilage") String loadedMilage,
#Field("estTotal") String estTotal,
Callback<Response> callback);
Then in the MainActivity:
//Class for our main activity with OnClickListener
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//Declaring views
private WebView webView;
private EditText trucksID;
private EditText tripReportNumber;
private EditText enteredDate;
private EditText emptyMilage;
private EditText loadedMilage;
private Button buttonRegister;
private String estTotal;
private String estMilage;
//This is our root url
public static final String ROOT_URL = "http://hosturl/php/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing Views
trucksID = (EditText) findViewById(R.id.trucksID);
tripReportNumber = (EditText) findViewById(R.id.tripReportNumber);
enteredDate = (EditText) findViewById(R.id.enteredDate);
emptyMilage= (EditText) findViewById(R.id.emptyMilage);
loadedMilage = (EditText) findViewById(R.id.loadedMilage);
buttonRegister = (Button) findViewById(R.id.buttonRegister);
//Adding listener to button
buttonRegister.setOnClickListener(this);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.openWebsite:
Intent website = new Intent(this,ourWebsite.class);
this.startActivity(website);
return true;
case R.id.goto2:
Intent activity2 = new Intent(this,MainActivity2.class);
this.startActivity(activity2);
return true;
case R.id.about_us:
Intent about = new Intent(this,aboutUs.class);
this.startActivity(about);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void insertUser(){
//Here we will handle the http request to insert user to mysql db
//Creating a RestAdapter
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(ROOT_URL) //Setting the Root URL
.build(); //Finally building the adapter
//Creating object for our interface
RegisterAPI api = adapter.create(RegisterAPI.class);
//Defining the method insertuser of our interface
api.insertUser(
//Passing the values by getting it from editTexts
trucksID.getText().toString(),
tripReportNumber.getText().toString(),
enteredDate.getText().toString(),
emptyMilage.getText().toString(),
loadedMilage.getText().toString(),
//can take this out below
estTotal,
//Creating an anonymous callback
new Callback<Response>() {
#Override
public void success(Response result, Response response) {
//On success we will read the server's output using bufferedreader
//Creating a bufferedreader object
BufferedReader reader = null;
//An string to store output from the server
String output = "";
try {
//Initializing buffered reader
reader = new BufferedReader(new InputStreamReader(result.getBody().in()));
//Reading the output in the string
output = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
//Displaying the output as a toast
Toast.makeText(MainActivity.this, output, Toast.LENGTH_LONG).show();
}
#Override
public void failure(RetrofitError error) {
//If any error occured displaying the error as toast
Toast.makeText(MainActivity.this, error.toString(),Toast.LENGTH_LONG).show();
}
}
);
}
//Overriding onclick method
#Override
public void onClick(View v) {
//Calling insertUser on button click
insertUser();
int value1=Integer.parseInt(emptyMilage.getText().toString());
int value2=Integer.parseInt(loadedMilage.getText().toString());
int estMilage=value2-value1;
Toast.makeText(this, "Estimated Milage:"+String.valueOf(estMilage), Toast.LENGTH_SHORT).show();
}
}
Here is the activity_main.xml i am using:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/logoview"
android:src="#mipmap/platelogo"
android:contentDescription="#string/atrixlogo"
android:maxHeight="55dp"
android:minHeight="55dp"
android:minWidth="55dp"
android:maxWidth="55dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<TextView
android:text="Enter Truck ID:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/trucksID"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="Enter Trip Report Number:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/tripReportNumber"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="Enter Trip Date:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/enteredDate"
android:inputType="date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="MM/DD/YY" />
<TextView
android:text="Enter Empty Milage:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/emptyMilage"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="Enter Loaded Milage:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/loadedMilage"
android:inputType="date"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/buttonRegister"
android:text="Submit Trip Report"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
But all i am seeing is the Toast, not carrying forward and inserting the estTotal into the Database onClick
When I run message:
Error:(112, 25) error: cannot find symbol method getText()
Error:(102, 12) error: method insertUser in interface RegisterAPI cannot be applied to given types;
required: String,String,String,String,String,String,Callback<Response>
found: String,String,String,String,String,<anonymous Callback<Response>>
reason: actual and formal argument lists differ in length
If i take out the estTotal.gettext from the Mainactivity and RegisterAPI no errors on running.
I have been looking at a lot of the StackOverflow's but most of those are on one intent or one activity. Any help would be awesome.
The problem is here: estTotal.getText().toString() - because you are trying to call method getText() on estTotal - but estTotal is type String, not EditText or TextView - this is how you declared it private String estTotal;.
So removing the line estTotal.getText().toString() should solve the problem. What are you using estTotal for anyway?
I hope this helps.

how to get the value of edittext

i have a feedback activity and it has an EditText and a button :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="230dp"
android:background="#color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:text="#string/bugtitle"
android:textSize="35sp"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:textColor="#000000"
android:id="#+id/BUG_title"
android:fontFamily="sans-serif-thin"/>
<EditText
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="#+id/BUG_title"
android:layout_marginTop="15dp"
android:layout_marginRight="30dp"
android:id="#+id/et_bug"
android:layout_marginLeft="30dp"
android:fontFamily="sans-serif-light"
android:hint="#string/bug_et"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:text="SEND"
android:fontFamily="sans-serif-light"
android:id="#+id/send_bug"/>
</RelativeLayout>
so user write his feedback and when he clicked on SEND button it should send to Parse host with this code:
et1 = (EditText)findViewById(R.id.et_bug);
ParseObject bugsend = new ParseObject("BUGS");
bugsend.put("BUG_REPORT", et1.getText().toString());
bugsend.put("OS", OS_VERSION);
bugsend.put("MODEL", PHONE_MODEL);
bugsend.saveInBackground();
and problem is here when users sends his feedback a null text will be send to Parse host
also i have tried this one:
et1 = (EditText)findViewById(R.id.et_bug);
String BUG_SEND = et1.getText().toString();
ParseObject bugsend = new ParseObject("BUGS");
bugsend.put("BUG_REPORT", BUG_SEND);
bugsend.put("OS", OS_VERSION);
bugsend.put("MODEL", PHONE_MODEL);
bugsend.saveInBackground();
but same as the first one
and then i have tried SharedPreferences and saving the value to SP and send SP value to Parse but its null :|
this is the feedback dialog:
public void onBug(View view) {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.bug_dialog);
Button dialogButton = (Button) dialog.findViewById(R.id.send_bug);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String PHONE_MODEL = android.os.Build.MODEL;
final String OS_VERSION = Build.VERSION.RELEASE;
et1 = (EditText)findViewById(R.id.et_bug);
String BUG_SEND = et1.getText().toString();
ParseObject bugsend = new ParseObject("BUGS");
bugsend.put("BUG_REPORT", BUG_SEND);
bugsend.put("OS", OS_VERSION);
bugsend.put("MODEL", PHONE_MODEL);
bugsend.saveInBackground();
Toast.makeText(getApplication(),"Thank You We Will Check This Report",Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
dialog.show();
}
anyone knows how i should fix this?
EDIT: I've already changed a bit your code:
public void onBug(View view) {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.bug_dialog);
Button dialogButton = (Button) dialog.findViewById(R.id.send_bug);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ParseObject bugsend = new ParseObject("BUGS");
bugsend.put("BUG_REPORT", et1.getText().toString()).commit();
bugsend.put("OS", Build.VERSION.RELEASE;).commit();
bugsend.put("MODEL", android.os.Build.MODEL).commit();
bugsend.saveInBackground(); //THIS GUY
Toast.makeText(getApplication(),"Thank You We Will Check This Report",Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
dialog.show();
}
If you're using SharedPreferences you need after putting variable, also commit it like in code above. I don't know what is doing your saveInBackground() method, but i think that adding .commit() after every argument should also work.
Please check it and let me know if it won't work

Weird Imagebutton result

I try to make a image button for login. But the result is weird. Please see the attachment.
The weird thing is the image button is inside the button...
Hope for helps.
this is xml code...
<ImageButton
android:id="#+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/chkRememberMe"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:src="#drawable/login_off" />
this is java code for the login button...
imageButtonLogin = (ImageButton) findViewById(R.id.loginButton);
imageButtonLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String inputPassword = passwordEditText.getText().toString();
if (password.isEmpty()) {
showDialog(DIALOG_ALERT);
} else {
String inputUserName = userNameEditText.getText()
.toString();
Contact contact = new Contact();
contact.setUsername(inputUserName);
contact.setPassword(inputPassword);
if (contactDb.searchContact(contact)) {
// logged in
/*Toast.makeText(getApplicationContext(),
getResources().getString(R.string.loggedIn),
Toast.LENGTH_LONG).show();*/
Intent newActivity = new Intent();
//go to AudioRecoder page
newActivity
.setClass(MainActivity.this, AudioActivity.class);
startActivity(newActivity);
} else {
// login failed
showDialog(DIALOG_ALERT);
}
}
}
You need to use
android:background="#drawable/login_off"
instead of src like you are.
<ImageButton
android:id="#+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/chkRememberMe"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#null"
android:src="#drawable/login_off" />
you can set the background or trasparent android:background="#00FFFFFF" or to null android:background="#null"

EditText not returning content on getText()

The code snippet below displays a Dialog with a simple login-form. The problem is that when the user hits the login-button, the text that was entered into the EditTexts are not returned on the getText()-call. However, if I set android:setText="foo" on the EditTexts in the xml-layout "foo" is returned on getText(). Any idea why the text entered at runtime won't stick?
private void showLoginDisplay() {
loginDialog = new Dialog(GOFdroid.this);
loginDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
loginDialog.setTitle("Logga in");
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = li.inflate(R.layout.login_dialog, null);
loginDialog.setContentView(dialogView);
loginDialog.show();
Button loginButton = (Button) dialogView.findViewById(R.id.login_button);
Button cancelButton = (Button) dialogView.findViewById(R.id.cancel_button);
EditText unameView = (EditText) dialogView.findViewById(R.id.uname_id);
if (unameView != null)
Log.d(TAG, "unameView != null");
Log.d(TAG, "uname.getText(): " + unameView.getText().toString());
uname = unameView.getText().toString();
EditText pwdView = (EditText) dialogView.findViewById(R.id.pwd_id);
if (pwdView != null)
pwd = pwdView.getText().toString();
Log.d(TAG, "uname = " + uname + ", pwd = " + pwd);
loginButton.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
Log.d(TAG, "Clicked <logga in>");
loginDialog.dismiss();
viewEvents = new Runnable() {
//#Override
public void run() {
getEvents(uname, pwd);
}
};
Thread thread = new Thread(null, viewEvents, "MagentoBackground");
thread.start();
pDialog = ProgressDialog.show(GOFdroid.this,
"Uppdaterar...", "Hämtar registrerade körningar...", true);
}
});
}
and the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="10dip"
android:paddingRight="10dip"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/uname_label"/>
<EditText
android:id="#+id/uname_id"
android:layout_width="220dip"
android:layout_height="wrap_content"
android:text="foo" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/pwd_label"
android:paddingTop="10dip"/>
<EditText
android:id="#+id/pwd_id"
android:layout_width="220dip"
android:layout_height="wrap_content"
android:text=""
android:inputType="textPassword" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="15dip"
>
<Button
android:id="#+id/login_button"
android:layout_width="100dip"
android:layout_height="fill_parent"
android:text="Logga in" />
<Button
android:id="#+id/cancel_button"
android:layout_width="100dip"
android:layout_height="fill_parent"
android:text="Avbryt" />
</LinearLayout>
</LinearLayout>
The problem is that you are not updating uname and pwd within the anonymous inner class. You only update uname and pwd when the showLoginDisplay() method is called. When the user changes the text, and hits the login button the onClick() method is called.
Within the onClick() method of the inner class you want to do:
uname = unameView.getText();
pwd = pwdView.getText();
You will have to make unameView and pwdView global/field variables, so they are accessable by the anonymous inner class.
The reason you have to do this is because when the login button is pressed, only the code with the anonymous inner class's onClick is run.
I hope that fixes the problem.

Categories

Resources