Android GridLayout- cover whole screen programmatically -
i want grid layout cover whole screen , want programmatically. when in static xml adding app:layout_columnweight
attribute, looks want have follows:
but when tried create same layout programmatically, looks this:
i want same first image. here code:
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); android.support.v7.widget.gridlayout gridlayout = new android.support.v7.widget.gridlayout(this); gridlayout.setlayoutparams(new linearlayout.layoutparams(linearlayout.layoutparams.match_parent, linearlayout.layoutparams.wrap_content)); gridlayout.setcolumncount(3); (int = 0; < 9; i++) { button button = new button(mainactivity.this); gridlayout.layoutparams lp = (gridlayout.layoutparams) button.getlayoutparams(); if (lp == null) { lp = new gridlayout.layoutparams(); } lp.columnspec = gridlayout.spec(gridlayout.undefined, 1, 1f); button.setlayoutparams(lp); button.settext("button " + (i + 1)); gridlayout.addview(button); } linearlayout linearlayout = new linearlayout(this); linearlayout.addview(gridlayout); setcontentview(linearlayout); } }
please me. note- using min sdk 21.
update- here xml code
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.gridlayout android:layout_width="match_parent" android:layout_height="wrap_content" app:columncount="3"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_columnweight="1" app:layout_gravity="fill" android:text="button 1" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_columnweight="1" app:layout_gravity="fill" android:text="button 2" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_columnweight="1" app:layout_gravity="fill" android:text="button 3" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_columnweight="1" app:layout_gravity="fill" android:text="button 4" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_columnweight="1" app:layout_gravity="fill" android:text="button 5" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_columnweight="1" app:layout_gravity="fill" android:text="button 6" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_columnweight="1" app:layout_gravity="fill" android:text="button 7" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_columnweight="1" app:layout_gravity="fill" android:text="button 8" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_columnweight="1" app:layout_gravity="fill" android:text="button 9" /> </android.support.v7.widget.gridlayout> </linearlayout>
replace activity with:
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.your_layout_name); } }
Comments
Post a Comment