Search code examples
androidbuttonandroid-linearlayoutadtmove

moving Layouts in LinearLayout


I'm trying to change the position of a Relative Layout in a vertical Linear Layout. The Relative Layout should first be invisible/gone but when I press button1 it should be between button1 and button2. My container_input contains some seekBars and and textViews which I use to change some values.

I tried to archive this by doing this:

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"/>

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"/>

<Button
    android:id="@+id/button3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"/>

<include
    android:id="@+id/container_input_ref"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/container_input" />
</LinearLayout>

Java Code:

int selected = 0;
public void onClick(final View view)
{        
 if(view == button1)
 {
  selected = 1;
  container_input_ref.setVisibility(view.VISIBLE);
  container_input_ref.setY(button1.getBottom());
  button2.setY(container_input_ref.getBottom());
  button3.setY(button2.getBottom());
 }

 if(view == button2)
 {
  selected = 2;
  container_input_ref.setVisibility(view.VISIBLE);
  container_input_ref.setY(button2.getBottom());
  button3.setY(container_input_ref.getBottom());
 }
 etc. ...
}

But if I press button1 the Layout is not like that what i was supposed to get.

Does anyone have an idea how to move layouts/buttons OnClick?

Greetings


Solution

  • You can do something like this to change the position of your views:

    layout1.removeView(container_input_ref)
    layout1.addView(container_input_ref, index)
    

    Where index will be the new position of your container