Use findViewById more efficiently… here is a solution to the problem.
Use findViewById more efficiently
I’m currently using the following code wondering if there’s a more efficient way to do 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); }
Solution
I usually have a bunch of assistants to set general 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);
The helper is:
protected void setChildVisibility(int id, boolean visible) {
View view = findViewById(id);
if (view != null) {
view.setVisibility(visible? View.VISIBLE:View.GONE);
}
}