I have a graph view (custom view) in Android and a main view. Depending on the user’s preference I want to add 1-5 graph views onto my main view but am not quite sure how to. (I’m using purely Java and not xml). I was reading that I might have to use a Relative Layout or something in order to stack views.
Any advice or suggestions are welcome
in your activity you probably have something like this towards the begining of your onCreate() method:
inside your main.xml file you probably have an element that is some kind of layout. I will assume LinearLayout for now, but it works similarly with all types. You’ll need to get a reference to this layout and to do that it must have an id. So if that layout does not have something like this in it you need to add it:
Then back in your java sometime after you’ve called setContentView() you can find the reference to your layout with something like this:
Once you have a reference to your layout you can add your graph views to it with something like this:
If you want to skip the xml layout all together you are allowed to make your layout in java. To do that it would like this:
Note that you can only call setContentView() once so you’ll need to pass some kind of Layout to it if you want to add more than 1 View.
Edit:
I have never specifically tried but I would think you could call addView() from the constructor in your custom view:
do you have a custom view for your layout too?