0

I was able to create and combine multiple views from my custom view class which extends a LinearLayout, and it worked well. I followed this Stack Overflow answer. But you have to create an XML file and inflate it to populate the custom view.

I want to do it programmatically not by using an XML layout and inflating it. Is this possible?

Jama Mohamed
  • 3,057
  • 4
  • 29
  • 43
  • https://stackoverflow.com/a/28473190/6727332 check this answer – Adarsh Dec 06 '19 at 12:07
  • 1
    Yeah, sure you can do that. You can instantiate `View`s with `new` just like any other regular Java class, and then use `addView()` to add them to your custom `ViewGroup` subclass. – Mike M. Dec 06 '19 at 20:25

2 Answers2

0

This is the answer from my personal experience.

  • You can use LayoutInflater to add multiple child view inside Parent view

Example :

  • Parent View : FrameLayout
  • Child View : MyView

LayoutInflater inflater = LayoutInflater.from(this);

View view = inflater.inflate(R.layout.MyView, FrameLayout, false);

FrameLayout.addView(view);

Community
  • 1
  • 1
Nidhi Savaliya
  • 162
  • 2
  • 9
0

to do so..

  1. make your custom view that extends View class

  2. make your custom viewgroup that extends ViewGroup class

and you can add your view in the viewgroup Using addView() method

view = https://developer.android.com/reference/android/view/View

viewgroup = https://developer.android.com/reference/android/view/ViewGroup

Jaydeep chatrola
  • 2,423
  • 11
  • 17