Last active
January 2, 2016 05:49
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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