Gniewomir
Gniewomir

Reputation: 109

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.

Upvotes: 4

Views: 2995

Answers (4)

Rishabh Dutt Sharma
Rishabh Dutt Sharma

Reputation: 764

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.

Upvotes: 3

HenriqueMS
HenriqueMS

Reputation: 3974

View positions inside ViewGroups are defined by the LayoutParams

How does this happen? Views pass their LayoutParams to their parent ViewGroups

    //100% programatic approach with simple LayoutParams
                LinearLayout myLinearLayout = new LinearLayout(this);

    //if the **parent** of the new linear layout is a FrameLayout 
                FrameLayout.LayoutParams layoutParams = 
new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);

                //or if you have the XML file you don't have to worry about this
                //myLinearLayout = (LinearLayout)findViewById(R.id.my_simple_linear_layout);


                //you could have a LinkedList<TextView>
                LinkedList<TextView> textViewList = new LinkedList<>();


                //assuming the order is the correct order to be displayed
                Iterator<TextView> descendingIterator = textViewList.descendingIterator();

                while(descendingIterator.hasNext())
                {
                    //just add each TextView programatically to the ViewGroup
                    TextView tView = descendingIterator.next();
                    myLinearLayout.addView(tView);
                }

Just like we defined LayoutParams for the LinearLayout we could also define LayoutParams for the TextView

IMPORTANT: when setting LayoutParams you need to be sure they fit the VIEWGROUP, that is the parent of the View being added

private TextView textViewFactory(String myText) {


            TextView tView = new TextView(getBaseContext());

            //controling the position relatively to the PARENT
            //because you are adding the textview to a LINEAR LAYOUT
            LinearLayout.LayoutParams paramsExample =
                    new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);

            tView.setLayoutParams(paramsExample);


            //configuring the insides of the textview
            //you can also do all kinds of stuff programatically
            tView.setGravity(Gravity.CENTER_HORIZONTAL);

            tView.setPadding(10, 10, 10, 10);
            tView.setTypeface(Typeface.DEFAULT_BOLD);// (null, Typeface.BOLD_ITALIC);
            tView.setTypeface(Typeface.SANS_SERIF);
            tView.setTypeface(null, Typeface.ITALIC);
            tView.setTypeface(Typeface.defaultFromStyle(R.style.AppTheme));
            tView.setId(R.id.aux_info);

            tView.setText(myText);
            //.........all kinds of stuff really

            return tView;
    }

Upvotes: 1

Max D
Max D

Reputation: 199

If you mean adding a view programmatically so that the new one is added above the previous one, instead of below it, then I suggest this:

  • Maintain an ArrayList with the items you want to turn into views
  • Put them into a ListView
  • When you want to add a new view that must appear at the top of the list, insert it as the first element of your ArrayList and recreate the ListView from it.

Upvotes: 0

Scott Kruse
Scott Kruse

Reputation: 11

Would really need more information from you to give a more accurate answer, but if you're saying what i think you are then you can just add these views to a LinearLayout with orientation set to vertical.

And assuming you're iterating through a list to dynamically add views, instead of incrementing from 0, increment down from the size of the list.

for(int i = size; i >= 0; i--){
   linearLayout.add(new TextView(Context));
}

Upvotes: 1

Related Questions