2

I created a custom Button, TextView, and ImageView. None of these appear properly in the Graphical Layout of any XML. Instead of showing a button or text with a custom font, it instead shows a huge grey box with the name of the custom class I'm calling. How do I get these to show in the preview?

public class FontTextView extends TextView {

public static Typeface FONT_NAME;

public FontTextView(Context context) {
    super(context);
    if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "font.ttf");
    this.setTypeface(FONT_NAME);
}
public FontTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "font.ttf");
    this.setTypeface(FONT_NAME);
}
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "font.ttf");
    this.setTypeface(FONT_NAME);
}

and

<com.example.gesturetest.FontTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text placeholder" />
Zeek Aran
  • 523
  • 4
  • 18
  • http://stackoverflow.com/questions/15423149/how-to-use-isineditmode-to-see-layout-with-custom-view-in-the-editor This question helped me. I just needed to wrap my constructor/init code in: if(!isInEditMode()) – Zeek Aran Apr 23 '14 at 16:57

1 Answers1

6

You can do this in your Custom View:

if(!isInEditMode()){
   // Your custom code that is not letting the Visual Editor draw properly
   // i.e. thread spawning or other things in the constructor
}

Reference

do this in the constructor of your customView

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. 
    }

}
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
  • Helpful, However, I needed to put it inside my `onLayout()` method for me to get the `LayoutParams` and modify it. – Sufian Jan 31 '16 at 18:53