Jørgen R
Jørgen R

Reputation: 10806

Android background based on screen size

I'm creating an Android application, and want different background images for phone sized screens and tablet sized ones. I already have different resolution images for different resolutions (mdpi, hdpi etc.). The challenge is that most tablets use mdpi, but some phones might use it too.

I've tried to do some research, but only it seems to be possible to define screen sizes in the layouts, e.g. res/layout/layout-large. Does that mean I have to create a separate xml for tablets, linking to a @drawable/tablet-background? Or is there any other solutions, either xml or programmatic?

Upvotes: 0

Views: 364

Answers (2)

user
user

Reputation: 87064

and want different background images for phone sized screens and tablet sized ones

Try the qualify the drawable folder with the desired screen size and also the density value for the target device, for example drawable-large-mdpi. The screen size must come first based on the configuration qualifiers table.

Upvotes: 2

davide.web
davide.web

Reputation: 95

mdpi, hpdi etc. refers to screen density and that's independent from the device being a tablet or a phone. If I understood correctly, what you're trying to do is to use different background images for tablet and for phones. There are three ways:

  1. Create an xml layout for tablet and an xml layout for phone using layout-large, layout-xlarge folders
  2. Using the same xml layout (except for an element id) in layout and in layout-large folders and change the background programmatically using something like this:

    public boolean isTablet(){
        return (findViewById(R.id.tablet_view) != null); 
    }
    

    where tablet_view is an element id present only in the layout-large xml layout.

  3. Another trick: Determine if the device is a smartphone or tablet?

Upvotes: 3

Related Questions