3

I have an android app that works on phone/tablet. I want to add support for Android TV.

Official documentation says to create a separate activity for TV.

Only difference between phone and TV is presentation layer (i.e. view), so I should only have to create new layouts, not the controller as well.

I created layouts that work for TV and use the same controller. Problem is that I cannot specify which layouts are for TV and which ones are for phones/tablets.

I cannot make assumptions about screen size and device type. Some phones have higher screen resolution than TVs and vice versa.

How can I provide different layouts for phone and TV?

If there is a better way to do this without writing (or copying) my existing java code I would accept that too.

3 Answers 3

8

How can I provide different layouts for phone and TV?

If this is an official Android TV environment, AFAIK res/layout-television/ should work. See the legendary Table 2.

2
  • what about UI mode in android resources?
    – Basi
    Mar 12, 2019 at 11:27
  • Awesome! I created layout, layout-land and layout-land-television with different layouts. layout-land is used on phone/tablet, layout-land-television on a TV. Same for styles. Works great! Thanks!
    – under
    Mar 12, 2019 at 11:43
2

A more reliable way to identify if it is a TV, or Phone could be. It can even check if it is watch, appliance or other devices.

UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
    Log.d(TAG, "Running on a TV Device");
} else {
    Log.d(TAG, "Running on a non-TV Device");
}
0

you can use this boolean check to se if you are on a TV :

    context.getPackageManager().hasSystemFeature("com.google.android.tv")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.