Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create custom view and display in Layout Preview [duplicate]

Tags:

I've been creating custom views for a while now, but never got to make them in a way that the Android Studio preview displays them correctly.

I initialize the custom attributes using:

if (!isInEditMode()) { ... } 

But this results in an invisible component on preview. Any ideas on how to achieve this?

like image 639
Maximiliano Ambrosini Avatar asked Feb 17 '17 14:02

Maximiliano Ambrosini


People also ask

When creating a custom view in Android which method is responsible for displaying that view on the screen?

Creating custom views. By extending the View class or one of its subclasses you can create your custom view. For drawing view use the onDraw() method. In this method you receive a Canvas object which allows you to perform drawing operations on it, e.g. draw lines, circle, text or bitmaps.

How do you inflate a custom view?

What you want to do is create a Compound Control. You'll create a subclass of RelativeLayout, add all our your components in code (TextView, etc), and in your constructor you can read the attributes passed in from the XML. You can then pass that attribute to your title TextView. Show activity on this post.

What are the types of custom views?

In Android, there are actually two other Views readily available to do this: Spinner and AutoCompleteTextView , but regardless, the concept of a Combo Box makes an easy-to-understand example. To create a compound component: The usual starting point is a Layout of some kind, so create a class that extends a Layout.


1 Answers

You need to rebuild project. After that custom views in preview will work

like image 57
mohax Avatar answered Sep 22 '22 12:09

mohax