0

I have list of fragment containing buttons and one fragment, that shows up, after slide gesture is made ( similar to menu navigation drawer ). Problem is, that the buttonFragments always stay on top of my menuFragment.

Illustration:

enter image description here

I tried to use View.bringToFront() method, but it didnt work. Am I doing it wrong, or should I do it in different way?

Activity EditKeyboard.java:

public class EditKeyboard extends AppCompatActivity{

        ArrayList<Fragment> keyFragments; // buttons
        Fragment bMenu; // menu

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

            createButtons();
            createBMenu();
        }


        public void createBMenu(){
            bMenu = ButtonMenuFragment.newInstance("a", "a");
            getFragmentManager().beginTransaction().add(R.id.edit_keyboard_layout,
                    bMenu, "bMenu").commit();
            View v = bMenu.getView();
            v.bringToFront(); // TODO bring menu to top
            ((View) v.getParent()).requestLayout();
            ((View) v.getParent()).invalidate();
            getFragmentManager().popBackStackImmediate("TAG", FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }

So what am I doing wrong?

Or should I use different layout (Relative or Frame one?)

Ford O.
  • 1,394
  • 8
  • 25

2 Answers2

1

I would use relativelayout.

<RelativeLayout>
     ...
     <fragment 
         android:name= "packagename.button_fragment"
         ...width, height, id, postion... />

     <fragment 
         android:name= "packagename.bMenu"
         ...width, height, id, postion... />

Here because bMenu is placed after button_fragment, it will be on top of button_fragment. So button_fragment is inflated first while rendering followed by bMenu putting it on top of it (subject of positioning being such).

suku
  • 10,507
  • 16
  • 75
  • 120
  • I am creating multiple button_fragments in `for` cycle, but in your example you use only one in xml. Will it still work? – Ford O. Apr 04 '16 at 08:38
  • Basic idea is the same, you add your fragments after the background fragment is rendered. So add your button_fragments first programmatically and then add your bMenu fragment programmatically. Don't include both in your layout then. You will have to set position and height and width in the java code – suku Apr 04 '16 at 10:11
  • that's exactly what am i doing right now, only difference is, that i use absolute layout. Does that mean, that absolute layout doesn't have this feature, or am i doing something wrong? – Ford O. Apr 05 '16 at 10:18
  • I have never used absolute layout..but you can do this with relative layout – suku Apr 05 '16 at 10:21
0

I have solved it by wrapping each button into FrameLayout.

Ford O.
  • 1,394
  • 8
  • 25