Android: Custom layout-based view: how?

I am creating an Android app and I'm struggling a bit with custom views.

I would like to have a reusable view consisting of several standard layout elements. Let them say relativelayout with some buttons in it.

How should I continue. Should I create a custom view class that extends RelativeLayout and programmatically adds these buttons? I would have thought that a little outwit?

How to do it in Android?

+6
android custom-view
source share
1 answer

The following are some rough steps regarding one way to create a custom collection view:

  • extend RelativeLayout
  • Provide a constructor in a new class that takes a Context and an AttributeSet to call the superclass first. Do not add anything at this moment. Wait until the next step.
  • override the onFinishInflate method, where you can add your content through Java code or inflate an XML resource
  • Add event handlers, etc.
  • If necessary, create a resource file if your widget requires setting attributes.

+11
source share

All Articles