Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

Get excited for the Samsung Galaxy S5! Find everything you need and discuss it in our Galaxy S5 Forum!

test: Reply
 
LinkBack Thread Tools
Old May 10th, 2012, 04:00 PM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: May 2012
Posts: 4
 
Device(s):
Carrier: Not Provided

Thanks: 1
Thanked 0 Times in 0 Posts
Question Device Screen Density

Hello All,
First time posting so go easy. I have searched the entire internet, twice I think, trying to figure out how Android handles different screen densities. I have read the official documentation and about a million different posts, but I think I must have missed a fundamental concept somewhere along the way. My problem is simple and is more theoretical than practical, so please don't ask why I would want to do such a thing...

I have a phone and a 7" tablet, both 480x800, with 240dpi / 131dpi respectively. In my main layout, I have a background image with a burned in spot for password entry. I am trying to overlay a textview positioned perfectly on the image for nice effect. All looks well in the Eclipse design window, but when deployed to the devices, it's always one or the other depending on how I tweak my margins/padding.

It was my understanding that as long as I use dp/dip, the system would scale accounting for different dpi, especially on devices with the same resolution. I hope I have explained my situation in enough detail, and thanks for your help!

Nandroider is offline  
Reply With Quote
sponsored links
Old May 10th, 2012, 05:17 PM   #2 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,936
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 478 Times in 292 Posts
Default

Moved to the Application Development forum for better hits. =)
jonbonazza is offline  
Reply With Quote
Old May 10th, 2012, 05:22 PM   #3 (permalink)
Member
 
Join Date: Dec 2011
Location: Silicon Valley
Posts: 110
 
Device(s): few
Carrier: Not Provided

Thanks: 7
Thanked 32 Times in 29 Posts
Default

can you post your layout sample?
wubbzy is offline  
Reply With Quote
Old May 10th, 2012, 10:43 PM   #4 (permalink)
New Member
Thread Author (OP)
 
Join Date: May 2012
Posts: 4
 
Device(s):
Carrier: Not Provided

Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hey wubbzy, thanks for your quick reply. There's not much of a layout but I have included it anyway. I think I just missed Android101. I've written apps before but they were always deployed on the same devices. The real world seems scary. Here's my layout:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"
android:background="@drawable/background_image"
androidrientation="vertical">





<TextView

android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:layout_marginTop="230dp"
android:text="password"/>



</LinearLayout>


Just a single textview(password field) that I'm trying to position in a linear layout, but nicely mapped onto the backround image. I don't care about the image being stretched, I just want correct positioning on the image. I have finally got it figured out for my 2, now 3, devices but had to create custom device configurations in the Eclipse designer with corresponding alternate layout folders. This makes me think that I have to create a layout for every combination of size/density. Why when both devices are 480x800, couldn't my single default layout configuration that works on the phone, be displayed proportionately on the tablet? Thanks again.
Nandroider is offline  
Last edited by Nandroider; May 10th, 2012 at 10:46 PM.
Reply With Quote
Old May 11th, 2012, 01:33 AM   #5 (permalink)
Member
 
Join Date: Dec 2011
Location: Silicon Valley
Posts: 110
 
Device(s): few
Carrier: Not Provided

Thanks: 7
Thanked 32 Times in 29 Posts
Default

Why don't you use

<TextView
android:background="..."
/>

You can stretch image by changing padding/margins of textView

...or maybe I misunderstood? Please allow me to repeat your question in that case -
You would like to create a textview centered on an image?

Is that question correct?

...one more suggestions (edit) since I know I could be offline:
<RelativeLayout>
<ImageView ...>
<TextView
alignTop="@ImageViewID"
...more alignment options if required...
</TextView>
</RelativeLayout>
wubbzy is offline  
Last edited by wubbzy; May 11th, 2012 at 01:36 AM. Reason: Added relative layout
Reply With Quote
Old May 11th, 2012, 10:06 AM   #6 (permalink)
New Member
Thread Author (OP)
 
