2

I'm dealing with my first Android app and I'm a bit lost right now.

I have a ListView, and when the user clicks on one element from this list, a detail view should appear. I wouldn't be any trouble but my app will run on both tablet and phone, and I want to show this detail view modally on tablet and normally (using fragmentManager.beginTransaction().add(...)) on phone.

enter image description here

I know how to create a DialogFragment (in fact, I use one somewhere else in the app), but I'm not sure if a DialogFragment can be use with a normal transaction.

And I don't know how can distinguish if my app is running on phone or tablet (apart of creating a layout inside a layout-sw600dp folder)

EDIT

Well, thanks to the answer of Margarita I reached a solution (not the only one, but one of some possibilities).

1) Define B as a DialogFragment

2) Create a values.xml inside res/values folder with this code

  <bool name="isTablet">false</bool>

3) Create another values.xml inside res/values-sw600dp folder with this code.This way, I have a boolean resource telling me if the app is running on a tablet.

  <bool name="isTablet">true</bool>

4) On Fragment A (who is in charge of showing Fragment B), create a method similar to this:

private void showFragmentB(){

    Resources res = getResources();
    boolean isTablet = res.getBoolean(R.bool.isTablet);

    if (isTablet) {

        // We're running on a tablet, so we show Fragment B as a dialog    
        FragmentB.newInstance().show(getActivity().getSupportFragmentManager(), FragmentB.class.toString());

    } else {
        // We're running on a phone, adding Fragment B to the stack 
        // with some animation
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        transaction.setCustomAnimations(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
        transaction.add(R.id.container, FragmentB.newInstance(), FragmentB.class.toString());
        transaction.addToBackStack(FragmentB.class.toString());
        transaction.commit();

    }
}

5) Call that method whenever Fragment B should appear.

Thanks for helping me.

WedgeSparda
  • 1,161
  • 1
  • 15
  • 40
  • "I'm not sure if a DialogFragment can be use with a normal transaction" -- AFAIK it can. Rather handy that way. "apart of creating a layout inside a layout-sw600dp folder" -- well, you could create a `res/values-sw600dp/bools.xml` to have a `boolean` resource, if you didn't need a custom layout. I would use resources, though, versus looking stuff up at runtime using `DisplayMetrics` or whatever. – CommonsWare Apr 10 '15 at 15:45

2 Answers2

0

There is no other (reliable enough and supported) way to distinguish phone and tablet except of providing different layouts (or resources) for different screen sizes.

There are 2 ways using a simple Fragment and FrameLayout and using a DialogFragment (though DialogFragment can be used as a simple Fragment too):

  1. You can define a <FrameLayout> container in your A layout (and insert a fragment there in runtime). Then in case of a phone, this FrameLayout will have "match_parent" for both layout_width and layout_height and in case of a tablet it can be either wrap_content`` ormatch_parent``` with margins (or, of course, some predefined value in dp).

  2. Or you can use one DialogFragment that will have e.g. different themes in res/values and res/values-sw600dp. If your layout is "busy" enough - many widgets - then you maybe even do not need two themes. You can just use wrap_content and the layout will shrink to the width for phones and will take as much space as it needs on tablets.

I'd rather use a second way because it seems to be a lot cleaner.

Margarita Litkevych
  • 2,086
  • 20
  • 28
0

you can use a different layout for tablet and mobile and use a FrameLayout in both of layout with a same id and fragment B be a child Fragment that replace inside this FrameLayout. please see these Layout for tablets in Android

Fragments within Fragments

Community
  • 1
  • 1
zohreh
  • 1,055
  • 1
  • 9
  • 26
  • And it is possible to hide that fragment B until the user clicks on one element from the list? – WedgeSparda Apr 10 '15 at 16:10
  • you click on one element from list and then fragment b display in FrameLayout and you cant now click on other element you should press back and hide Fragment B and then you can click on other element – zohreh Apr 10 '15 at 16:18