Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText setEnabled vs setFocusable

Tags:

android

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.

like image 500
Matt Avatar asked Sep 16 '26 02:09

Matt


1 Answers

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.

like image 70
Skizo-ozᴉʞS Avatar answered Sep 19 '26 14:09

Skizo-ozᴉʞS