Reputation: 8408
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;
}
}
Upvotes: 6
Views: 7571
Reputation: 2482
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);
}
}
Upvotes: 6
Reputation: 640
for new comers to this question, you could use the view hierarchy from android studio's android device monitor:
open the app from your device or an emulator, and browse to the view that you want to inspect
1- from the Studio's Menu bar open
tools -> Android -> Android device monitor
2- from Android device monitor Menu bar open
window -> open perspective -> Hierarchy view
it may load for some time.
if you want to learn more: https://developer.android.com/studio/profile/hierarchy-viewer.html
Upvotes: 0