2

I am trying to display a grid of buttons using gridLayout at runtime. I am trying to fit the gridLayout to fit the entire screen's width. I am able to do that for 3x3 and 5x5 button grid (using my nexus 4). But when I go for 7x7, I lose the last column of buttons i.e the last column of GridLayout. I seriously cant understand whats going wrong here.

Here's what I am doing.. I take the grid size from settings activity and this is the number of columns in GridLayout (numColumns). I use LayoutParams and get the width and height and set the buttons' width and height to (layoutWidth/numColumns) and (layoutHeight/numColumns). Please help me here guyz.. I have pasted the code below.

public class StartGameActivity extends ActionBarActivity {

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start_game);




    SharedPreferences sharedPref= PreferenceManager.getDefaultSharedPreferences(this);
    int numColumns=Integer.parseInt(sharedPref.getString("Grid", "3").toString());
    // create a RelativeLayout
    RelativeLayout relativeLayout = new RelativeLayout(this);
    //LinearLayout linearLayout = new LinearLayout(this);

    // define the RelativeLayout layout parameters.
    //LinearLayout.LayoutParams linearLayoutparams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
    DisplayMetrics metrics=new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int width=metrics.widthPixels;
    int height=metrics.heightPixels;
    RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(width,height);
    RelativeLayout.LayoutParams relativeLayoutParams1 = new RelativeLayout.LayoutParams(width,height);


    GridLayout gridLayout=new GridLayout(this);
    GridLayout.LayoutParams gridLayoutParams = new GridLayout.LayoutParams();   
    gridLayoutParams.setGravity(Gravity.FILL_HORIZONTAL);


    gridLayout.setColumnCount(numColumns);
    gridLayout.setRowCount(numColumns);
    gridLayout.setMinimumHeight(height);
    gridLayout.setMinimumWidth(width);

    for(int i=0;i<numColumns*numColumns;i++)
    {


        Button button=new Button(this);
        button.setWidth(width/numColumns);
        button.setHeight(width/numColumns);
        button.setMinWidth(width/numColumns);
        button.setMinHeight(width/numColumns);
        button.setGravity(Gravity.FILL);
        //buttonForEveryRow++;
        //columIndex++;
        gridLayout.addView(button);
    }


    relativeLayout.addView(gridLayout,relativeLayoutParams1);
    setContentView(relativeLayout,relativeLayoutParams);



}

}

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3371005
  • 55
  • 1
  • 5
  • You have not considered the padding/spacing being added by Android by default in your width calculation. See http://stackoverflow.com/questions/21455495/gridlayoutnot-gridview-spaces-between-the-cells. I tried it but didn't work for me. – faizal May 24 '14 at 10:11
  • 1
    Hey @faizal.. Thanks for your reply..It doesnt work for me too..Its still showing the same – user3371005 May 24 '14 at 15:10
  • Inside your for loop, add the following lines at the end(after the addView line) : ViewGroup.LayoutParams par = button.getLayoutParams(); par.width=width/numColumns; par.height=width/numColumns; button.setLayoutParams(par); – faizal May 24 '14 at 17:13

1 Answers1

0

You should set the parameters of the button only after it has been added to it's parent layout.

So inside your FOR loop, remove all the button.set.. lines and add the following lines at the end(after the gridLayout.addView line) :

ViewGroup.LayoutParams par = button.getLayoutParams(); 
par.width=width/numColumns; 
par.height=width/numColumns; 
button.setLayoutParams(par);

http://developer.android.com/reference/android/view/View.html#getLayoutParams%28%29 :

All views should have layout parameters. These supply parameters to the parent of this view specifying how it should be arranged.

faizal
  • 3,497
  • 7
  • 37
  • 62