Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count amount of views in layout

Is there a way for Android Studio to show how many views that are present within an XML layout? As we all know, layouts should contain <=80 views hence any more than that then this warning appears therefore it would be very helpful to be told the amount.

LayoutName.xml has more than 80 views, bad for performance

public class FragmentApple extends android.support.v4.app.Fragment {

    public FragmentApple () {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_apple,container,false);

        int num = FragmentApple.getChildCount(); Log.d("Number of views in layout: ", "" + num);

        return v;
    }
}
like image 305
wbk727 Avatar asked Aug 12 '15 13:08

wbk727


1 Answers

Have you tried this?:

layout.getChildCount();

UPDATE

after what we discussed in the comments, this is what you should do within your code:

public class FragmentApple extends android.support.v4.app.Fragment {

public FragmentApple () {
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_apple,container,false);



    return v;
}

@Override
public void onStart(){
super.onStart()
RelativeLayout (or Linear as in your xml) rl = (RelativeLayout) v.findViewbyId(R.id.your_layout_id)

int num = rl.getChildCount();
 Log.d("Number of views in layout: ", "" + num);
}

    }
like image 113
eyadMhanna Avatar answered Oct 31 '22 06:10

eyadMhanna