Do not change focus if edit text is empty - android

I am working with validations in edit text. I have 5 edit text in my form and what I want is if the any of the edit text is null then it should not break its focus. I have tried verified answer from this link.
but in my case of 5 edit text its not working properly.
Please Help, Thanks.

Try this
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String val1=editText1.getText().toString().trim();
String val2=editText2.getText().toString().trim();
String val3=editText3.getText().toString().trim();
String val4=editText4.getText().toString().trim();
String val5=editText5.getText().toString().trim();
if(TextUtils.isEmpty(val5)){
editText5.requestFocus();
editText1.setFocusable(false);
editText2.setFocusable(false);
editText3.setFocusable(false);
editText4.setFocusable(false);
}else {
editText4.setFocusable(true);
}
if(TextUtils.isEmpty(val4)){
editText4.requestFocus();
editText1.setFocusable(false);
editText2.setFocusable(false);
editText3.setFocusable(false);
}else {
editText3.setFocusable(true);
editText1.setFocusable(false);
editText2.setFocusable(false);
editText4.setFocusable(false);
}
if(TextUtils.isEmpty(val3)){
editText3.requestFocus();
editText1.setFocusable(false);
editText3.setFocusable(false);
editText4.setFocusable(false);
}else {
editText2.setFocusable(true);
}
if(TextUtils.isEmpty(val2)){
editText2.requestFocus();
}else {
editText1.setFocusable(true);
}
if(TextUtils.isEmpty(val1)){
editText1.requestFocus();
}
if(!TextUtils.isEmpty(val1) &&
!TextUtils.isEmpty(val2) &&
!TextUtils.isEmpty(val3) &&
!TextUtils.isEmpty(val4) &&
!TextUtils.isEmpty(val5) ){
Toast.makeText(MainActivity.this, "PASS", Toast.LENGTH_SHORT).show();
}
}
});

First check EditText empty,null or matches to some specific text
based on your requirement.
Check 5 EditTexts using if & else if statements. if a condition is true, change things to EditText which
you want to do.
In else part, set things you want to set if, if or else if conditions false.
According to my understanding in your question, I wrote a code. try it.
final EditText et1 = (EditText) findViewById(R.id.et1); //get referance to all 5 EditTexts. I showed for 2 EditTexts
final EditText et2 = (EditText) findViewById(R.id.et2);
if (et1.getText().toString().length() < 1) { //check EditText empty or not.
//do what you want to edit/change in EditText. like below.
et1.setFocusableInTouchMode(true);
et1.requestFocus();
} else if (et2.getText().toString().length() < 1) {
//Check for second EditText empty or not. Do this for all 5 edit texts.
et2.setFocusableInTouchMode(true);
et2.requestFocus();
}
else{
//if All EditTexts not empty. do what you want to edit/change in edittext.
et1.setFocusableInTouchMode(false);
et2.setFocusableInTouchMode(false);
}

Related

I want to perform many tasks in one Edittext

I want to perform many task in one Edittext. Like when I edit the first number in edittext it store in string value and edittext may be null after that when I enter 2nd number in same edittext 2nd number may also save in another string. Then after some action perform answer show in Textview.
Please help me out...
private void save_on_cick_listen(){
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(save == 0){
// save first string
editText.getText().clear(); //or you can use editText.setText("");
save+;
} else if (save == 1){
// save second string
editText.getText().clear(); //or you can use editText.setText("");
save+;
}
}
});
}

How to validate several TextView field with infromation before going into the next activity

What I am trying to do is check if all the TextView and Spinner(Drop down) have been filled and selected before going into the next activity. If any or some of them are empty I want to highlight the respective field. Any suggestions or help coding would be great.
I have something like this:
value_1=tv1.getSelectedItem().toString();
value_2=tv2.getText().toString();
value_3=tv3.getText().toString();
value_4=tv4.getText().toString();
value_5=tv5.getText().toString();
Intent intent= new Intent(FormActivity.this,MapsActivity.class);
intent.putExtra("key1",value_1);
intent.putExtra("key2",value_2);
intent.putExtra("key3",value_3);
intent.putExtra("key4",value_4);
intent.putExtra("key5",value_5);
startActivity(intent);
define a view
private View focusView = null;
OnClick of button call following method
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (validate()) {
//do your work
} else {
focusView.requestFocus();
}
}
});
//code to validating each editext
private boolean validate() {
if (TextUtils.isEmpty(getEmail)) {
emailText.setError(getString(R.string.error_field_required));
focusView = emailText;
return false;
} else if (!getEmail.matches(EMAIL_REGEX)) {
emailText.setError("Invalid Email Address");
focusView = emailText;
return false;
} else if (TextUtils.isEmpty(getName)) {
nameText.setError(getString(R.string.error_field_required));
focusView = nameText;
return false;
}
else{
return true;
}
}
this will focus you view if particular edittext is empty and invalid
For your spinner problem look at the following tutorial.
Spinnerdrop
That's a good tutorial. You need a listener to listen to a onclick method. Follow that and I think you will get there. If you need more help, feel free to ask
The following is for if you need to check whether a string is zero or not.
The method length() wil return a int value of the length of the given string. In your case that value musn't become 0 (zero).
if((value_1.length() != 0 ) && (value_2.length() != 0) && (value_3.length() != 0) && (value_4.length() !=0) && (value_5.length() !=0))
{
//Your code
} else {
//handle error
}
PS: Don't use android-studio tag, that's only for questions about the ide. Stick to android tag.

