Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the position of a view in a Linear Layout.?

Tags:

android

So I have a Linear Layout already populated with children. Is there a way to change the position at which one of the children is located?

I'm trying to swap to views between them if that's of any help.

final LinearLayout parrent = (LinearLayout)findViewById(R.id.llWidgetScreen);
    final LinearLayout Delailah = new LinearLayout(this);
    Delailah.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
    AppWidgetHostView wedgy = attachWidget(mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo));
    Delailah.addView(wedgy);
    final Button btn = new Button(this);
    btn.setLayoutParams(new android.widget.LinearLayout.LayoutParams((int)(20*scale +0.5f), android.view.ViewGroup.LayoutParams.FILL_PARENT, 0f));
    btn.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            parrent.removeView(Delailah);
            return false;
        }
    });
    btn.setBackgroundColor(mainColor);
    btn.setText(parrent.getChildCount()+1+"");
    btn.setTextColor(textColor);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(ReadyForDrag==0)
            {
                btn.setBackgroundColor(actiColor);
                ReadyForDrag++;
                DragPosition1=Integer.parseInt(btn.getText().toString());
            }
            else if(ReadyForDrag==1)
            {
                btn.setBackgroundColor(actiColor);
                ReadyForDrag=0;
                LinearLayout v1 = (LinearLayout)parrent.getChildAt(DragPosition1);
                LinearLayout v2 = (LinearLayout)parrent.getChildAt(Integer.parseInt(btn.getText().toString()));
                //move view 2 to position 1
                //move view 1 to position 2
            }               
        }
    });
    Delailah.addView(btn);
    parrent.addView(Delailah);
like image 390
Taranasus Avatar asked Sep 21 '11 18:09

Taranasus


People also ask

How do you align views in linear layout?

To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”. Let us create an Android application with LinearLayout containing two Button widgets as children. We shall center align these children using gravity attribute.

How do I change to linear layout?

Go to -> Edit File Templates option. Change the ${ROOT_TAG} to LinearLayout in both of these LayoutResourceFile. xml and LayoutResourceFile_vertical. xml and you will always get LinearLayout as parent for future.


1 Answers

You can use ViewGroup.removeView(View) and ViewGroup.addView(View child, int index, ViewGroup.LayoutParams params) for this.

like image 110
inazaruk Avatar answered Oct 05 '22 23:10

inazaruk