-1

my current android project requires the width of button should be set dynamically depending on the resolution available on device

i want to arrange 9 button in a row but the size will depend on the resolution available

for the current scene we are using below code

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    //int sheight = displaymetrics.heightPixels;
    int swidth = displaymetrics.widthPixels;
    int px=swidth/11;
    bW.setWidth(px);
    bE.setWidth(px);
    bR.setWidth(px);
    bT.setWidth(px);
    bY.setWidth(px);
    bU.setWidth(px);
    bI.setWidth(px);
    bO.setWidth(px);
    bP.setWidth(px);

somehow the width of button is not changed in any manner it remains the same

Squonk
  • 48,735
  • 19
  • 103
  • 135
khizar ansari
  • 1,476
  • 2
  • 18
  • 29

4 Answers4

0

If i consider you buttons to be images, that you have set using the Selector file, then you need to keep those files in drawable-hdpi, drawable-mdpi, and drawable-ldpi, and the images will accomodate itself according to the available resolution..

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • That's not actually true... dpi corresponds to (approximate) pixel density, not resolution. – kabuko Sep 04 '12 at 17:57
  • @but resolution depends on the density of these pixels in the screen – Kumar Vivek Mitra Sep 04 '12 at 17:59
  • Nope, not at all unless you're saying density in combination with screen (physical) size. You could have a 800x480 hdpi device or a 1920x1080 hdpi device. Completely different resolutions but the same dpi, one will just be physically larger than the other. – kabuko Sep 04 '12 at 19:02
  • @KumarVivekMitra do you think that this will arrange desired nine image in a row – khizar ansari Sep 05 '12 at 05:41
0

i think you should use android:layout_weight property.Its going to consume space depending on the width available.

set to all the buttons as android:layout_weight="1"

Try this url

Linear Layout and weight in Android

Community
  • 1
  • 1
moDev
  • 5,248
  • 4
  • 33
  • 63
  • if i do that then it will not arrange button in a row another row may be created – khizar ansari Sep 05 '12 at 05:37
  • 1
    why another row?? suppose you have 5 buttons , set android:weightSum="5" on layout and set android:layout_weight="1" on each button. It will automatically consume the necessary width. – moDev Sep 05 '12 at 12:01
0

Buttons and other objects that uses a ninepatch image won't get smaller than the ninepatch itself.

Make sure you are using an appropriate ninepatch image for you buttons that will fit the sizes you need.

BrainCrash
  • 12,992
  • 3
  • 32
  • 38
0

i came up with this solution

    ViewGroup.LayoutParams params = button.getLayoutParams();
    params.width=px;
    button.setLayoutParams(params);
khizar ansari
  • 1,476
  • 2
  • 18
  • 29