0

I am developing an application to support multiple different screen sizes. I have a different design for tablet and phone. I have a list fragment and webfragment in tablet. I have a list fragment in phone. My requirement is that if the user clicks list view, if it is a tablet, I have to refresh the data, and if it is a phone I have to go to the next activity.

Can anybody tell me how to do this?

Thanks

Illidanek
  • 996
  • 1
  • 18
  • 32
mohan
  • 13,035
  • 29
  • 108
  • 178

3 Answers3

1

with the size of the screen only i think :

private boolean isTablet()
{
    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    display.getMetrics(displayMetrics);

    int width = displayMetrics.widthPixels / displayMetrics.densityDpi;
    int height = displayMetrics.heightPixels / displayMetrics.densityDpi;

    double screenDiagonal = Math.sqrt( width * width + height * height );
    return (screenDiagonal >= 9.0 );
}
Renaud Favier
  • 391
  • 6
  • 20
1

Define a Different id in the layout file for layout-large, layout-xlarge and layout.

res/layout

<FrameLayout android:id="@+id/normal>...</FrameLayout>

res/layout-large

<FrameLayout android:id="@+id/large>...</FrameLayout>

Use the id to detect whether its a Tablet or a Phone ( please not tablet could be running Android 2.2 and Phone could be running 4.0 so this is the best way to detect)

Rajdeep Dua
  • 11,190
  • 2
  • 32
  • 22
  • When you say use the id to check which one it is, do you mean to check if a specific layout component exists? like: findViewById(R.id.fragment_container) == null; ? – fersarr Feb 16 '14 at 18:20
0

Well you can do it like this:

private boolean isTabletDevice() {  
if (android.os.Build.VERSION.SDK_INT >= 11) { // honeycomb  
  // test screen size, use reflection because isLayoutSizeAtLeast is only available since 11  
  Configuration con = getResources().getConfiguration();  
  try {  
     Method mIsLayoutSizeAtLeast = con.getClass().getMethod("isLayoutSizeAtLeast", int.class);  
     Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE  
     return r;  
  } catch (Exception x) {  
     x.printStackTrace();  
     return false;  
  }  
}  
return false;  
}  

An idea was just to check the API level, but in future the tablets and smartphones could use the same Android version (because they merged tablet version with smartphones version starting with ICS), so this is not a good idea.

The next challenge is to make the code backward-compatible with reflection, because the low level Android devices API level < 9 doesn't support the required method for checking the screen resolution.

Good luck, Arkde

Aurelian Cotuna
  • 3,076
  • 3
  • 29
  • 49