0

I'm new to the android world and this problem has been giving me headaches. I have a gridview with 4 buttons and I'd like to split the screen into 4 equal height and width buttons. (Without assigning custom dp) . I Tried to use layout_weight but it didn't work for me :/ Here is some example of my code. This is the class where I'm using the gridview

public class DietAdminPanel extends Activity {

private Button mydiets_button;
private ArrayList<String> data;
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.gridview_layout);
    data = new ArrayList<String>();
    data.add("Test1");
    data.add("Test2");
    data.add("Test3");
    data.add("Test4");
    Log.d("On create", "success");
    GridView gridview = (GridView)findViewById(R.id.gridview);
    gridview.setAdapter(new GridViewAdapter(this, data));

gridview_layout.xml

 <?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="30dp"
    android:numColumns="2"
     android:verticalSpacing="0dp"
     android:horizontalSpacing="0dp"
     android:stretchMode="columnWidth"
     android:fillViewport="true"

     >

</GridView>

and gridview_item_layout.xml ( for the custom adapter)

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView android:id="@+id/gridview_textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="test"/>
</LinearLayout>

Here is how it actually looks like

https://i.stack.imgur.com/Iah2y.jpg

This is how i want my gridview to look like ( I used width in dp for this, and thats not what i want :/ )

https://i.stack.imgur.com/xHgkj.jpg

If you know something please tell me ;)

user2430929
  • 113
  • 5
  • 15

2 Answers2

1

GridView scales horizontally only, meaning it will create as many columns as you defined with the same size only horizontally.

The height of each cell will depend on the View inside it

momo
  • 3,404
  • 6
  • 37
  • 66
-1

GridView is for displaying a number of items that can't all be displayed at same time, and hence they are scrolled into view. Still you can use android:verticalSpacing="50dp" to set vertical space between items.

But if you have a small fixed number of items which can all be displayed on screen at same time, you may want to use TableLayout or GridLayout instead.

S.D.
  • 29,290
  • 3
  • 79
  • 130
  • Thanks for your comment, I'll look around those layout,though, I can't use Gridlayout because I'm using 2.3.3 API :/. I don't own a 4.X android phone – user2430929 Aug 07 '13 at 01:32