user2152573
user2152573

Reputation: 23

Place programmatically imagebuttons of an array on a view

I have a question about an imagebutton array. I want to place programmatically each imagebutton of the array on a different position by changing marginTop and marginLeft in a RelativeLayout. Here is my code, but it only shows all imagebuttons on the same position, like they had no margin:

DatabaseHandler db;
db= new DatabaseHandler(this);
int databasereadercount=db.getMemoryCount();

ImageButton[] button= new ImageButton[60];
setContentView(R.layout.playsolo);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.playsololayout);
for(int i=0;i<60;i++){
if(i<=databasereadercount){
    button[i]=new ImageButton(this);
    FrameLayout.LayoutParams[] params = new FrameLayout.LayoutParams[60];
    params[i]=new FrameLayout.LayoutParams(100,100);
    params[i].setMargins(10*i, 5*i, 0, 0);       //for example
    Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.raster);
    bitmap=Bitmap.createScaledBitmap(bitmap, 480, 320, true);
    Drawable drawable = new BitmapDrawable(getResources(),bitmap);
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] {android.R.attr.state_pressed},drawable);
    states.addState(new int[] {android.R.attr.state_focused},drawable);
    states.addState(new int[] { },drawable);
    button[i].setImageDrawable(states);
    button[i].setId(i);
    button[i].setOnClickListener(new View.OnClickListener(){
    public void onClick(View v) {
            // TODO Auto-generated method stub
        startlevel(v.getId());
        }
    });
    layout.addView(button[i],params[i]);

    }
}

Upvotes: 2

Views: 178

Answers (1)

Gerrit-K
Gerrit-K

Reputation: 1308

You're using FrameLayout.LayoutParams while your layout is a RelativeLayout. If I'm not totally mistaken you should use RelativeLayout.LayoutParams instead.

Upvotes: 2

Related Questions