Change the width of a button in java code? (in dip)

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Change the width of a button in java code? (in dip)

Postby twankg » Thu Jun 04, 2009 2:34 pm

Good morning,

How can i change the width of a button in java code so that its width is 100dip.

It is simple to change width in pixels: but.setWidth(100); //sets it to 100 pixels.

LayoutParams lp=new LayoutParams(R.dimen.butwidth, LayoutParams.WRAP_CONTENT);
but.setLayoutParams(lp);
//R.dimen.butwidth is a dimension 100dip

But that doesn't have any effect.

I know this is easy in layout xml, but i have a few java generated buttons that need to be sized to 100 dip.

Kind regards,
Twan
twankg
Freshman
Freshman
 
Posts: 2
Joined: Tue May 26, 2009 7:56 pm

Top

Postby keylimepai » Thu Jun 04, 2009 8:57 pm

Try:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. {
  2.  
  3.    DisplayMetrics dm = new DisplayMetrics();
  4.  
  5.    getWindowManager().getDefaultDisplay().getMetrics(dm);
  6.  
  7.    but.setWidth((int)(100 * dm.density));
  8.  
  9. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


(I haven't tried it, but looking at the API, it looks like this might be what you want... )
keylimepai
Junior Developer
Junior Developer
 
Posts: 14
Joined: Wed May 27, 2009 7:14 pm

Postby twankg » Fri Jun 05, 2009 9:12 am

Thanks keylimepai!
That sure did the trick.

Image

The cancel button comes from xml and the login and register buttons are generated by java code. They look the same to me :P
twankg
Freshman
Freshman
 
Posts: 2
Joined: Tue May 26, 2009 7:56 pm

Postby konic » Tue Sep 29, 2009 4:03 pm

Does Android have API (or something) to convert dip(sp) to pixels and/or back?
konic
Developer
Developer
 
Posts: 32
Joined: Mon Aug 24, 2009 3:16 pm

difference between dip and px

Postby saamyandroid » Mon Jan 04, 2010 6:34 am

Actually
What is the difference between dip and px
saamyandroid
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Sep 09, 2009 8:17 pm
Location: INDIA

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 3 guests