0

I am having major issues getting my program to properly display 4 buttons, side by side, with the same width. I have tried a bunch of combinations, and spent over an hour on StackOverflow searching solutions, with no luck on any of them. How can I go about making these four buttons all with the same height in the same row on a vertical interface?

This is what I have so far with no luck. Either buttons too large, too small, or are hidden since width 0.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        layout.setWeightSum(1);

        Button redButton = new Button(this);
        redButton.setText("Red");
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                0,
                LayoutParams.WRAP_CONTENT, 
                0.25f);
        redButton.setWidth(0);
        redButton.setLayoutParams(p);
        layout.addView(redButton);

        Button greenButton = new Button(this);
        greenButton.setText("Green");
        greenButton.setLayoutParams(p);
        greenButton.setWidth(0);
        layout.addView(greenButton);

        Button blueButton = new Button(this);
        blueButton.setText("Blue");
        blueButton.setLayoutParams(p);
        blueButton.setWidth(0);
        layout.addView(blueButton);

        Button yellowButton = new Button(this);
        yellowButton.setText("Yellow");
        yellowButton.setLayoutParams(p);
        yellowButton.setWidth(0);
        layout.addView(yellowButton);

        setContentView(layout);
    }
  • 2
    why don't you use an xml for this ? – Srikanth Pai Apr 13 '13 at 02:07
  • Trying to do something different. I already know how to do it with XML, but I want to play around with programmatically layouts for an app I am working on which will require some dynamic layout stuff. – NadeMaster Apr 13 '13 at 02:12

1 Answers1

1

What about getting the resolution for the devices screen using something like this:

Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int screen_width = d.getWidth();
int screen_height = d.getHeight();

and then say if you want 4 buttons each with same width accross the screen calling from your example greenButton.setWidth(screen_width/4);

and do this for each of your buttons... same width spreading over a resolution of any size screen all dynamically done!

brendosthoughts
  • 1,683
  • 5
  • 21
  • 38
  • just a reference the getting display width and height was taken from this SO thread http://stackoverflow.com/questions/2902640/android-get-the-screen-resolution-pixels-as-integer-values (so I dont know if it actually works) – brendosthoughts Apr 13 '13 at 02:23
  • It's a deprecated method, doesn't this mean it will be removed in future versions? – NadeMaster Apr 13 '13 at 02:27
  • yes it does mean that, check out android dev pages i'm sure they've implemented a new way to do same functionally (this is just an example of how to do it) ... get the width of display set each button to 1/4th of the width – brendosthoughts Apr 13 '13 at 02:33