Usman Khan
Usman Khan

Reputation: 3953

Edittext field enable and disable using button in Android

I am working on an Android application in which I want to make my edittext enable and disable by using a "EDIT" button. I am making something like if I press the button it will enable edittexts and if I again press EDIT button then it will disable EditTexts.

My code snippet is given below. In following condition on first attempt it works fine, but after clicking again it just return "click1".

editText.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clickCount == 0){
                    Toast.makeText(getApplicationContext(), "click0", Toast.LENGTH_SHORT).show();
                    fName.setEnabled(true); 
                    lName.setEnabled(true); 
                    mailText.setEnabled(true);  
                    mobileText.setEnabled(true);    
                }
                clickCount = 1;


  Toast.makeText(getApplicationContext(), "click1", Toast.LENGTH_SHORT).show();
        fName.setEnabled(false);
        lName.setEnabled(false);
        mailText.setEnabled(false);
        mobileText.setEnabled(false);
            }
        });

Upvotes: 0

Views: 3187

Answers (4)

Thientvse
Thientvse

Reputation: 1791

You can create 2 function :

public static void disableEditText(EditText editText) {
        editText.setFocusable(false);
        editText.setEnabled(false);
        editText.setCursorVisible(false);

     }

And

public static void enableEditText(EditText editText) {
    editText.setFocusable(true);
    editText.setEnabled(true);
    editText.setCursorVisible(true);
 }

It work for me, I hope it will help you!

Thanks!

Upvotes: 0

Remees M Syde
Remees M Syde

Reputation: 2609

Try this

  editText.setOnClickListener(new View.OnClickListener() {

              @Override
              public void onClick(View v) {
              if (clickCount == 0){
                  Toast.makeText(getApplicationContext(), "click0", Toast.LENGTH_SHORT).show();
                  fName.setEnabled(true); 
                  lName.setEnabled(true); 
                  mailText.setEnabled(true);  
                  mobileText.setEnabled(true);    
                  clickCount = 1;
            }else if(clickCount == 1){
                  Toast.makeText(getApplicationContext(), "click1", Toast.LENGTH_SHORT).show();
                  fName.setEnabled(false);
                  lName.setEnabled(false);
                  mailText.setEnabled(false);
                  mobileText.setEnabled(false);
                  clickCount = 0;
            }
    });
}

Upvotes: 4

M S Gadag
M S Gadag

Reputation: 2057

u can try this too

boolean clickCount =false;

editText.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
       if (!clickCount){
           Toast.makeText(getApplicationContext(), "click0", Toast.LENGTH_SHORT).show();
           fName.setEnabled(true); 
           lName.setEnabled(true); 
           mailText.setEnabled(true);  
           mobileText.setEnabled(true);    
           clickCount = true;
         }else {
            Toast.makeText(getApplicationContext(), "click1", Toast.LENGTH_SHORT).show();
            fName.setEnabled(false);
            lName.setEnabled(false);
            mailText.setEnabled(false);
            mobileText.setEnabled(false);
            clickCount = false;//this line is optional
            }
    });
}

Upvotes: 1

Blaze Tama
Blaze Tama

Reputation: 10948

Use if-else and isEnabled :

if(fName.isEnabled())
   fName.setEnabled(false);
else
   fName.setEnabled(true);
//do the same for other Views

Upvotes: 3

Related Questions