-2

The first time the "edit info" button is pressed nothing happens but the subsequent times it works fine. There are many similar questions and I have red through and tried everything I could with no success.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sharedPrefs = getSharedPreferences(USER_PREFERENCES, MODE_PRIVATE);
    //call function to load previous values, no intteraction with button



        final Button myButton = findViewById(R.id.button);
        myButton.setTag(1);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final int status =(Integer) v.getTag();
                if(status == 1) {
                    myButton.setText(getString(R.string.edit_info));
                    setEditable(false);
                    v.setTag(0);
                } else {
                    myButton.setText(getString(R.string.save_info));
                    setEditable(true);
                    v.setTag(1);
                }
            }

        });
       //proposed solution from other question, see note bellow (*)    
        myButton.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP && !v.hasFocus()) {
                    // onClick() is not called when the EditText doesn't have focus,
                    // onFocusChange() is called instead, which might have a different
                    // meaning. This condition calls onClick() when click was performed
                    // but wasn't reported. Condition can be extended for v.isClickable()
                    // or v.isEnabled() if needed. Returning false means that everything
                    // else behaves as before.
                    myButton.performClick();
                }
                return false;
            }
        });
    }

(*) From this question Matěj Hrazdíra proposes adding OnTouchListner but doing this made the button never toggle back.

Here is activity_main.xml

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="@string/edit_info" />
northerner
  • 756
  • 5
  • 21
  • What are you expecting to happen? Given that the initial text on the `Button` is `edit_info`, and that's what you're setting when the tag is `1` – which is its initial value – it seems like it should look like nothing changes on the first click. Did you mean to set the tag to `0` initially? – Mike M. Aug 21 '18 at 08:05
  • @MikeM. this line should change it to save info `myButton.setText(getString(R.string.save_info));` – northerner Aug 21 '18 at 08:08
  • `myButton.setTag(1);` – On the first click, the `if` block inside `onClick()` will run, where it sets the text to `edit_info`. – Mike M. Aug 21 '18 at 08:09
  • @MikeM. it does run, just not the first time (hence this question) – northerner Aug 21 '18 at 08:10
  • 1
    If you're expecting the text to change on the first click, it won't with your current setup, because you've set the tag to `1`, and if that's what you're going by to determine if it's running, that's why it seems as though it's not. – Mike M. Aug 21 '18 at 08:14
  • @MikeM. thanks that's right. I spent a lot of time looking in other areas such as which part had focus, if a different listener should've been implemented etc. This seems like such a small, easy fix. Any tip on how it could have been caught earlier? I returned to this problem on several days. – northerner Aug 21 '18 at 08:17
  • 1
    Well, I suppose you could've put a log print or a `Toast` first thing inside `onClick()`, then you would've seen that `onClick()` was actually working, and that something was probably wrong with the logic. – Mike M. Aug 21 '18 at 08:19

1 Answers1

1

From the xml the button has already the text "@string/edit_info".
In onCreate() you set its tag to 1. So the listener will execute:

            myButton.setText(getString(R.string.edit_info));
            setEditable(false);
            v.setTag(0);

So the text does not change.
After that the subsequent clicks will change its text.
I don't know what you want exactly, but if in onCreate() you set its tag to 0, you will see change from the 1st click.