How to use isInEditMode() to see layout with custom View in the editor
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if (!isInEditMode()) {
createTypeface(context, attrs); //whatever added functionality you are trying to add to Widget, call that inside this condition.
}
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
//Typeface I wan to set to my Custom TextView It can be any other functionality of your choice
private void createTypeface(Context context, AttributeSet attrs) {
TypedArray styledAttrs = context.obtainStyledAttributes(attrs,
R.styleable.CustomTextView);
String fontName = styledAttrs
.getString(R.styleable.CustomTextView_Typeface);
styledAttrs.recycle();
if (fontName != null) {
Typeface typeface = Typeface.createFromAsset(context.getAssets(),
"fonts/" + fontName);
setTypeface(typeface);
styledAttrs.recycle();
}
}
}
isInEditMode()
should be used inside the Custom View constructor.
Try the following code:
public class GraphView extends View implements Grapher
{
public GraphView(Context context, AttributeSet attrs) {
super(context, attrs);
if(!isInEditMode())
init(context);
}
public GraphView(Context context) {
super(context);
if(!isInEditMode()){
touchHandler = new TouchHandler(this);
init(context);
}
}