I have a very simple app which removes buttons and re-adds buttons to certain layouts when a button is clicked. The problem is, this process is taking a very long time. My question is: How can I make this process faster

I have a simple program which swaps two buttons:

  1. Remove view (button)
  2. Generate random number, and from my button array list, get the button in the array list with the index of that random number
  3. Remove that random view (button)
  4. Add the two buttons in each others places.

Now, the problem is that I want the buttons to swap places right after I click on a button. Instead, the program is taking about three seconds to do the above four tasks after I click the button.

Here is the code:

    red.setOnClickListener(new View.OnClickListener() {
        public void onClick(final View v) {

                // Remove the clicked button from it's frame layout parent.
                    ViewManager p = (ViewManager) v.getParent();
                    // Find another button randomly.
                    Random rng = new Random();
                    View v2;
                        int i = rng.nextInt(4);
                        v2 = buttons.get(i);
                    while (v2 == v); // Loop until you get a different button to swap with.
                    // Remove the other button from it's frame layout parent.
                    ViewManager p2 = (ViewManager) v2.getParent();
                    // Now simply insert each button into the other buttons frame layout.
                    p.addView(v2, v2.getLayoutParams()); //Adding v2, the random button, into where v, the button clicked, was before(p)!
                    p2.addView(v, v.getLayoutParams()); //Adding v, the button clicked, into where v2, the random button, was before(p2)

So, having done my research, I learned that one thing that may be slowing me down could be the fact that the main UI is handling all of this work. So, I decided to put all of this code inside of a thread to run in the background:

 red.setOnClickListener(new View.OnClickListener() {
        public void onClick(final View v) {

            new Thread(new Runnable() {

                public void run() {



But putting this code in a thread gave me another exception. This is probably happening because I am trying to update the UI (remove and add buttons) from the thread.

 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

So, I don't know how I should approach this problem, since I do need to change my views...

Another thing that could be slowing down my app (from the research I did) is that my layout might be inflating after a while, due to such a giant layout. My layout is pretty big, so that may be it:

enter image description here

I doubt it is the layout though, as the screen loads pretty fast. What happens is that when I click a button, the screen freezes for three seconds, and then updates the UI with the completed tasks.

Could I somehow take up more memory to be faster as an app? The whole basis of my app runs on being able to complete those four tasks and update the UI as soon as the user presses on a button. What else should I do? I have also added this line in my manifest after looking at similar questions:

android:largeHeap = "true"

Now, what else should I do? Thanks so much for your help, I really appreciate it. I have been stuck on this all day...so I really (REALLY!) would like any feedback.

Thanks so much for your time

  • \$\begingroup\$ What do you need button swapping for? If you swap only text, for example, you could try avoiding this completely. \$\endgroup\$
    – wondra
    Nov 27, 2015 at 9:03
  • \$\begingroup\$ @wondra No, I am swapping the image buttons for my game. I have to complete that process. Maybe the images could be slowing it down? If so, how can I fix that? \$\endgroup\$ Nov 27, 2015 at 17:41
  • \$\begingroup\$ I think what wondra is suggesting is that swapping the content and function of the buttons might force less heavy lifting onto the UI system than removing & re-adding the buttons themselves, while still giving an identical result as far as the player can see. Have you looked into such an option? \$\endgroup\$
    – DMGregory
    Nov 27, 2015 at 17:44
  • \$\begingroup\$ @DMGregory Yeah, maybe just changing the image of a random button and swapping listeners instead would be better. Except I have no idea how I can do that... What should I do? Thanks so much! \$\endgroup\$ Nov 27, 2015 at 18:09


You must log in to answer this question.