Join Date: May 2012
Posts: 4
 
Device(s):
Carrier: Not Provided

Thanks: 1
Thanked 0 Times in 0 Posts
Default

I have tried both these solutions with no result. The textView does not have to be centered on the image. It must be positioned where there is a spot for it within the background image.

I'm just realizing that if i use pixel units (px) for my padding/margins/textsize then the layout displays the same on both the phone and tablet, which makes sense, but I thought using pixels was frowned upon. Do I need to make alternate layouts for every device screen spec if they have different resolutions?
Nandroider is offline  
Reply With Quote
Old May 11th, 2012, 11:56 AM   #7 (permalink)
Member
 
Join Date: Dec 2011
Location: Silicon Valley
Posts: 110
 
Device(s): few
Carrier: Not Provided

Thanks: 7
Thanked 32 Times in 29 Posts
Default

One thing I'm certain about is you can't do this with layout XML, which expects things to be quite static. If you go the route of create density based layouts, you may end up with lot of device dependent layout. Unless as you said "just a theoretical issue", in which case you should be alright since you're just experimenting.

As far as eclipse comparison between messed up device layout and perfect eclipse layout you could try to play with ADT eclipse device density on top-left.

PS: Whether XML or dynamic I strongly suggest relative layout over linear, as it would give you more control on where to place elements.

Pixel vs device independent pixel is certainly frowned upon, but tell you the truth project requirements determine what needs to be used. Call is certainly yours to make. Its not so bad using pixels, but reason they're looked down upon is different devices have different densities and hence if you were using pixels you'll have to code in some logic to appropriately position layout elements. Another problem is that density is no measure of screen size or DPI, manufacturers are free to change it.

float scale = getBaseContext().getResources().getDisplayMetrics( ).density;
return (int) (pixel * scale + 0.5f);

converts pixels to DIP, change 'pixel' var to your input or whatever value you want to play with.

Usual way to accomplish what you're trying to do is split background image and 'burned in hole' into different image files. Use background in relative layouts and use burned in as textview/imageview to position with various 'alignment' options.

Yet another option is do basic XML layout (i.e. don't worry where they are and how it looks in eclipse) and 'adjust' it in code after inflating. Give you more precise and dynamic control.

Hopefully this helps....&...I've still resisted the question why, I hope? :-)
wubbzy is offline  
Last edited by wubbzy; May 11th, 2012 at 12:00 PM.
Reply With Quote
The Following 2 Users Say Thank You to wubbzy For This Useful Post:
alostpacket (May 15th, 2012), Nandroider (May 11th, 2012)
Old May 11th, 2012, 12:58 PM   #8 (permalink)
New Member
Thread Author (OP)
 
Join Date: May 2012
Posts: 4
 
Device(s):
Carrier: Not Provided

Thanks: 1
Thanked 0 Times in 0 Posts
Default

Wow thanks wubbzy! Your explanation above has helped me more than you know. I just figured that using device independant pixels in my xml layout would scale everything on a different screen density, with no extra work involved, again, not caring about image quality, etc. Thanks for clearing this up....and for not asking why
Nandroider is offline  
Reply With Quote
Old May 15th, 2012, 10:38 AM   #9 (permalink)
Over Macho Grande?
 
alostpacket's Avatar
 
Join Date: Nov 2009
Location: NY
Posts: 7,873
 
Device(s): GlassXE, MotoX, N5, N4, N7'12, GNex, N1, SGT10.1, Revue, Xoom, Eris, OG Droid
Carrier: TMO

Thanks: 4,582
Thanked 3,563 Times in 1,522 Posts
Default

You may also want to check out:

Draw 9-patch | Android Developers
alostpacket is offline  
Reply With Quote
Reply
Tags
android, density, dpi, multiple, screen


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 08:10 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.