layout - How do you add a custom view on a view in Android? -
I have a graph view (custom view) and a main view in Android. Based on the user's preference, I want to add a 1-5 graph view to my main scene but I'm not sure how (I'm purely using Java and not xml).
Any advice or suggestion you are welcome
In your activity, Something that is about the beginning of your on-Crete () method:
setContentView (R.layout.main); Your main In the XML file you are probably the one element that has some type of layout. I agree to LinearLayout for now, but it works well with all kinds. You will need to get the context of this layout and to do this, there should be an ID in it. So if there is nothing like that in the layout, then you have to add it:
android: id = "@ + id / myMainLayout" Then your Java After some time after calling setContentView (), you can find the context of your layout in such a way:
LinearLayout myLayout = (LinearLayout) findViewById (R.id.myMainLayout) ; Once you reference your layout, you can add it to your graph ideas like this:
myLayout.addView (graph1) ; MyLayout.addView (graph2); // etc ... If you want to leave the XML layout together, you have permission to create your layout in Java so that it will be like it to:
LinearLayout myLayout = new linerlayout (your activity.); MyLayout.addView (graph1); MyLayout.addView (graph2); SetContentView (myLayout); Note that you can only call setContentView () once, if you want to add more than 1 view, then you have to pass some type of layout.
Edit:
I've never tried specifically but I think you can call addView () from the constructor in your custom view:
public custom view () addView (anotherView); } Do you also have a custom view for your layout?
Comments
Post a Comment