2

I am totally new to stackoverflow, and only a couple of weeks with android mobile development.

A big thanks for stackoverflow, which is where I land on 90% of my google searches that begin with "Android eclipse".

Now to the chase.

I have a linearLayout with say a button and two textView elements.

Is it possible to dynamically:

  1. Change their order?
  2. Move some of them to another linearLayout or relativeLayout?
Jean-Paul Bardou
  • 107
  • 2
  • 11
  • Possible duplicate of [In Android, how can I set the order of children in a FrameLayout as they're added?](http://stackoverflow.com/questions/21390615/in-android-how-can-i-set-the-order-of-children-in-a-framelayout-as-theyre-adde) – HopefullyHelpful Sep 26 '16 at 09:16

2 Answers2

0

By dynamically you mean in java code?

If yes then those view should be added dynamically as well. and then to change their order you could remove them from the Layout and add them again in the order you would like. If you mean inside of you XML file, then all you need to do is to change their order in the XML file.

To add a view to a layout you should:

1. find the layout by id:

LinearLayout llviews = (LinearLayout) findViewById(R.id.your_linear_layout_id);

2. Then, you can add a view to this layout:

TextView tvText = new TextView();
llviews.addView(tvText); 

3. and remove a view:

llviews.removeView(tvText);
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
-1
  1. Yes it is possible to change the order of views dynamically, even though the Views are added in XML.
  2. Yes it's possible to move sub-views around from one layout to another layout.

Check out this example code:

public class MainActivity extends Activity {
    private Button button1, button2, button3;
    private CheckBox checkBox1;
    private LinearLayout ll1, ll2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ll1 = (LinearLayout) findViewById(R.id.ll1);
        ll2 = (LinearLayout) findViewById(R.id.ll2);

        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // Move views around.
                float button2x = button2.getX();
                float button2y = button2.getY();

                float checkbox1x = checkBox1.getX();
                float checkbox1y = checkBox1.getY();

                button2.setX(checkbox1x);
                button2.setY(checkbox1y);

                checkBox1.setX(button2x);
                checkBox1.setY(button2y);
            }
        });

        button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Move a view from one layout to another.
                ll1.removeView(button2);
                ll2.addView(button2);
            }
        });

        button2 = (Button) findViewById(R.id.button2);

        checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
    }
}

=== activity_main.xml ===

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:id="@+id/ll1">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="42dp"
        android:layout_marginTop="22dp"
        android:text="Button" />

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

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="99dp"
        android:text="CheckBox" />

    <LinearLayout android:id="@+id/ll2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

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

    </LinearLayout>

</LinearLayout>
Darwind
  • 7,284
  • 3
  • 49
  • 48
  • I hope that is the right place to say thank you for the answer and the code excerpt! – Jean-Paul Bardou Apr 02 '13 at 12:01
  • 1
    You can give my answer, an accept and an upvote, which is also the right way to say thanks ;) – Darwind Apr 02 '13 at 12:23
  • Darwind, Thanks for the code. It comes up with errors, though, and because I am soooo new to this, I don't know what to do about them! Here is a list of what I encountered: // I used Alt-Control O to get the following imports... import android.app.Activity; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.LinearLayout; More in the next comment (How can I write more than this limited space!) – Jean-Paul Bardou Apr 02 '13 at 12:25
  • // line below gives the following error: The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){}) button1.setOnClickListener(new OnClickListener() { // line below gives the following error: The method onClick(View) of type new DialogInterface.OnClickListener(){} must override or implement a supertype method public void onClick(View arg0) { One more screen with the last two error messages follow... – Jean-Paul Bardou Apr 02 '13 at 12:27
  • Forgot to write @Darwind --- // line below gives the following error: The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){}) button3.setOnClickListener(new OnClickListener() { ... // line below gives the following error: The method onClick(View) of type new DialogInterface.OnClickListener(){} must override or implement a supertype method public void onClick(View v) { – Jean-Paul Bardou Apr 02 '13 at 12:29
  • Forgot to write @Darwind --- // line below gives the following error: The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){}) button3.setOnClickListener(new OnClickListener() { ... // line below gives the following error: The method onClick(View) of type new DialogInterface.OnClickListener(){} must override or implement a supertype method public void onClick(View v) { – Jean-Paul Bardou Apr 02 '13 at 12:35
  • You must change one import, where it says: `DialogInterface.OnClickListener` to `View.OnClickListener`, then it should work. – Darwind Apr 02 '13 at 12:49
  • Thank you, the change in import did the trick, and now I can see what the code is doing live! Can you explain for me how I could have known, when I used Control shift O to include the imports, that I had to choose View.onClickListener instead of the other one. And, if I don't impose too much on you, will you tackle my other question from the same day. I sorely miss an answer... Thanks again – Jean-Paul Bardou Apr 03 '13 at 09:47