bebeTech
bebeTech

Reputation: 153

Using findViewById more effeciently

I am currently using the following code and wondering if there is a more effecient way of doing this via a function?

showDisplay = (LinearLayout)findViewById(R.id.display1);
if (isA)
{ 
{ showDisplay.setVisibility(0);}
else
{ showDisplay.setVisibility(8); }


showDisplay = (LinearLayout)findViewById(R.id.display2);
if (isB)
{ showDisplay.setVisibility(0);}
else
{ showDisplay.setVisibility(8); }

showDisplay = (LinearLayout)findViewById(R.id.display3);
if (isC)
{ showDisplay.setVisibility(0);}
else
{ showDisplay.setVisibility(8); }

Upvotes: 3

Views: 2987

Answers (3)

Tseng
Tseng

Reputation: 64160

I'd do it like this

// Do this in your onCreate method and store the references as class member variables
showDisplay1 = (LinearLayout)findViewById(R.id.display1);
showDisplay2 = (LinearLayout)findViewById(R.id.display2);
showDisplay3 = (LinearLayout)findViewById(R.id.display3);

// Do this somehwere in your code
showDisplay1.setVisibility(isA?View.VISIBLE:View.GONE);
showDisplay2.setVisibility(isB?View.VISIBLE:View.GONE);
showDisplay3.setVisibility(isC?View.VISIBLE:View.GONE);

For efficiency it's important to store the references as member variables, as calling findViewById is a quite expensive action (compared to accessing a member variable), because you'll only ever need to call it once when the application is created (this also accounts for orientation changes, as the Activity gets destroyed and is recreated again).

And it's quite clean and tidy. Inline if-expressions are very usefull for this kind of functions, where you only have either that or this.

myFunction((expression)?if_value:else_value);

is short of

int value = 0;
if(expression) {
    value = if_value;
} else {
    value = else_value;
}
myFunction(value);

you can basically ignore the ( ) if the variable is already a boolean, if not you have to put the brackets. So this will work fine too

myFunction((someVariable>3)?View.VISIBLE:View.GONE);

Edit2:

int value = 0;
if(somveVariable > 3) {
    value = View.VISIBLE;
} else {
    value = View.GONE;
}
myFunction(value);

So instead of making a variable to hold a value you want to pass to a function, you can do it all inline.

Edit: Oh and btw: Please, never use 0 or 8 for View.setVisibility(...) method. This is a very bad practice and will break your application if the values ever change in future. View.VISIBLE is already a public static final int, which means the compiler will replace all occurrences of View.VISIBLE with a 0. In byte code it's same as using 0, no performance impact, but if the value is ever to be changed, all the change will automatically occur on compile time with the new SDK and no manual modification is needed, while with your attempt you would have to replace every single 0 and 8 with the new values!

Upvotes: 1

Reuben Scratton
Reuben Scratton

Reputation: 38707

I usually have a bunch of helpers for setting common properties like visibility, text, etc. It makes the code look prettier.

setChildVisibility(R.id.display1, isA);
setChildVisibility(R.id.display2, isB);
setChildVisibility(R.id.display3, isC);

With the helper being :

protected void setChildVisibility(int id, boolean visible) {
    View view = findViewById(id);
    if (view != null) {
        view.setVisibility(visible?View.VISIBLE:View.GONE);
    }
}

Upvotes: 2

kgiannakakis
kgiannakakis

Reputation: 104178

I would do something like this:

public void showDisplay(int displayId, boolean show) {
    if (show) {
      ((LinearLayout)findViewById(displayId)).setVisibility(0);
    }
    else {
      ((LinearLayout)findViewById(displayId)).setVisibility(8);
    }
}

showDisplay(R.id.display1, isA);
showDisplay(R.id.display2, isB);
showDisplay(R.id.display3, isC);

The code becomes more readable, not more efficient.

Upvotes: 1

Related Questions