Gniewomir Gniewomir - 5 months ago 28
Android Question

How to set position of view while using addView

I'm adding multiple views by code into layout. I need each new view to be above previous one(top of the parent layout).

EDIT: To be more accurate I'll describe what the app module should does. User start with clean screen and one button at the bottom of the screen. The button adds a view at the top of the screen. Next clicks should add next views above previous ones to make the newest view be on the top of a container. The app saves state and on restart user see views in the same order.


Call the following method from Button's onClick Event.

private final int LAYOUT_TOP_INDEX = 0;

private void addViewOnTop(View view){
    if(layout != null && view !=null)
        layout.addView(view, LAYOUT_TOP_INDEX);

where 'layout' is your Layout (e.g., LinearLayout) to which the View is to be added.