Skip to content

Instantly share code, notes, and snippets.

@ochim
Last active April 17, 2019 10:12
Show Gist options
  • Select an option

  • Save ochim/8a8791bf7d4779db3be18949659bc3df to your computer and use it in GitHub Desktop.

Select an option

Save ochim/8a8791bf7d4779db3be18949659bc3df to your computer and use it in GitHub Desktop.
[Android]端末のスクリーンサイズを取得して、レイアウトやViewに割り当て

How to programmatically set the width of the LinearLayout?

Display display = getActivity.getWindowManager().getDefaultDisplay();
Point size = new Point();
try {
    display.getRealSize(size);
} catch (NoSuchMethodError err) {
    display.getSize(size);
}
int width = size.x;
int height = size.y;

//幅の1/3を割り当て
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)(width/3),
                    LinearLayout.LayoutParams.WRAP_CONTENT); // or set height to any fixed value you want

your_layout.setLayoutParams(lp);
// OR
your_textView.setLayoutParams(lp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment