1

I have create a new layout which is type of FrameLayout and I have added four elements of image view I want to take the first image view to the end programmatically
in short reorder the elements of framelayout programmatically. suppose we do that listen to button clickListener

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/framelayout">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/a20141008_091817"
        android:id="@+id/i1"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/a20141008_091819"
        android:id="@+id/i2"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/a20141008_091821"
        android:id="@+id/i3"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/a20141008_091823"
        android:id="@+id/i4"/>

    </FrameLayout>

I appreciated any help.

Naham Soft
  • 91
  • 2
  • 12

3 Answers3

2

In this case, it seems you want to re-order the child-views of the FrameLayout. I'd suggest you simply use the ViewGroup, and use getChildAt(some-index), addView(viewToAdd, index-to-place-it) as well as removeViewAt(some-index) to achieve the re-ordering; In my suggested solution - I assume the layout won't change (this can be improved):

  ViewGroup viewGroup = (ViewGroup)this.findViewById(android.R.id.content).getRootView();

        int first = 0;
        int second = 1;
        int third = 2;
        int fourth = 3;

        View view1 = viewGroup.getChildAt(first);
        View view4 = viewGroup.getChildAt(fourth);
        viewGroup.removeViewAt(first);
        //move fourth to first
        viewGroup.addView(view4, first);
        viewGroup.removeViewAt(fourth);
        //move first to fourth
        viewGroup.addView(view1, fourth);

        View view2 = viewGroup.getChildAt(second);
        View view3 = viewGroup.getChildAt(third);

        viewGroup.removeViewAt(second);
        //move third to second
        viewGroup.addView(view3, second);
        //move second to third
        viewGroup.removeViewAt(third);
        viewGroup.addView(view2, third);

I hope this helps you and others. You can also checkout the selected answer in this closely related question and this one, and the selected answer here shows how to re-order views.

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
  • where is the swapping form the front to the end ? – Naham Soft May 16 '16 at 20:48
  • OK, I thought the code sample will get you started. Let me add the piece where you re-order the items. One second... – ishmaelMakitla May 16 '16 at 20:54
  • updated the answer, please check it out and let me know if this works. – ishmaelMakitla May 16 '16 at 21:15
  • 1
    first of all Thanks so much bro. your way it can work but sorry it is out of my question cuz I am looking for how to do the same work of yours using the framelayout I find this way and it works fine frameLayout.bringChildToFront(frameLayout.getChildAt(1)); – Naham Soft May 16 '16 at 21:28
  • Noted. Well done on getting that one working. By the way, are you using index `1` for first - shouldn't this be `0`? Let me know if I should update the answer or if you've got a working code already. – ishmaelMakitla May 16 '16 at 21:38
  • 1
    I know that bro. thanks for attention that is just suggestion. sure I will send U the correct answer wait a minute – Naham Soft May 16 '16 at 21:55
  • Hi . you can see how did I solve my problem . and thanks so much for your help. – Naham Soft May 19 '16 at 18:38
1

first of all. Thanks so much for every one try to help me. then i figure out my problem in good practice way and I wish to share it with you may any one will need it in future. the steps

  1. create a frameLayout as in the xml in my question
  2. add your image into frameLayout
  3. create a java file and connect it to the xml
  4. we must to know the ids of all the images into the framlayout
  5. create a timer which will change the order of image in X seconds
  6. get the count of all the images into the framLayout
  7. create Intager var which will determine which is next of image to bring to front
  8. still calling the timer to work in x times as i mentioned
  9. create method to stop the timer if we want in my case I stop the timer in the onPause() method

    this is the java file which will do all the above steps. wish it help any one

public class FrameLayout5 extends AppCompatActivity {

FrameLayout frameLayout; 
Handler handler;             // this class will work as timer for change the order of images in delay 
List<Integer> views;    //  this Array list will store all the ids of all the ImageViews into framelayout 
int counter;        //  this counter will help us to find the next element to bring it to front 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_frame_layout5);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    frameLayout = (FrameLayout) findViewById(R.id.framelayout);
    handler = new Handler();
    views = new ArrayList<Integer>();
    counter=0;

    // get all the children of framelayout 
    for (int i = 0; i < frameLayout.getChildCount() ; i++) {
        views.add(frameLayout.getChildAt(i).getId());
    }

    handler.postDelayed(runnable, 1000); // 1000 mean that this method will excute every 1 second 


}

Runnable runnable = new Runnable() {
    @Override
    public void run() {

        int i=counter%(frameLayout.getChildCount());    
            counter++;
        ImageView a = (ImageView) findViewById(views.get(i));

        frameLayout.bringChildToFront(a);

        handler.postDelayed(runnable, 1000);
        Log.d("run", "called "+i);
    }
};

@Override
protected void onStop() {
    super.onStop();
    handler.removeCallbacks(runnable);
}

}

@ExpensiveBelly @ishmaelMakitla

Naham Soft
  • 91
  • 2
  • 12
0

What about changing the visibility of the ImageViews to invisible/gone so you just display the one you want to show?

ExpensiveBelly
  • 444
  • 5
  • 8
  • I thought about that but I think it is not good practice and in the framelayout I have read that it is possible to reorder its views. by the way thanks for your reply – Naham Soft May 16 '16 at 20:34
  • Hi . you can see how did I solve my problem . and thanks so much for your help. – Naham Soft May 19 '16 at 18:40