Skip to content

Instantly share code, notes, and snippets.

@AlexKGwyn
Created June 15, 2017 23:16
Show Gist options
  • Save AlexKGwyn/bcf22d8897f89ef48a5ceafdc36ced41 to your computer and use it in GitHub Desktop.
Save AlexKGwyn/bcf22d8897f89ef48a5ceafdc36ced41 to your computer and use it in GitHub Desktop.
A layout inflater that sets the layouts resource name as a view tag for logging purposes
package com.alexgwyn.layoutname;
import android.content.Context;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A layout inflater that adds the tag of the layout to the views it inflates
*/
public class LayoutNameInflater extends LayoutInflater {
public LayoutNameInflater(LayoutInflater original, Context newContext) {
super(original, newContext);
}
@Override
public LayoutInflater cloneInContext(Context newContext) {
return new LayoutNameInflater(this, newContext);
}
@Override
public View inflate(int resource, @Nullable ViewGroup root, boolean attachToRoot) {
String layoutName = getContext().getResources().getResourceName(resource);
View view = super.inflate(resource, root, attachToRoot);
setLayoutTag(view, layoutName);
return view;
}
private void setLayoutTag(View view, String layoutName) {
view.setTag(R.id.layout_name, layoutName);
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i = 0; i < group.getChildCount(); i++) {
View child = group.getChildAt(i);
setLayoutTag(child, layoutName);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment