3

When attempting to save info to a server, I disable EditTexts (odds are the user can't change the info, but better safe than sorry).

In order to prevent the user from using the EditTexts, I can use either setEnabled(false) or setFocusable(false). In order to start using the EditTexts again, I call setEnabled(true) or setFocusable(true); setFocusableInTouchMode(true).

I'm guessing using setEnabled is more efficient because there are less method calls, but is this the case (basically, I'd like to know which method is more efficient)?

Or are there other side-effects of using one vs the other that I don't know about?

Edit - Solution

In order to prevent myself from needing to setEnable(true/false) to multiple different Views in multiple different Fragments, I implemented the following code (I took the idea from another StackOverflow answer):

public static void setViewAndChildrenEnabled(View view, boolean enabled) {
    view.setEnabled(enabled);

    if(view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) view;
        for(int i = 0; i < viewGroup.getChildCount(); i++) {
            View child = viewGroup.getChildAt(i);
            setViewAndChildrenEnabled(child, enabled);
        }
    }
}

I put this in my Utils package so any Activity/Fragment can call it. It works pretty nicely.

Community
  • 1
  • 1
Matt
  • 1,392
  • 12
  • 24

2 Answers2

1

In order to prevent the user from using the EditTexts

I recommend you to use setEnable() instead of setFocusable() since you see a difference when an EditText it's enabled or not, if you don't want to see the difference just use setFocusable(false) it will keep the same appearance but you won't be able to modify the value.

In order to start using the EditTexts again

You can do it calling setEnabled(true) (if you used setFocusable(false), you'll have to use setFocusable(true)

Let's say something: setFocusable() it's used for enable or disable views focus event.

BlackBlind
  • 772
  • 9
  • 26
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
0

You should use setEnabled to enabled and disable. setFocusable allows the user to tap, same with touch mode, which would be ignored.

Brandon Lerner
  • 297
  • 1
  • 7