-9

To determine the device type(i.e. phone or tablet), Determine if the device is a smartphone or tablet? "isTablet" is the variable which holds the device type. To change icon dynamically in java class :

if(isTablet){
// apply tablet icon
}else{
//apply phone icon
}

But if I have two sets of icons(one set for phone and other is for tablets which is bigger in size than phone icons) then, how to set the icon dynamically?

Durga M
  • 534
  • 5
  • 17

3 Answers3

0

EDIT:

You want to use specific sets for tablets vs smartphones. In this case, you can't just use the screen density because it isn't always related only to the screen size.

Check this question and answer on how to decide if it's a tablet or a smarthphone: Determine if the device is a smartphone or tablet?


You don't need to change icons manually on any device screens, it will automatically choose the right density.

Your icons should be available in multiple sizes, one for each screen density, but they must be each in its density folder.

You'll notice your app contains folders starting with drawable-, such as drawable-xhdpi. Those are the densities (xhdpi etc). You can find the specifications for each size on the documentation. Generate each size for your icon and put in those folders, it will be done.

The launcher icons should also be placed in folders like that, but ideally inside the "mipmap-xhdpi" and related folders. You can check this answer with some detailed info about the mipmap and drawable folders.

George
  • 6,886
  • 3
  • 44
  • 56
  • Thanks for your response. But I think you have mis-understood the question. I am asking about how to set icon in the different device type (like phone and tablet). I have one set of icons for phone and other set of icon for tablet. How to set the same in run-time with out using if-else? The way android takes respective layouts automatically. – Durga M Jul 27 '17 at 13:29
  • @DurgaM Is this case check this question: https://stackoverflow.com/questions/9279111/determine-if-the-device-is-a-smartphone-or-tablet – George Jul 27 '17 at 14:03
0

Unfortunately, you can't do this programmatically.

However, you can place drawables in an array of folders to get (close to) your desired effect.

Screen sizes are defined by their size and their pixel density, which gives you a handful of options that look like this:

drawable-large-hdpi, drawable-xlarge-hdpi, drawable-large-xhdpi, drawable-xlarge-xhdpi, etc...

For example, a nexus 7 tablet is considered a large-hdpi device. Phones, by contrast, may be considered a normal-mdpi device.

An extensive list of screen metrics can be found here: https://material.io/devices/

You can refer to the supporting multiple screen sizes documentation, as well: https://developer.android.com/guide/practices/screens_support.html

anomeric
  • 688
  • 5
  • 17
0

In my opinion, the question shouldn't be "is the user on a phone or a tablet", because that's hiding the real question: "is the user on a device large enough that I should display a different UI?".

It is generally accepted that any device that is at least 600dp wide (on its shortest dimension) is a "tablet" for these purposes. And, as it turns out, the Android resource framework makes it really easy to use different drawables in these cases.

You probably already have your icons split up for different resolutions:

/res/drawable-mdpi/icon.png
/res/drawable-xhdpi/icon.png
etc

Use the same pattern, but add the sw600dp qualifier for your "tablet" versions:

/res/drawable-sw600dp-mdpi/icon.png
/res/drawable-sw600dp-xhdpi/icon.png
etc

Now there's no need to write any Java code at all. If you have an <ImageView> tag that specifies android:src="@drawable/icon", it will automatically use the "phone" version on smaller screens and the "tablet" version on any screen that's at least 600dp wide.

Ben P.
  • 52,661
  • 6
  • 95
  • 123