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:

625 questions
120
votes
5 answers

How to get an enum which is created in attrs.xml in code

I created a custom View (find it here) with an declare-styleable attribute of type enum. In xml I can now choose one of the enum entries for my custom attribute. Now I want to create an method to set this value programmatically, but I can not access…
Informatic0re
  • 6,055
  • 5
  • 37
  • 54
73
votes
4 answers

Android ImageView size not scaling with source image

I have a few ImageViews inside a LinearLayout. I need to scale down the ImageViews so that they maintain their aspect ratios whilst fitting inside the LinearLayout vertically. Horizontally, I just need them to be adjacent to each other. I've made…
Rok
  • 2,498
  • 4
  • 24
  • 27
69
votes
7 answers

android data binding with a custom view

The Android data binding guide discusses binding values within an activity or fragment, but is there a way to perform data binding with a custom view? I would like to do something like:
Dave
  • 1,405
  • 2
  • 12
  • 9
55
votes
5 answers

How to create custom view programmatically in swift having controls text field, button etc

I am trying to access the MyCustomView from another class using the following code in ViewController.swift .. var view = MyCustomView(frame: CGRectZero) .. in the viewDidLoad method. The problem is the view does not get initialized in the…
Anish Parajuli 웃
  • 18,431
  • 9
  • 54
  • 69
44
votes
6 answers

Can a custom View know that onPause has been called?

I have a custom View that runs a Thread operation which sits around making calls to the interwebs periodically. I would like to know if there's a way for me to not have to kill that thread from the parent Activity (onPause) so that the Thread isn't…
Yevgeny Simkin
  • 26,055
  • 37
  • 127
  • 228
40
votes
4 answers

Camera with Custom View

My Application uses camera, I would like to add overlay over the camera preview. For example, I want to use a picture frame when I use Camera, also I would like to add a custom bar for camera operations. Kindly help me to do the same.
cyclingIsBetter
  • 16,737
  • 47
  • 149
  • 236
34
votes
3 answers

Android Custom View Constructor

I'm learning about using Custom Views from the following: http://developer.android.com/guide/topics/ui/custom-components.html#modifying The description says: Class Initialization As always, the super is called first. Furthermore, this is not a…
Mitch
  • 1,671
  • 3
  • 25
  • 38
34
votes
9 answers

Multiple choice list with custom view?

I've seen example com.example.android.apis.view.List11 from ApiDemos. In that example, each row takes the view android.R.simple_list_item_multiple_choice. Each such view has a TextView and a CheckBox. Now I want each view to have 2 TextViews and 1…
Phil
  • 5,370
  • 5
  • 32
  • 55
33
votes
1 answer

How can I get the canvas size of a custom view outside of the onDraw method?

I need to be able to access the size of the view's canvas to perform some calculations. For some reason, the size of the view passed to onSizeChanged is different than the size of the canvas passed to onDraw. My current workaround uses a boolean…
dfetter88
  • 5,291
  • 12
  • 41
  • 54
27
votes
4 answers

Slow load time for custom UIView with UITextView property in Swift

I originally asked this question. I had assumed that the reason for the slow load time of my custom view was because of layering multiple views on top of each other or perhaps because of some recursion problem. However, after cutting out more and…
Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198
25
votes
2 answers

How do I get a view in Interface Builder to load a custom view in another nib?

I am making a custom widget that I would like to use in multiple nibs. So I make a new view nib Screen3, add some buttons, and now want my UIAwesomeSauce widget. If I just add a view and then change the Class Identity, it doesn't get the…
CBGraham
  • 1,368
  • 1
  • 13
  • 14
24
votes
3 answers

How to pass view reference to android custom view?

I did as follows 1) Creating a styleable 2) defining custom view layout
anonim
  • 2,302
  • 5
  • 26
  • 38
23
votes
6 answers

Custom Checkable View which responds to Selector

I have a group of FrameLayout which I want to be checkable/selectable, That is, after a click I would like the FrameLayout to display as checked - when pressed again I would like it to become unchecked. What's more, I want this visual que to be…
Graeme
  • 24,857
  • 23
  • 121
  • 182
21
votes
2 answers

Custom View Extending Relative Layout

package com.binod.customviewtest; import android.content.Context; import android.view.LayoutInflater; import android.widget.RelativeLayout; public class CustomView extends RelativeLayout{ public CustomView(Context context) { …
Binod Singh
  • 654
  • 3
  • 11
  • 20
20
votes
2 answers

Views inside a custom ViewGroup not rendering after a size change

I'm running into a problem that has me stumped and I was hoping someone could give me some pointers. I'm working on an application that uses a custom ViewGroup (actually a FrameLayout that contains a RelativeLayout) to render an event calendar. The…
Michael Sims
  • 898
  • 6
  • 9
1
2 3
41 42