Question

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.

Was it helpful?

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

});

OTHER TIPS

If I were you I would actually use two buttons one for edit, and one for save. Make them the same size and in the same position, when you want to switch between them make one invisible, and the other visible. Doing it that way would let you keep your onClickListeners separate which would make your code more understandable in my mind.

That being said you could technically achieve it with a single button as well. Just change the text on the button when you want to switch between them, and add an if statement into your click listener to check which "mode" your button is currently in to determine which action it should take.

I am not sure there is an easy way to do this or not. but you can sure use different behaviors of button clicks like

// When you press it for long time.

    dummyButton.setOnLongClickListener(new OnLongClickListener() { 
    @Override
    public boolean onLongClick(View v) {
        return true;  // Can do lot more stuff here I am just returning boolean
    }
});

// Normal click of button 
    dummyButton.setOnClickListener(new OnClickListener() { 
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //do lot more stuff here.
    }
});

Do it this way :

Make a public boolean variable

public boolean isClickedFirstTime = true;

make your 3 editTexts enabled false in xml and

onClick of your button

    @Override
        public void onClick(View v) {
    if (v.getId() == R.id.edit_button_id) { //whatever your id of button
    Button button = (Button) findViewById(R.id.edit_button_id);
    if(isClickedFirstTime)
                {
                edit1.setEnabled(true);
                edit2.setEnabled(true);
                edit3.setEnabled(true);
                butt.setText("Save");
                isClickedFirstTime = false;
                }
                else
                {
                    ....//Get your values from editText and update your database
                            isClickedFirstTime = true;
                }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top