How to make edittext editable after clicking edit button

I have a user profile screen in my android app and there I show the user information during registration they provided and an edit button to change their information.

+3


source to share


4 answers


You can turn it off in your xml

android:editable="false" 
android:inputType="none" 

      



And turn it on programmatically in onClick()

editButton

EditText mEdit = (EditText) findViewById(R.id.yourid);
mEdit.setEnabled(true);

      

+5


source


edT.setFocusable(false); //to disable it

 button.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
      edT.setFocusableInTouchMode(true); //to enable it
      }
  });

      



+2


source


android: editable is deprecated.

Use only android:focusable="false"

in xml.

And when the button is pressed -

EditText editText = findViewById(R.id.yourid);
editText.setFocusableInTouchMode(true);

      

0


source


You can use this code: it handles editing and executes both events.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private EditText edtText;
private Button btnEdit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    edtText = findViewById(R.id.edtTxt);
    btnEdit = findViewById(R.id.btnEdt);

    edtText.setEnabled(false);
    btnEdit.setText("Edit");

    btnEdit.setOnClickListener(this);
}


@Override
public void onClick(View v) {

    switch (v.getId()) {
        case R.id.btnEdt:  //Your edit button
            enableDisableEditText();
            break;
        default:
            break;
    }
}

//method for enable or disable edittext
private void enableDisableEditText() {
    if (edtText.isEnabled()) {
        edtText.setEnabled(false);
        btnEdit.setText("Edit");
    } else {
        edtText.setEnabled(true);
        btnEdit.setText("Done");
    }
}

      

}

Happy coding.

0


source







All Articles