Android single button multiple functions

user2740599 picture user2740599 · Oct 17, 2013 · Viewed 7.3k times · Source

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.

Answer

meda picture meda · Oct 17, 2013

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

});