Q:

android gridview not showing top row

I am using a gridview inside a relativelayout and im trying to make the whole grid show up on the screen.  The top row is not completely showing..I am only getting the top right button to show.  The grid is 4 columns and 6 rows.  All of the rows show up besides the one at the top of the screen.  My code is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent" >

<GridView
    android:id="@+id/grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:columnWidth="20dp"
    android:horizontalSpacing="1dp"
    android:numColumns="4"
    android:verticalSpacing="1dp" >
</GridView>

and my main.java is:
public class MainActivity extends Activity  implements OnClickListener 
{
private long startTime = 0L;
private Handler customHandler = new Handler();
private int game_running = 0;
private int button_clicks = 0;
private int previous_button_id = 0;
private int current_button_id = 0;
private CharSequence button_value = null;
private CharSequence prevbutton_value = null;
private int j = 0;
private int isgameover = 0;
boolean flag = false;
boolean reset = false;
long gametime = 0;
long resettime = 0;
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
boolean resetbool = false;
private ArrayList<Button> mButtons = new ArrayList<Button>();
private ArrayList<Button> comp_buttons = new ArrayList<Button>();
private int[] numbers =
{
        1,
        1,
        2,
        2,
        3,
        3,
        4,
        4,
        5,
        5,
        6,
        6,
        7,
        7,
        8,
        8,
        9,
        9,
};
Animation fadeout;

protected void onSaveInstanceState(Bundle savedInstanceState) 
{
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putBoolean("Reset", true);;
}

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    shuffle(numbers);
    fadeout = AnimationUtils.loadAnimation(this,R.anim.fadeout);
    Button cb = null;
    for (int i=0; i<24; i++) 
    {
        cb = new Button(this);
        cb.setId(i);
        cb.setOnClickListener(this);
        cb.setText(Integer.toString(numbers[j]));
        cb.setTextSize(0);
        cb.setBackgroundResource(R.drawable.button);
        cb.setHeight(100);
        j++;
        registerForContextMenu(cb);
        mButtons.add(cb);
        if(j >= 18)
        {
            j = 0;
        }
    }
    GridView gridView = (GridView) findViewById(R.id.grid);
    gridView.setAdapter(new CustomAdapter(mButtons));
}

  static void shuffle(int[] ar)
  {
    Random rnd = new Random();
    for (int i = ar.length - 1; i > 0; i--)
    {
      int index = rnd.nextInt(i + 1);
      // Simple swap
      int a = ar[index];
      ar[index] = ar[i];
      ar[i] = a;
    }
  }

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

}
and my adapter class:
public class CustomAdapter extends BaseAdapter {
private ArrayList<Button> mButtons = null;

public CustomAdapter(ArrayList<Button> b)
{
    mButtons = b;
}

@Override
public int getCount() 
{
    return mButtons.size();
}

@Override
public Object getItem(int position) 
{
    return (Object) mButtons.get(position);
}
@Override
public long getItemId(int position) 
{
    //in our case position and id are synonymous
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    Button button;
    if (convertView == null) 
    {
        button = mButtons.get(position);
    } 
    else 
    {
        button = (Button) convertView;
    }
    if( (position == 0) || (position == 1) || (position == 2) )
    {
        button.setVisibility(convertView.GONE);
    }
return button;
}

}
The idea here is to have the grid eventually scroll vertically (add more buttons with for loop) and I want it to scroll when some of the buttons in the grid disappear.  I wanted to be able to draw the grid with buttons correctly first before moving on.  Thanks for the help.

A:

You will need a custom GridView or use smoothScrollToPosition to show the top row.
As you add elements to the GridView, it will scroll. If your buttons are too large for the available space, it will scroll down. Likewise, if you specify the height of the GridView it will truncate the buttons.
So, you need to measure the available space on the screen, then set your button heights accordingly. Or you can scroll to the top after building it and allow it to scroll vertically, which sounds like what you want anyway.
Fundamentally, a GridView scrolls, so loading it with more elements than can fit on the screen initially should result in what you are seeing. Reset the anchor position or resize your elements are your only options.

