Questions tagged [custom-view]

It is possible to create custom views by:

  • Compound views - combining views with a default wiring

  • Custom views - creating your own views

    a) by extending an existing view, e.g. Button, TextView, EditText, ListView, CheckBox, RadioButton, Gallery, Spinner, AutoCompleteTextView, ImageSwitcher, and TextSwitcher.

    b) by extending the View class

Creating custom views

By extending the View class or one of its subclasses you can create your custom view.

Creating your own View subclasses gives you precise control over the appearance and function of a screen element.

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. If the view should be re-drawn you call the invalidate() method which triggers a call to the onDraw() method of this view.

If you define own views, ensure you review the ViewConfiguration class, as it contains several constants for defining views.

To draw your Views you typically use the 2D Canvas API.

Useful links:

644 questions
20
votes
4 answers

Android - Is it possible to create a custom library to use across several applications?

Is it possible to create a custom library in Android (having its own layout resources) for use across several Android applications? I created a regular *.jar file but when I tried to create/style my views dynamically most of the properties do not…
Tawani
  • 11,067
  • 20
  • 82
  • 106
19
votes
2 answers

android:how to instantiate my custom view with attributeset constructor

My custom view has dynamic custom attribute,e.g. the backgroundimage attribute,assign via current week. I wan't to use construtor CalendarView(Context context, AttributeSet attrs) to pass several attribute,and I try to instantiate the attributeset…
gaconel
  • 191
  • 1
  • 6
18
votes
2 answers

Android custom view Bitmap memory leak

I've got a custom view in which I need to draw two bitmaps, one is a background, representing the image of a map and one is a pin which will be drawn on top/left position in canvas. The both images are drawn onDraw and remain the same during the…
Alin
  • 14,809
  • 40
  • 129
  • 218
18
votes
0 answers

Calendar view Event Transition

I am working on Calendar view customization. I'm using this library. I want this kind of uiview Added in to this Calendar. Actually I am able to add event by long press (as you can see in below gif), but I want to add those two dot(button) to resize…
Premal Khetani
  • 3,175
  • 1
  • 26
  • 58
17
votes
3 answers

How to loop over viewbuilder content subviews in SwiftUI

So I’m trying to create a view that takes viewBuilder content, loops over the views of the content and add dividers between each view and the other struct BoxWithDividerView: View { let content: () -> Content init(@ViewBuilder…
Eddy
  • 274
  • 3
  • 17
16
votes
5 answers

Best practices for dealing with expensive view height calculation?

I keep running into a sizing and layout problem for custom views and I'm wondering if anyone can suggest a "best practices" approach. The problem is as follows. Imagine a custom view where the height required for the content depends on the width of…
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
15
votes
4 answers

Cannot cast to custom ViewPager class

I'm using this source from Chrisbanes Github: https://github.com/chrisbanes/PhotoView and tried making ViewPager with LinearLayout but it's getting error when casting ViewPager to HackyViewPager Here's my xml (bacaan.xml):
Victorio Pui
  • 358
  • 1
  • 3
  • 11
14
votes
5 answers

How to get android default attributes in a custom view

I have created a simple custom view that contains a RelativeLayout and an EditText:
14
votes
5 answers

How do I create a custom view class Programmatically?

I want to create a very simple customView with a few UIlabel on it, How should i do this . any tutorial or suggestion would be appreciated . I am new to this , didn't try before. I tried this with xib. @interface MyCustomView : UIView @property…
Ezimet
  • 5,058
  • 4
  • 23
  • 29
12
votes
1 answer

Custom Notification View

I would like to create a notification icon view that looks similar to the Google+ app's notification. The difference will be that I need to be able to change the color at runtime where as the Google+ icons gray or red so I'm assuming they are using…
Jeremy Edwards
  • 14,620
  • 17
  • 74
  • 99
12
votes
3 answers

Android drawing button to canvas with custom view?

How can I draw a button on top of the canvas in a custom view? (Preferably on the mid-right side) Is there something I have to call before doing the button.draw(canvas)? public class MyClass extends View { public Simulation(Context context)…
unknownone
  • 368
  • 2
  • 5
  • 13
11
votes
3 answers

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…
ikbal
  • 1,844
  • 4
  • 26
  • 46
11
votes
1 answer

Android: Resizing custom views. Parent is resized but width/height changes not passed down to child view

I am currently trying to make a game based on the old school battleship board game using android. I am kinda just messing around at the moment trying to get a feel for it and for the components that I may need to make the game. Basically what I…
cgreene
  • 111
  • 1
  • 5
10
votes
2 answers

How to create a simple custom View?

I would like to create an custom View on Android. I have tried to do it as simple as possible and created an almost empty class MyView and used it in my LinearLayout but the application fails on start with "Force Close". How can I do a simple custom…
Jonas
  • 121,568
  • 97
  • 310
  • 388
10
votes
4 answers

How do I access layout_height from within my custom view?

I have a custom view and I simply wish to access the xml layout value of layout_height. I am presently getting that information and storing it during onMeasure, but that only happens when the view is first painted. My view is an XY plot and it…
Brad Hein
  • 10,997
  • 12
  • 51
  • 74
1
2
3
42 43