How to set width and height of a View in DP through code not in XML ?

by Mansoor » Thu, 27 Jan 2011 13:47:47 GMT


Sponsored Links
 hi ,
i am creating a Image view  through code dynamically based on certain
condition as below:

ImageView t = new ImageView(mContext);
t.setLayoutParams(new LinearLayout.LayoutParams(390,
                                                                300));
the width and height is in PX i.e 390 PX and 300 PX

but i want to know how to set width and height in dp instead of px
programmatically . In xml we can do but i have no idea how to do it in
code
please help me .

Thanks
Mansoor V.M

-- 



Other Threads

1. about android wifi problem

LInux:2.6.27

rootfs:android1.5

usb wifi:rt3070


# ifconfig ra0 up
[ 727.970373] CEEPDQ
[ 727.973934] #
[ 727.990331] CEEPDQ
[ 727.993865] #
[ 728.010325] CEEPDQ
[ 728.013855] #
[ 728.030324] CEEPDQ
[ 728.033854] #
[ 728.050326] CEEPDQ
[ 728.053856] #
[ 728.070324] CEEPDQ
[ 728.073854] #
[ 728.090324] CEEPDQ
[ 728.093854] #
[ 728.110324] CEEPDQ
[ 728.113856] #
[ 728.130323] CEEPDQ
[ 728.133853] #
[ 728.150324] CEEPDQ
[ 728.153855] #
[ 728.170324] CEEPDQ
[ 728.173856] #
[ 728.190324] CEEPDQ
[ 728.193856] #
[ 728.210324] CEEPDQ
[ 728.213855] #
[ 728.230330] CEEPDQ
[ 728.233863] #
[ 728.250325] CEEPDQ
[ 728.253857] #
[ 728.266879] #
[ 728.280314] CEEPDQ
[ 728.283855] #
[ 728.300313] CEEPDQ
[ 728.303853] #
[ 728.320313] CEEPDQ
[ 728.323854] #
[ 728.340322] CEEPDQ
[ 728.343873] #
[ 728.360313] CEEPDQ
[ 728.363855] #
[ 728.380312] CEEPDQ
[ 728.383853] #
[ 728.400311] CEEPDQ
[ 728.403852] #
[ 728.420312] CEEPDQ
[ 728.423852] #
[ 728.440312] CEEPDQ
[ 728.443853] #
[ 728.460312] CEEPDQ
[ 728.463852] #
[ 728.480312] CEEPDQ
[ 728.483852] #
[ 728.500311] CEEPDQ
[ 728.503851] #
[ 728.520330] CEEPDQ
[ 728.520381] #
[ 728.530348] CEEPDQ
[ 728.533880] #
[ 728.550326] CEEPDQ
[ 728.550359] #
[ 728.560312] CEEPDQ
[ 728.563854] #
[ 728.580312] CEEPDQ
[ 728.583853] #
[ 728.600312] CEEPDQ
[ 728.603853] #
[ 728.627635] ifconfig: page allocation failure. order:6, mode:0x1
[ 728.630806] [<c010441c>] (dump_stack+0x0/0x14) from [<c0150a0c>]
(__alloc_pages_internal+0x310/0x3fc)
[ 728.640070] [<c01506fc>] (__alloc_pages_internal+0x0/0x3fc) from
[<c0105d54>] (__dma_alloc+0x118/0x490)
[ 728.649378] [<c0105c3c>] (__dma_alloc+0x0/0x490) from [<c010615c>]
(dma_alloc_coherent+0x60/0x68)
[ 728.658335] [<c01060fc>] (dma_alloc_coherent+0x0/0x68) from [<c02c5560>]
(hcd_buffer_alloc+0x60/0xa8)
[ 728.667485] r7:c88a1002 r6:00000002 r5:00000020 r4:c88a1194
[ 728.673073] [<c02c5500>] (hcd_buffer_alloc+0x0/0xa8) from [<c02b9c70>]
(usb_buffer_alloc+0x2c/0x30)
[ 728.682102] r5:00000000 r4:c88a1150
[ 728.685659] [<c02b9c44>] (usb_buffer_alloc+0x0/0x30) from [<bf0c9a1c>]
(NICInitTransmit+0xbc/0x828 [rt3070sta])
[ 728.695731] [<bf0c9960>] (NICInitTransmit+0x0/0x828 [rt3070sta]) from
[<bf0ca1e4>] (RTMPAllocTxRxRingMemory+0x5c/0xd4 [rt3070sta])
[ 728.707520] [<bf0ca188>] (RTMPAllocTxRxRingMemory+0x0/0xd4 [rt3070sta])
from [<bf0b3b84>] (rt28xx_open+0xb8/0x5b0 [rt3070sta])
[ 728.718865] r5:c6c90000 r4:00000000
[ 728.722376] [<bf0b3acc>] (rt28xx_open+0x0/0x5b0 [rt3070sta]) from
[<bf0b4460>] (MainVirtualIF_open+0xec/0x150 [rt3070sta])
[ 728.733403] [<bf0b4374>] (MainVirtualIF_open+0x0/0x150 [rt3070sta]) from
[<c0315d84>] (dev_open+0xa4/0x100)
[ 728.743124] r5:c6c9002c r4:c6c90000
[ 728.746680] [<c0315ce0>] (dev_open+0x0/0x100) from [<c0314c28>]
(dev_change_flags+0x78/0x180)
[ 728.755188] r5:c6c90000 r4:00000041
[ 728.758745] [<c0314bb0>] (dev_change_flags+0x0/0x180) from [<c0357d58>]
(devinet_ioctl+0x774/0x86c)
[ 728.767843] r7:00000000 r6:00000000 r5:c7bd4000 r4:beb3e9b8
[ 728.773414] [<c03575e4>] (devinet_ioctl+0x0/0x86c) from [<c035855c>]
(inet_ioctl+0x1ac/0x1c4)
[ 728.781921] [<c03583b0>] (inet_ioctl+0x0/0x1c4) from [<c030626c>]
(sock_ioctl+0xc8/0x24c)
[ 728.790081] [<c03061a4>] (sock_ioctl+0x0/0x24c) from [<c0180a14>]
(vfs_ioctl+0x34/0x98)
[ 728.798095] r7:c4df2620 r6:beb3e9b8 r5:00008914 r4:c4df2620
[ 728.803707] [<c01809e0>] (vfs_ioctl+0x0/0x98) from [<c0180c7c>]
(do_vfs_ioctl+0x204/0x338)
[ 728.811955] r7:c4df2620 r6:beb3e9b8 r5:c4df2620 r4:00000003
[ 728.817596] [<c0180a78>] (do_vfs_ioctl+0x0/0x338) from [<c0180df0>]
(sys_ioctl+0x40/0x68)
[ 728.825755] r9:c7bd4000 r8:c00ffe48 r7:c4df2620 r6:00008914 r5:beb3e9b8
[ 728.832264] r4:00000003
[ 728.834870] [<c0180db0>] (sys_ioctl+0x0/0x68) from [<c00ffca0>]
(ret_fast_syscall+0x0/0x2c)
[ 728.843210] r7:00000036 r6:00000000 r5:00000004 r4:000ba6c7
[ 728.848844] Mem-info:
[ 728.851101] DMA per-cpu:
[ 728.853619] CPU 0: hi: 42, btch: 7 usd: 35
[ 728.858433] Active:9815 inactive:4101 dirty:68 writeback:0 unstable:0
[ 728.858443] free:8283 slab:996 mapped:3229 pagetables:467 bounce:0
[ 728.871124] DMA free:33132kB min:1440kB low:1800kB high:2160kB
active:39260kB inactive:16404kB present:130048kB pages_scanned:0
all_unreclaimable? no
[ 728.884476] lowmem_reserve[]: 0 0 0
[ 728.887904] DMA: 2455*4kB 2126*8kB 256*16kB 27*32kB 11*64kB 5*128kB
0*256kB 0*512kB 0*1024kB 0*2048kB 0*4096kB = 33132kB
[ 728.898755] 7330 total pagecache pages
[ 728.902486] 0 pages in swap cache
[ 728.905791] Swap cache stats: add 0, delete 0, find 0/0
[ 728.911050] Free swap = 0kB
[ 728.913856] Total swap = 0kB
[ 728.924840] 32768 pages of RAM
[ 728.925056] 8509 free pages
[ 728.928870] 8112 reserved pages
[ 728.930958] 996 slab pages
[ 728.933646] 12045 pages shared
[ 728.936684] 0 pages swap cached
[ 728.939897] <-- ERROR in Alloc TX TxContext[2] HTTX_BUFFER !!
[ 728.945863] <-- RTMPAllocTxRxRingMemory, Status=3
[ 728.950392] ERROR!!! RTMPAllocDMAMemory failed, Status[=0x00000003]
[ 728.956567] !!! RT2870 Initialized fail !!!
ifconfig: SIOCSIFFLAGS: Operation not permitted

anyone knows ?
thanks

--~--~---------~--~----~------------~-------~--~----~
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: 

2. android ndk audio eac busy

hi,
i have ported my sip stack on android,  i am opening "/dev/eac" audio
in  ndk, i am getting
device busy error,
how do i open or use the "/dev/eac" audio in ndk ?
--~--~---------~--~----~------------~-------~--~----~

3. Determining keyboard vibrate setting

4. Cyanogen 4.2.1

5. repo: command not found

6. WebView clearHistory() problem

7. Cara logout account google kita di htc magic gmn?tolong master