Validation android - android

I'm new in android programming but I have a validation function after the user inserts some data like:source point,destination point,...etc
I have 2 spinners :source and destinations ,both include same values..and i want to be able to send a pop up if a user is selecting the same source and destination and the function is this one:
spSursa=is the source spinner
spDestinatie=is the destinations spinner
private boolean validare_Date()
{
if(spSursa.getSelectedItem().toString()!=spDestinatie.getSelectedItem().toString() )
{
if(ratb.isChecked()==false && metrorex.isChecked()==false && both.isChecked()==false)
{
Toast.makeText(getApplicationContext(), R.string.ADAUGA_RUTA_EROARE_TRANSPORT, Toast.LENGTH_SHORT).show();
return false;
}
else
{
return true;
}
}
else {
Toast.makeText(getApplicationContext(), R.string.ADAUGA_RUTA_EROARE_SURSA_DEST, Toast.LENGTH_SHORT).show();
return false;
}
}

Try to change
if(spSursa.getSelectedItem().toString()!=spDestinatie.getSelectedItem().toString() )
To
if(!spSursa.getSelectedItem().toString().equals(spDestinatie.getSelectedItem().toString()))

Related

How to check for tap in monogame?

I want to check that Rectangle was tapped. This mehod does the job and it works almost how I want:
private bool CheckRectangleTouch(Rectangle target)
{
var touchCollection = TouchPanel.GetState();
if (touchCollection.Count > 0)
{
foreach (var touch in touchCollection)
{
if (target.Contains(touch.Position))
{
return true;
}
}
}
return false;
}
Problem I have is that after I've tapped rectangle it keeps returning true until I release it (it can register 10-30 times for one tap) and I want it to return true just once - for the first touch.
I've tried this (replace code inside foreach):
var isFirstTouch = !touch.TryGetPreviousLocation(out _);
if (target.Contains(touch.Position) && isFirstTouch)
{
return true;
}
And this (bad one, I don't really want it to register after release):
if (target.Contains(touch.Position) && touch.State == TouchLocationState.Released)
{
return true;
}
But nothing is does it. Either logic is not consistent or doesn't work at all.
So how do I check for tap?
Update: this works but it's very hacky, has delay and gives me random phantom taps:
try
{
var tap = TouchPanel.ReadGesture(); // falls each time when no input
return tap.GestureType == GestureType.Tap && target.Contains(tap.Position);
}
catch { }
return false;
Here's what I ended up doing:
I have singleton to hold my game state (many different props updated as needed). I added to it:
public TouchCollection TouchCollection { get; set; }
Prop to hold TouchPanel.GetState result. I fill it in Games Update method once per frame, as #craftworkgames suggested:
State.TouchCollection = TouchPanel.GetState();
Also I added this prop to my game state:
public bool TouchActive { get; set; }
And this is the method to check for rectangle tap. It returns true only for the first contact in tap:
private bool CheckRectangleTap(Rectangle target)
{
if (State.TouchCollection.Count == 0)
{ // if no input
return State.TouchActive = false;
}
var targetTouched = false;
foreach (var touch in State.TouchCollection)
{
if (target.Contains(touch.Position))
{
targetTouched = true;
}
}
if (targetTouched && !State.TouchActive)
{ // if target is touched and it's first contact
return State.TouchActive = true;
}
return false;
}
It doesn't seem ideal but it works for my case.

How to prevent navigation until all validation are fulfill?

I want to add validation to my form. But there is one problem I press submit button it shows the validation for a quick time, and blank form submitted to recyclerview. please give me the solution if you have.
/**
* Performs action to submit the form if all the validations are fulfilled
*/
public void submitForm() {
if (validateFields()) {
//Todo add your form submission code here
}
}
/**
* Validate all the fields present in the form according to the requirements
* Returns true if there is no validation error, false otherwise.
*/
public boolean validateFields() {
if (editTextEmail.getText().toString().isEmpty()) {
//Show toast or snackbar for validation failed
return false;
} else if (//todo another validation code)
{
//Show toast or snackbar for validation failed
return false;
}
return true;
}
Performs action to submit the form if all the validations are fulfilled
public void submitForm() {
if (validateInputFields()) {
//Todo add your form submission code here
}
}
Validate all the fields present in the form according to the requirements
Returns true if there is no validation error, false otherwise.
public boolean validateInputFields() {
if (TextUtils.isEmpty(email)) {
//Show toast or snackbar for validation failed
return false;
}
else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
//Show toast or snackbar for validation failed
return false;
}
else if (//todo another validation code)
{
//Show toast or snackbar for validation failed
return false;
}
return true;
}
You can go like this:
public void clickAction(){
if(validateFields()){
//Todo add your form submission code here
}
}
public boolean validateFields(){
if(editTextEmail.getText().toString().isEmpty()){
//Show toast validation failed
return false;
}else if(//todo another validation code){
return false;
}
return true;
}
Try the below validation method, if it's not working, please share your code here to look further more into your problem.
if (editTextName.getText().toString().trim().length() <= 0 ||
editTextAge.getText().toString().trim().length() <= 0) {
Toast.makeText(LoginActivity.this, "Fields should not be blank",
Toast.LENGTH_LONG).show();
} else {
callSubmitFormApi();
}

onKeyUp function not calling second time

I have been trying to built an app based on GHOST game.
I have written an onKeyUp function which only accepts lowercase alphabets and adds it to a string called wordfragment and then calling the function computerTurn in it. But i had seen after successfully running first time i.e. calling the computerTurn function and getting return statement from computerturn function it(onkeyup) does not works second time.
Here my code to onKeyUp function.
#Override
public boolean onKeyUp(int KeyCode, KeyEvent event) {
char ch = (char)event.getUnicodeChar();
if( !( ch >= 'a' && ch <='z' ) ) {
return super.onKeyUp(KeyCode, event);
}
wordFragment = wordFragment + ch;
label.setText(COMPUTER_TURN);
text.setText(wordFragment);
userTurn = false;
computerTurn();
return true;
}
and my code to computerTurn function is
private boolean computerTurn() {
if(wordFragment.length() >= 4 && dictionary.isWord(wordFragment)){
label.setText("Computer wins");
// challenge.setEnabled(false);
return true;
}
else {
String word = dictionary.getAnyWordStartingWith(wordFragment.toLowerCase());
if(word!=null){
Toast.makeText(GhostActivity.this, "comp word found", Toast.LENGTH_SHORT).show();
wordFragment += word.charAt(wordFragment.length());
}
else{
Toast.makeText(GhostActivity.this, "comp word not found", Toast.LENGTH_SHORT).show();
label.setText("User Wins!!");
//challenge.setEnabled(false);
// wordFragment += (char)(random.nextInt(26) + 61);
}
}
// Do computer turn stuff then make it the user's turn again
userTurn = true;
label.setText(USER_TURN);
text.setText(wordFragment);
Toast.makeText(GhostActivity.this, "return true", Toast.LENGTH_SHORT).show();
return true;
}
Android softkeyboards rarely use key events. The correct way to use an android soft keyboard is via InputConnection. Only hardware keys generally issue key events. Basically you're coding this the right way for Windows or web, but the wrong way for Android.

ionic keyboard go button to next button

Reference:Change go button to next button in android
I am developing an application with sign up page using Ionic framework.
Is there any option to replace go button with next button? I want to move cursor from one field to another using next button in the keyboard.
you can use following reference to achieve your requirement in ionic.Below code is for cordova
(function($) {
$.fn.enterAsTab = function(options) {
var settings = $.extend({
'allowSubmit': false
}, options);
this.find('input, select, textarea, button').live("keypress", {localSettings: settings}, function(event) {
if (settings.allowSubmit) {
var type = $(this).attr("type");
if (type == "submit") {
return true;
}
}
if (event.keyCode == 13) {
var inputs = $(this).parents("form").eq(0).find(":input:visible:not(disabled):not([readonly])");
var idx = inputs.index(this);
if (idx == inputs.length - 1) {
idx = -1;
} else {
inputs[idx + 1].focus(); // handles submit buttons
}
try {
inputs[idx + 1].select();
}
catch (err) {
// handle objects not offering select
}
return false;
}
});
return this;
};
})(jQuery);
For adding next button , you can refer following link:
How to add Next button in Ionic soft keyboard plugin

error in applying validation in android application

I am developing an android application in which iIhave 4 edittext and below a button. I want to apply validation on each field so that any field is not left blank. I have tried a lot of samples and applied same logic, but its not working.
I have coded for that. My java class is
http://pastebin.com/ZdeYZPxX
But problem is that when I enter first edittext and then click button, it works. Can anyone help me to come out of this problem?
See I have made this function in class
public boolean checkForEmpty(String fieldString) {
if (fieldString.equalsIgnoreCase("")
|| fieldString.equalsIgnoreCase(null))
return true;
else
return false;
}
Then
username = editUserName.getText().toString().trim();
if (checkForEmpty(username)) {
Showalert("Please enter User ID");
editUserName.requestFocus();
}
After showing a Toast telling the user that one of the EditText is empty you should add a return statement to stop the rest of the code(that starts a new Activity) being run until the user fills all EditTexts:
if (un.equals("") || pw.equals("")) {
if (un.equals("")) {
Toast.makeText(BloodPressureScreen.this, "FirstName is empty", Toast.LENGTH_SHORT).show();
return;
} else {
Toast.makeText(BloodPressureScreen.this, "LastName is empty", Toast.LENGTH_SHORT).show();
return;
}
}
if (cn.equals("") || em.equals("")) {
if (cn.equals("")) {
Toast.makeText(BloodPressureScreen.this, "Contact is empty", Toast.LENGTH_SHORT).show();
return;
} else {
Toast.makeText(BloodPressureScreen.this, "Email is empty", Toast.LENGTH_SHORT).show();
return;
}
}
public boolean checkForEmpty(String fieldStr) {
if (fieldStr.length() == 0 )
return true;
else
return false;
}

Categories

Resources