0

I have a first TableLayout with rows and cells generated dynamically by code.
Now, i have a second TableLayout smaller composed by 3 row.
This second table is showed by clicking any cell in the first Table.
Now my question is:
How i can align this second Table at the position of the cell clicked, overlapped to the first table?

Thanks for the advices.


EDIT: unfortunally i can't show the layout to public. but i drawed how it should be. i'm sorry for the unconvenience.

first table. click on cell

show the second table at the senter of cell clicked

A.Scudino
  • 3
  • 2

1 Answers1

0

You may choose to use a custom Dialog Fragment to do so.

First, get the global x & y position of the cell of the first table view that you clicked. From this post:

Rect myViewRect = new Rect();
myView.getGlobalVisibleRect(myViewRect);
float cx = myViewRect.exactCenterX();
float cy = myViewRect.exactCenterY();

Then, create a class that extends DialogFragment (let's call it SecondTableFragment). To change the Dialog from your default Android style dialogs, you need to do a few things

Make the title disappear

@Override
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    return dialog;
}

Set the width of the Dialog to fit your second table view

    @Override
    public void onResume() {
    getDialog().getWindow().setLayout([PARENT_LAYOUT_TYPE].LayoutParams.WRAP_CONTENT, [PARENT_LAYOUT_TYPE].LayoutParams.WRAP_CONTENT);
    super.onResume();
}

Set the position of your dialog window above that of your clicked cell (this should most probably be done in onCreateDialog() as well):

 WindowManager.LayoutParams wmlp =  dialog.getWindow().getAttributes();

 wmlp.gravity = Gravity.TOP | Gravity.LEFT;
 wmlp.cx = cx;   //x position
 wmlp.cy = cy;   //y position

For the logic behind filling out the Dialog view itself, you need to create a new layout file for the 2nd table layout (like you do for a new Activity) and inflate it and fill it up with relevant information like any other normal fragment. Make sure to pass in the x & y co-ordinates you want the dialog to be in when calling it's newInstance() method. See Google's Documentation

Once everything's set up, you can then show a "2nd table Dialog" above the 1st TableLayout with the relevant information by using Fragment Manager (or ChildFragmentManager if you are already in a Fragment)

SecondTableFragment fragment = SecondTableFragment.newInstance(cx, cy, RELEVANT INFORMATION ...);
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment prev = getSupportFragmentManager().findFragmentByTag("secondTable");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.add(fragment, "secondTable");
    ft.addToBackStack(null);
    ft.commitAllowingStateLoss();
    fragment.setCancelable(false);
Mercato
  • 559
  • 4
  • 12
  • First of all thanks for you reply. In second way, i don't understand the method "findFragmentByTag("secondTable");". What's "secondTable" ? i've to define the table like a fragment in the xml?
    Again, sorry my newbe question!
    – A.Scudino Sep 22 '17 at 16:24
  • no problem; as you can observe, I add the fragment to the Fragment stack by calling ft.add(fragment, "secondTable"). This means I am giving it a "tag" of String "secondTable". That line "findFragmentByTag("secondTable") checks if a fragment with that exact tag already exists, and if it does, removes it first, so you can show a new one. – Mercato Sep 23 '17 at 12:32
  • no problem A.Scudino, if it helped you please do select this as the correct answer (the tick mark)! happy coding! – Mercato Sep 25 '17 at 13:03