Search code examples
androidbuttoneditbox

Android single button multiple functions


I am beginner to Android development. I have 3 edit boxes and one "Edit" button. When I launch the activity all the edit boxes should be disabled. When I click on the Edit button all the 3 edit boxes should get enabled and button text should change to "Save". After updating the data in the edit boxes, when I click on the "Save" button, I should be able to send the updated data to the backend.

My problem is how can I make use of a single button for two function "Edit" and "Save".

Please help me.


Solution

  • You can do it this way:

    button = (Button) findViewById(R.id.button1);
    
    button.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View arg0) {
            String ButtonText = button.getText().toString();
            if(ButtonText.equals("Save"){
                //code for save
                button.setText("Edit");
            }
            else{
                //code for edit
                button.setText("Save");
            }
        }
    
    });