rubin94
rubin94

Reputation: 542

Screen size and content dependant layouts

I would to inflate different layout for every screen size (different rows in list view), but for normal size I have to use two row layouts. Currently I'm checking the screen size, and if size is normal then I check once again which row inflate. Is there any better way to do this?

For example:
small

+---------+
|  TITLE  |
+---------+    
|  IMAGE  |
+---------+
| CONTENT |
+---------+

normal 1

+---------+
|  TITLE  |
+---------+    
|  IMAGE  |
+---------+
| CONTENT |
+---------+

or normal 2

+---------+--------+
|  TITLE  |  IMAGE |
+---------+--------+
| CONTENT |
+---------+

Upvotes: 0

Views: 61

Answers (1)

Felix Edelmann
Felix Edelmann

Reputation: 5162

You can use different layout file versions. For example, you're default layout is this:

+---------+
|  TITLE  |
+---------+    
|  IMAGE  |
+---------+
| CONTENT |
+---------+

You'll save it in res/layout/my_layout.xml.

Then you want another layout for screens wider than 400dp:

+---------+--------+
|  TITLE  |  IMAGE |
+---------+--------+
| CONTENT |
+---------+

You'll have to save it at res/layout-w400dp/my_layout.xml.

The file names must be the same, "w" means width (you can use "h" for height).

In your Java code, you propably don't have to change anything as long as you use the same ids for the same views in different layouts (e. g. R.id.title, R.id.image and R.id.content).

Upvotes: 1

Related Questions