Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add views inside a custom View?

I have a class like that, and there are about 10 of them

public class DataItemPlainView extends View{

    public DataItemPlainView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }}

Now I need to put TextView, ImageView etc inside this view. And when I call it from somewhere, I want to get my customView. setting a view to a custom layout is a case too.

Thanks

like image 877
ikbal Avatar asked Jun 13 '11 14:06

ikbal


People also ask

Can you create custom views How?

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.

What are custom views?

A well-designed custom view is much like any other well-designed class. It encapsulates a specific set of functionality with an easy to use interface, it uses CPU and memory efficiently, and so on. In addition to being a well-designed class, though, a custom view should: Conform to Android standards.

Why do we require custom view?

Custom view are useful for some cases: create something non-trivial visually speaking - meaning you want to draw something yourself to the screen, for instance- graph.

What is ViewGroup in Android Studio?

ViewGroup is a collection of Views(TextView, EditText, ListView, etc..), somewhat like a container. A View object is a component of the user interface (UI) like a button or a text box, and it's also called a widget.


1 Answers

Your custom view needs to extend ViewGroup or one of the other classes that extends ViewGroup. For example, you could extend from RelativeLayout or LinearLayout if those layouts fits what your custom view needs to do.

Remember, even the layout classes are just another View. They just happen to have methods to add other views as children and have code to recursively measure and draw their children.

like image 75
Steve Prentice Avatar answered Sep 29 '22 13:09

Steve Prentice