How to use isInEditMode() to see layout with custom View in the editor How to use isInEditMode() to see layout with custom View in the editor android android

How to use isInEditMode() to see layout with custom View in the editor


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);              }            }


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 choiceprivate 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();    }}}


The following classes could not be instantiated:- com.google.android.gms.ads.AdView (Open Class, Show Exception, Clear Cache)

Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE. If this is an unexpected error you can also try to build the project, then manually refresh the layout.

Exception Details java.lang.NoClassDefFoundError:     com/google/android/gms/ads/R$styleable   at     com.google.android.gms.internal.ads.zzuq.<init>(com.google.android.gms:play-services-ads-lite@@18.3.0:45)   at com.google.android.gms.internal.ads.zzxl.<init>(com.google.android.gms:play-services-ads-lite@@18.3.0:30)   at com.google.android.gms.internal.ads.zzxl.<init>(com.google.android.gms:play-services-ads-lite@@18.3.0:52)   at com.google.android.gms.internal.ads.zzxl.

<init>(com.google.android.gms:play-services-ads-lite@@18.3.0:17)   at com.google.android.gms.ads.BaseAdView.<init>(com.google.android.gms:play-services-ads-lite@@18.3.0:5)   at com.google.android.gms.ads.AdView.<init>(com.google.android.gms:play-services-ads-lite@@18.3.0:4)   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)   atandroid.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)   at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:863)   at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)   at android.view.LayoutInflater.rInflate(LayoutInflater.java:837)   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)   at android.view.LayoutInflater.inflate(LayoutInflater.java:515)   at android.view.LayoutInflater.inflate(LayoutInflater.java:394) Copy stack to clipboard