How do I make Button work only if Edittext is filled in?

Asked

Viewed 153 times

0

I just want to push the button after all the EditText's are filled in

package com.example.marcosnogueira.appnote;


import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.content.*;

public class Principal extends AppCompatActivity {

    private EditText nome;
    private EditText nomlojcli;
    private EditText endereco;
    private EditText cpf;
    private EditText cnpj;
    private EditText tel;
    private Button cadastrar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);

        nome = (EditText) findViewById(R.id.nome);
        nomlojcli = (EditText) findViewById(R.id.nomlojcli);
        endereco = (EditText) findViewById(R.id.endereco);
        cpf = (EditText) findViewById(R.id.cpf)
        cnpj = (EditText) findViewById(R.id.cnpj);
        tel = (EditText) findViewById(R.id.tel);
        cadastrar = (Button) findViewById(R.id.button);

        cadastrar.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder mensagem = new AlertDialog.Builder(Principal.this);
                mensagem.setMessage("Cadastrado com Sucesso");
                mensagem.setNeutralButton("Ok",null);
                mensagem.show();


            }
        });
    }
}
  • Your question is the algorithm? Do you have any idea? Have you tried anything?

  • "just press" - refers to being inactive (Enabled to false) or simply do nothing when clicked ?

1 answer

3


Thinking here I managed to find two ways to do this.

  1. The first (which I think best by explaining the "error", but not exactly what you asked for) is to leave the clickable button and click to make a condição checking whether all the EditText.length (size) are different from zero, if yes, shows the AlertDialog, if not, shows a Toast saying that information is lacking.

    eTNome = (EditText)findViewById(R.id.eTNome);
    eTCidade = (EditText)findViewById(R.id.eTCidade);
    btnSave = (Button)findViewById(R.id.btnSave);
    
    btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(eTCidade.length() != 0 && eTNome.length() != 0){
                //Mensagem avisando "cadastro feito com sucesso"!
            } else {
                //Avisando pra colocar mais informações
            }
        }
    });
    
  2. The other solution (which is what you want) would be every change in one EditText check that they are all filled and if they are leaving the clickable button, we can use the addTextChangedListener, with it you can do actions before, during and after the text is changed, overwriting the methods beforeTextChanged, onTextChanged and afterTextChanged, I only used the after, but I think in any one it serves, the code below:

    public class MainActivity extends AppCompatActivity {
        Button btnSave;
        EditText eTNome;
        EditText eTCidade;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            eTNome = (EditText)findViewById(R.id.eTNome);
            eTCidade = (EditText)findViewById(R.id.eTCidade);
            btnSave = (Button)findViewById(R.id.btnSave);
            btnSave.setEnabled(false);
            btnSave.setClickable(false);
    
            eTNome.addTextChangedListener(MudarTexto);
            eTCidade.addTextChangedListener(MudarTexto);
        }
        private TextWatcher MudarTexto = new TextWatcher(){
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                AoMudarTexto();
            }
        };
        private void AoMudarTexto(){
            if(eTCidade.length() != 0 && eTNome.length() != 0) {
                btnSave.setEnabled(true);
                btnSave.setClickable(true);
            } else {
                btnSave.setEnabled(false);
                btnSave.setClickable(false);
            }
        }
    }
    
  • 1

    Perhaps it would be preferable to use a single Textwatcher for all Edittext’s. So avoid code repetition.

  • How could I do that? It’s just that I actually program in C# with Xamarin, so I don’t know so much about Java, I’ll do a search and if I find something I edit the code to make it better

  • Ah, I found something here, I’ll update the code to make it better, thanks for the tip there!

  • Really, the solution is better 1. Simpler and better usability. Solution 2 has to be combined with some kind of indicator of what the problem is. It’s not cool to keep trying to hit a gray button and not getting it because you don’t know what’s blocking it.

  • @Pabloalmeida another thing you can do is, when it is inaccessible, change the text to "Fill in the information" or something like that, but I still prefer the first example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.