Skip to content

Instantly share code, notes, and snippets.

@nodinosaur
Last active January 2, 2016 05:49
Show Gist options
  • Save nodinosaur/8259270 to your computer and use it in GitHub Desktop.
Save nodinosaur/8259270 to your computer and use it in GitHub Desktop.
Tablet Resolver Activity: In rare instances a Tablet layout may not suffice as components may vary from Phone to Tablet devices
package uk.co.example.android.util;
import android.content.Context;
public class DeviceUtils {
/**
* The minimum width that would classify the device as a tablet.
*/
private static final int MINIMUM_TABLET_WIDTH_DP = 600;// 720
/**
* @param context Android's context
* @return Whether the app is should treat the device as a tablet for layout.
*/
public static boolean isTablet(Context context) {
int minimumScreenWidthDp = context.getResources().getConfiguration().smallestScreenWidthDp;
return minimumScreenWidthDp >= MINIMUM_TABLET_WIDTH_DP;
}
}
package uk.co.example.android.home;
import uk.co.example.android.home.phone.HomeActivity;
import uk.co.example.android.home.tablet.HomeActivity;
/**
* Resolves Activitites for Phone & Tablet
*/
public class HomeActivity extends TabletResolverActivity {
protected final Class forTablet() {
return uk.co.example.android.home.tablet.HomeActivity.class;
}
protected final Class forPhone() {
return uk.co.example.android.home.phone.HomeActivity.class;
}
}
package uk.co.example.android.base;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import uk.co.example.android.dependency.DependencyResolverImpl;
import uk.co.example.android.util.DeviceUtils;
/**
* Abstract Class; The Shell activity must extend this and
* implement the methods 'forTablet()' and 'forPhone()'
* returning the correct Activity for each.
*/
public abstract class TabletResolverActivity extends Activity {
protected abstract Class forTablet();
protected abstract Class forPhone();
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
DeviceUtils deviceUtils = DependencyResolverImpl.getInstance().getDeviceUtils();
Class clazz = deviceUtils.isTablet(this) ? forTablet() : forPhone();
Intent intent = new Intent(getIntent());
intent.setComponent(new ComponentName(this, clazz));
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment