I created registration screen i want to disable submit button and it should enable only when all the fields get correct input, bellow i added list of text fields these fields must get correct input and submit button must enable.
protected boolean validateData() {
if (email.getText().toString().trim().length() == 0) {
email.setError("Email-ID/Registered Number can not be empty!");
return false;
} else if (!(email.getText().toString().trim().contains("#"))
&& email.getText().toString().trim().length() < 10) {
email.setError("Email/Registered Number not valid!");
return false;
} else if (!(email.getText().toString().trim().contains("."))
&& email.getText().toString().trim().length() < 10) {
email.setError("Email/Registered Number not valid!");
return false;
}
else if (!(email.getText().toString().trim().contains("#"))
&& !(email.getText().toString().trim().contains("."))
&& email.getText().toString().trim().length() > 10) {
email.setError("Email/Registered Number not valid!");
return false;
}
else if (email.getText().toString().trim().contains("#")
|| email.getText().toString().trim().contains(".")) {
if (!WebServiceSingleTon.getInstance().isEmailValid(
email.getText().toString().trim())) {
email.setError("Email is not valid!");
return false;
} else if (password.getText().toString().trim().length() == 0) {
password.setError("password can not be empty!");
return false;
} else if (password.getText().toString().trim().length() < 6) {
password.setError("Your password need to have minimum 6 characters!");
return false;
} else if (password.getText().toString().trim().length() > 20) {
password.setError("Your password need to have maximum 20 characters!");
return false;
}
} else if (password.getText().toString().trim().length() == 0) {
password.setError("password can not be empty!");
return false;
} else if (password.getText().toString().trim().length() < 6) {
password.setError("Your password need to have minimum 6 characters!");
return false;
} else if (password.getText().toString().trim().length() > 20) {
password.setError("Your password need to have maximum 20 characters!");
return false;
}
return true;
}
to make a button "disabled" You'll need to set it's android:clickable property to false. Then, set a custom background on it that changes its background to gray (or however you want to it look while its disabled). Then after you call that validateData() function you can check to see if you should change the background of the button to be "enabled"
Related
if (buttonClicked.contains(1) && buttonClicked.contains(2)) {
if (playerOneLastClicked) {
imgViewBackground1.setImageResource(R.drawable.rca)
} else {
imgViewBackground1.setImageResource(R.drawable.wac)
}
}
if (buttonClicked.contains(3) && buttonClicked.contains(4) && buttonClicked.contains(5)) {
if (playerOneLastClicked) {
imgViewBackground2.setImageResource(R.drawable.rca)
} else {
imgViewBackground4.setImageResource(R.drawable.wac)
}
}
when player1 or player2 last click in (buttonClicked.contains(1) && buttonClicked.contains(2)) imgViewBackgound1 change but when the players click button 3,4 or 5 imgViewBackground keep changing.
I want when imgViewBackground1 take (R.drawable.rca) or (R.drawable.wac) don't change any more.
If imgViewBackground is not going to have an imageview you could just add an extra check e.g.
if (imgViewBackground1.drawable == null) {
if (playerOneLastClicked) {
imgViewBackground1.setImageResource(R.drawable.rca)
} else {
imgViewBackground1.setImageResource(R.drawable.wac)
}
}
}
or if it would have an imageview already and you don't want it set again
if (imgViewBackground1.drawable != resources.getDrawable(R.drawable.rcs, null)
|| imgViewBackground1.drawable != resources.getDrawable(R.drawable.wac, null))
if (playerOneLastClicked) {
imgViewBackground1.setImageResource(R.drawable.rca)
} else {
imgViewBackground1.setImageResource(R.drawable.wac)
}
}
}
Currently I have a list of TextViews whose background get changed when click for the first time. But I also want when user clicks on the second time its background should again change and vice versa.
With my existing code I am only able to change background for the first click, when I am clicking it again its background is not changing.
Here is my code:
public void onClick(View v) {
switch (v.getId()) {
case R.id.goalText1:
if (count <= 2) {
goals.add(mGoal1.getText().toString());
mGoal1.setTextColor(getResources().getColor(R.color.black));
mGoal1.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText2:
if (count <= 2) {
goals.add(mGoal2.getText().toString());
mGoal2.setTextColor(getResources().getColor(R.color.black));
mGoal2.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText3:
if (count <= 2) {
goals.add(mGoal3.getText().toString());
mGoal3.setTextColor(getResources().getColor(R.color.black));
mGoal3.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText4:
if (count <= 2) {
goals.add(mGoal4.getText().toString());
mGoal4.setTextColor(getResources().getColor(R.color.black));
mGoal4.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText5:
if (count <= 2) {
goals.add(mGoal5.getText().toString());
mGoal5.setTextColor(getResources().getColor(R.color.black));
mGoal5.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText6:
if (count <= 2) {
goals.add(mGoal6.getText().toString());
mGoal6.setTextColor(getResources().getColor(R.color.black));
mGoal6.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText7:
if (count <= 2) {
goals.add(mGoal7.getText().toString());
mGoal7.setTextColor(getResources().getColor(R.color.black));
mGoal7.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btnGoal:
Intent intent = new Intent(this, fiteness_level_selection.class);
try {
obj.put("SelectedGoal", goals);
} catch (Exception e) {
e.printStackTrace();
}
intent.putExtra("GoalJson", obj.toString());
startActivity(intent);
break;
}
So can anybody suggest me any easy way to achieve it.
Set a boolean array in your activity.
//suppose you've 3 textviews within listview
public boolean isTransparent[] = {false,false,false};
And then do this in your onClick method.
public void onOnclick(View v){
switch (v.getId()) {
//foreach textview do this
case R.id.textView1 :
int color;
if (isTransparent[0])
color = getResources().getColor(android.R.color.transparent);
else
color = getResources().getColor(R.color.white);
isTransparent[0]=!isTransparent[0];
textview1.setBackgroundColor(color);
break;
case R.id.textView2:
// now check for isTransparent[1]
and so on ...
...
}
}
As What I am understand is you want Change background for that you have take bool variable and in Button click you have to check the bool value.
Boolean click_firsttime = false;
Inside your Button click
use this way
If(click_firsttime == false){
//perform task
// and set bool value to true
click_firsttime = true;
}else{
// perform task and set bool value
click_firsttime = false;
}
First create a boolean :
private boolean foo = true;
Then change your code like this :
public void onClick(View v) {
switch (v.getId()) {
case R.id.goalText1:
if (foo) {
goals.add(mGoal1.getText().toString());
mGoal1.setTextColor(getResources().getColor(R.color.black));
mGoal1.setBackgroundColor(getResources().getColor(R.color.white));
count++;
foo = false;
} else {
// set background to another color
foo = true;
}
break;
I have an activity in which there are three EditTexts: First Name, Middle Name, Last Name, and a Submit button. If the user submits with a field blank, a different toast appears for different field. Here's is an example when button will be clicked!:
I'll assume you can figure out how to use a button listener yourself. Inside that button listener, you can use this code:
// create EditText references
EditText etFirstName = (EditText)findViewById(R.id.firstName);
EditText etMiddleName = (EditText)findViewById(R.id.middleName);
EditText etLastName = (EditText)findViewById(R.id.lastName);
// boolean variables that each represent whether or not the each field is blank
// if the EditText length is 0, it's true (blank); otherwise, it's false (filled)
boolean firstNameBlank = etFirstName.length() == 0;
boolean middleNameBlank = etMiddleName.length() == 0;
boolean lastNameBlank = etLastName.length() == 0;
if (firstNameBlank && middleNameBlank && lastNameBlank) {
// toast all three blank
}
else if (firstNameBlank && middleNameBlank) {
// toast just first and middle are blank
}
else if (firstNameBlank && lastNameBlank) {
// toast just first and last are blank
}
else if (middleNameBlank && lastNameBlank) {
// toast just middle and last are blank
}
else if (firstNameBlank) {
// toast just first is blank
}
else if (middleNameBlank) {
// toast just middle is blank
}
else if (lastNameBlank) {
// toast just last is blank
}
else {
// none are blank
}
Try this way
EditText e = (EditText)findViewById(R.id.EDITTEXT));
if(e.getText().length == 0){
//Show Toast
}else{
//continue your code
}
implement onClick() event of your submit button as below:
#Override
public void onClick(View view) {
super.onClick(view);
switch (view.getId()) {
case R.id.btnSubmit:
if(edtFirstName.getText().toString().length() < 1){
Toast.makeText(this, "please fill all boxes.",Toast.LENGTH_LONG).show();
}else if(edtSecondName.getText().toString().length() < 1){
Toast.makeText(this, "please fill middle name and last name.",Toast.LENGTH_LONG).show();
} else if(edtLastName.getText().toString().length() < 1){
Toast.makeText(this, "please fill last name.",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "please wait...processing.",Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
I'm trying to have the maximum character of password that i want to set in my android apps. What would be the best way to code it ? I only have the minimum of 6 character and i want to have maximum of 10 character set password. Here is my attempt:
public void UserSignUp() {
if(ET_PASSWORD_SIGNUP.getText().length() >0 && ET_CONFIRM_PASSWORD_SIGNUP.getText().length() >0 &&
ET_PASSWORD_SIGNUP.getText().length() >5 && ET_CONFIRM_PASSWORD_SIGNUP.getText().length() >5)
{
if(ET_PASSWORD_SIGNUP.getText().toString().equals(ET_CONFIRM_PASSWORD_SIGNUP.getText().toString())) {
String SignUpResult = "";
db = DBMANAGER.getWritableDatabase();
SignUpResult = CONTENTMANAGER.saveUserPassword(ET_PASSWORD_SIGNUP.getText().toString(),this);
Toast.makeText(getApplicationContext(), SignUpResult, Toast.LENGTH_SHORT).show();
ActivityLogIn();
} else {
Toast.makeText(getApplicationContext(), "Confirm Password and Password did not match!",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "Invalid Input", Toast.LENGTH_SHORT).show();
}
}
Not an answer as such but another solution,
Have a look at https://github.com/ragunathjawahar/android-saripaar, nice annotation library.
You can annotate your password fields and it will handle the logic for you...
#Password(order = 1, minLength = 4, maxLength = 10)
private EditText passwordEditText;
#ConfirmPassword(order = 2)
private EditText confirmPasswordEditText;
private boolean checkPW(String pw, String pw2) {
if(!pw.equals(pw2)){
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_pws_do_not_match), Toast.LENGTH_LONG).show();
return false;
}
if (pw.length() < 4) {
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_pw_too_short), Toast.LENGTH_LONG).show();
return false;
}
if(pw.length() > 10){
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_pw_too_long), Toast.LENGTH_LONG).show();
return false;
}
//continue here
return true;
}
});
}
Check this code..
if (ET_PASSWORD_SIGNUP.getText().toString().length() < 6
|| ET_PASSWORD_SIGNUP.getText().toString().length() > 10) {
ET_PASSWORD_SIGNUP.requestFocus();
ET_PASSWORD_SIGNUP.setError("Password should be between 6 to 10 characters long");
}
you can write this code:
// check password
public boolean checkPassword(String password, String confirmPassword) {
boolean state = false;
int passLength = password.length();
if (passLength >= 6) {
if (confirmPassword.length() != 0) {
if (password.equals(confirmPassword)) {
state = true;
} else {
makeToast("Password does't match!");
inputConfirmPassword.setText("");
inputPassword.setText("");
inputPassword.requestFocus();
}
} else {
Toast.makeText(this, "Please enter confirm password",
Toast.LENGTH_SHORT).show();
inputConfirmPassword.setText("");
inputConfirmPassword.requestFocus();
state = false;
}
} else {
Toast.makeText(this, "Please enter 6 digit password",
Toast.LENGTH_SHORT).show();
inputPassword.setText("");
inputPassword.requestFocus();
state = false;
}
return state;
}
I'm currently working on own implementation of Input Method Editor (IME) or can be called as Softkeyboard in Android, I have read creating an input method and I have downloaded the SoftKeyboard sample code provided as part of the SDK. I have the following code from sample Softkeyboard:
private void handleCharacter(int primaryCode, int[] keyCodes) {
if (isInputViewShown()) {
if (mInputView.isShifted()) {
primaryCode = Character.toUpperCase(primaryCode);
}
}
if (isAlphabet(primaryCode) && mPredictionOn) {
/**
* Swapping here with my desired unicode character
* */
if (primaryCode >= 97 && primaryCode <= 122 ) {
mComposing.append(Swap.swapLetters(primaryCode));
}else{
mComposing.append((char) primaryCode);
}
getCurrentInputConnection().setComposingText(mComposing, 1);
updateShiftKeyState(getCurrentInputEditorInfo());
updateCandidates();
} else {
getCurrentInputConnection().commitText(
String.valueOf((char) primaryCode), 1);
}
}
the code given above works fine, but when I click the key:
<Key android:codes="-1"
android:keyWidth="15%p" android:isModifier="true"
android:isSticky="true" android:keyEdgeFlags="left"/>
which is like Shift key, this converts the keys to uppercase I don't know how to show my next keys this key is clicked/pressed, the following is Logcat of exception:
I have traced that this might be occurring in this place where we handle the Shift key :
private void handleShift() {
if (mInputView == null) {
return;
}
Keyboard currentKeyboard = mInputView.getKeyboard();
if (mQwertyKeyboard == currentKeyboard) {
// Alphabet keyboard
checkToggleCapsLock();
mInputView.setKeyboard(mSindhi);
} else if (currentKeyboard == mSymbolsKeyboard) {
mSymbolsKeyboard.setShifted(true);
mInputView.setKeyboard(mSymbolsShiftedKeyboard);
mSymbolsShiftedKeyboard.setShifted(true);
} else if (currentKeyboard == mSymbolsShiftedKeyboard) {
mSymbolsShiftedKeyboard.setShifted(false);
mInputView.setKeyboard(mSymbolsKeyboard);
mSymbolsKeyboard.setShifted(false);
}
}
now I have not any idea how to get rid of exception mentioned above, and get my code working. please solve this one!
There may be some other solutions for your problem, but I have done one trick and came up with following :
Step-1 You have to remove all the calls to the method updateShiftKeyState(getCurrentInputEditorInfo()); in your code like:
private void handleCharacter(int primaryCode, int[] keyCodes) {
if (isInputViewShown()) {
if (mInputView.isShifted()) {
primaryCode = Character.toUpperCase(primaryCode);
}
}
if (isAlphabet(primaryCode) && mPredictionOn) {
/**
* Swapping here with my desired unicode character
* */
if (primaryCode >= 97 && primaryCode <= 122 ) {
mComposing.append(Swap.swapLetters(primaryCode));
}else{
mComposing.append((char) primaryCode);
}
getCurrentInputConnection().setComposingText(mComposing, 1);
//updateShiftKeyState(getCurrentInputEditorInfo()); // you can delete this method from your class and clean-up all the occurances of that
updateCandidates();
} else {
getCurrentInputConnection().commitText(
String.valueOf((char) primaryCode), 1);
}}
Step-2 change your handleShift method to his:
private void handleShift() {
if (mInputView == null) {
return;
}
Keyboard currentKeyboard = mInputView.getKeyboard();
if (mQwertyKeyboard == currentKeyboard) {
mInputView.setKeyboard(mSindhi);
} else if (currentKeyboard == mSymbolsKeyboard) {
mInputView.setKeyboard(mSymbolsShiftedKeyboard);
} else if (currentKeyboard == mSymbolsShiftedKeyboard) {
mInputView.setKeyboard(mSymbolsKeyboard);
}
}
that is all, Hope this may work for you...