Android button becomes small in Nexus 5 -


i creating button dynamically particular screen. visible in emulator looking small in device (nexus 5).

in emulator:

enter image description here

in device: enter image description here

i using below code button creation in code:

layoutparams updt_btn_params = new layoutparams(); update_data = new button(this); update_data.settext("update"); update_data.settextsize(9); updt_btn_params.width=80; updt_btn_params.height=45; updt_btn_params.gravity=gravity.center_horizontal; update_data.setonclicklistener(update_listnr); update_data.setlayoutparams(updt_btn_params); 

what else have getting buttons in device. tia

the problem using these:

updt_btn_params.width=80; updt_btn_params.height=45; 

with setting width , height in pixels, should never do. different devices have different pixel densities means size of pixels varies device device. nexus 5 has quite high pixel density makes buttons small.



there 2 ways around this:

1. define values in dimen.xml in resources

in resources there folder "values" should contain dimen.xml. in can define dimensions buttons this:

<dimen name="width">80dp</dimen> <dimen name="height">45dp</dimen> 

then can read them code via:

updt_btn_params.width = getresources().getdimensionpixelsize(r.dimen.width); updt_btn_params.height = getresources().getdimensionpixelsize(r.dimen.height); 

2. use xml

but if can define whole layout of activity in xml-file.

in there can define width , height in "dp" this:

layout_width="80dp" layout_height="45dp" 

it essential use "dp" instead of "px" make buttons same on every device.


Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -