Check if EditText Number Decimal is Empty - android

I tried everything.
if (editText == null)...
if (etNumero1.getText().toString().isEmpty())...
if (etNumero1.getText().toString().equals("")...
if (etNumero1.getText().toString().trim().length() >= 0))
Nothing works. Even my teatcher cant tell whats wrong.
Sorry for bad english im Brazilian.

Check, If the value is not an Empty
if (!etNumero1.getText().toString().isEmpty())
{
if(etNumero1.getText().toString().contains("."))
{
//Do your functionality here
}
}
else
{
//
}

Try
if (Integer.parseInt(etNumero1.getText().toString()) > 0)
If you specify your question, I could answer in better condition.

Use TextUtils.isEmpty("") check for null if not convert to formart to ###.##

Make sure that you are not making mistake on editText etNumero1??
I know editText is Edit Text but what is etNumero1?? please replace etNumero1 to editText, it should work
if (editText.getText().toString().isEmpty())...
Example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = findViewById(R.id.editText);
Button button = findViewById( R.id.button );
button.setOnClickListener( new View.OnClickListener(){
#Override
public void onClick (View v) {
if(editText.getText().toString().isEmpty()){
System.out.println("empty string");
}else{
System.out.println("" + editText.getText().toString());
}
}
});
}

if(String.valueOf(editText.getText() != null){
Double double = Double.parseDouble(editText.getText().toString);
}

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+;
}
}
});
}

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...

Do not change focus if edit text is empty

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);
}

edittext visibility change at runtime depending upon it's visibility

Hello all i want to do
1]if edittext is visible then invisible and if invisible then visible to it for that i have done this
btn_search.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// fragment=new BBQ();
// Intent i=new Intent(getApplicationContext(),
// Search_Activity.class);
// startActivity(i);
ed= (EditText) findViewById(R.id.editText1);
if(ed.getVisibility()==arg0.INVISIBLE)
{
ed.setVisibility(arg0.VISIBLE);
}
if(ed.getVisibility()==arg0.VISIBLE)
{
ed.setVisibility(arg0.INVISIBLE);
}
}
for me if it if invisible then it make visible but on second click it not invisible what is wrong i am doing?
I'll say that you should else-if condition:
if(ed.getVisibility()==View.INVISIBLE) {
ed.setVisibility(View.VISIBLE);
} else if(ed.getVisibility()==View.VISIBLE) {
ed.setVisibility(View.INVISIBLE);
}
Or with the ternary operator:
ed.setVisibility (ed.getVisibility() != View.VISIBLE ? View.VISIBLE : View.INVISIBLE);
May be when the code enters the first if, later you modify the visibility and set again invisible, so it enters again in the second if, try this:
if(ed.getVisibility()==arg0.INVISIBLE)
{
ed.setVisibility(arg0.VISIBLE);
}
else if(ed.getVisibility()==arg0.VISIBLE)
{
ed.setVisibility(arg0.INVISIBLE)
}
Try this:
Boolean isVisible=true;
ed= (EditText) findViewById(R.id.editText1);
btn_search.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if(isVisible){
ed.setVisibility(arg0.INVISIBLE);
}else{
ed.setVisibility(arg0.VISIBLE);
}
isVisible=!isVisible;
}
Try this..
EditText having tag attribute initility set the tag as visible then while giving invisible setTag("invisible"); or setTag("visible");
<EditText
android:id="#+id/url_edittext"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp"
android:tag="visible" />
Code
if(ed.getTag().equals("visible"))
{
ed.setVisibility(View.INVISIBLE);
ed.setTag("invisible");
}
else if(ed.getTag().equals("invisible"))
{
ed.setVisibility(View.VISIBLE);
ed.setTag("visible");
}
Edit your code
if(ed.getVisibility()==arg0.INVISIBLE)
{
ed.setVisibility(View.VISIBLE);
} else if(ed.getVisibility()==arg0.VISIBLE)
{
ed.setVisibility(View.GONE);
}
Hope this will solve your issue

Not getting an output from my inputs

http://pastebin.com/YmeB2D1N
Essentially, my program has two EditTexts, a Button, and a TextView. You input a number/string into the EditText, and an output is supposed to be shown in the TextView. However, all i get is a 'There is no Department'. How do i fix this?
I don't see anywhere that you are setting the values of department or name variables.
you can do:
EditText departmentEt = (EditText)findViewById(r.id.department_edit_text));
EditText nameEt = (EditText)findViewById(r.id.name_edit_text));
search.setOnClickListener(new OnClickListener) {
public void onClick(View v) {
if(deparmentEt.length() > 0) {
department = Integer.parseInt(departmentEt.getText().toString());
}
if(nameEt.length() > 0) {
name = nameEt.getText().toString();
}
//The rest of your code
}
}
use onTextChangedListener for EditText and
in that method write get value of EditText and set it to TextView.
I hope it works.
Code
textView= (TextView)findViewById(R.id.text);
textMessage = (EditText)findViewById(R.id.textMessage);
textMessage.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
textView.setText(textMessage.getText().toString());
}
});

Categories

Resources