Checking empty of EditText and send Toast - / avoid saving empty data to SQLite

Sorry taking your time, am asked a question in a very wrong way.
So what I doing now, a small notepad program where the title and content saved of the note to the SQLite database.
This part working as should, but I don't have any input check and the app saving the note with empty title and content.
there is my current code for this :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dba = new DatabaseHandler(MainActivity.this);
title = (EditText) findViewById(R.id.titleEditText);
content = (EditText) findViewById(R.id.wishEditText);
saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
saveToDB();
}
});
}
private void saveToDB() {
MyNote wish = new MyNote();
wish.setTitle(title.getText().toString().trim());
wish.setContent(content.getText().toString().trim());
dba.addWishes(wish);
dba.close();
//clear
title.setText("");
content.setText("");
Intent i = new Intent(MainActivity.this, DisplayNotesActivity.class);
startActivity(i);
How can I implement a basic input checking and avoid the saving of empty notes?
my first idea was to check the emptiness of the input and drop a toast message, tried several solutions from not, but not worked me.
many thanks
C
You have to be sure you're using the same EditText variable which in this case I think is title.
EditText title = (EditText) findViewById(R.id.titleEditText);
if(title.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Input Text Is Empty.. Please Enter Some Text", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, title.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
});
U need to specyfi your question.
But from what i undestood u don't know how check editText is empty and don't know why can't write into text box.
First easy method is just check length of string if it bigger than 0 that's meen it's not empty
EditText title = (EditText) findViewById(R.id.IDEDITTEXT);
if(String.valueOf(title.getText()).length()>0){
//do something
}else{
//do something
}
And about second (i thing question) is check your xml file (layout) does your editText is enabled and not is textViev.
Btw your code is hard to read like your question:)
EditText title = (EditText) findViewById(R.id.titleEditText);
if(title.getText().toString().length()<=0) {
Toast.makeText(MainActivity.this, "Input Text Is Empty.. Please Enter Some Text", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, title.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
});
try this code...

if a buttons text is empty, then

In this code I want to make a buttons visibility gone, if the text of the button is "":
if (button TEXT IS "")
{
button.setVisibility(View.GONE);
}
else
{
button.setVisibility(View.VISIBLE);
}
How can I form the if-Statement to get a result?
Thank you!
The answer is in the question, try with the Empty() method to check if the length of the characters in your String is 0 or greater :
if(text.isEmpty()){
//String is empty
}else{
//String includes characters
}
Button b = (Button)findViewByID("your button id");
String buttonText = b.getText().toString();
if (buttonText.equals("Your Text"))
b.setVisibility(View.GONE);
else
b.setVisibility(View.VISIBLE);
If you want to check if empty you may use
if(buttonText.isEmpty())
b.setVisibility(View.GONE);
else
b.setVisibility(View.VISIBLE)
if(button.getText() == null)
{
button.setVisibility(View.GONE);
}
else
{
button.setVisibility(View.VISIBLE);
}

making EditText not editable using if statement

I have to two EditTexts (editText1 and editText2 ) and I want the user to enter value inside editText1 first and then inside editText2, in another word I want to make editText2 not editable until the user enter something in editText1.
I tried but it didn't work:
EditText editText1 ;
EditText editText2 ;
String Str1 = editText1 .getText().toString();
if(Str1.matches(""))
{
editText2 .setKeyListener(null);
}
That might meet your requirements.
if(Str1.matches("")) {
editText2.setEnabled(false);
}else{
editText2.setEnabled(true); }
You should use setEnabled() method
if(Str1 == null || Str1.length() == 0)
{
editText2.setEnabled(false);
} else
{
editText2.setEnabled(true);
}
You should put that code into OnKeyListener for the first EditText. This will then enable and disable the second one based on the length of the text in the first one.
You can initially do editText2.setEnabled(false) and then when the user types something in the first editText, you can do editText.setEnabled(true).
You can disable any view element functionality using .setEnabled(false) method.
So, use
EditText.setEnabled(false);

Categories

Resources