roli
roli

Reputation: 301

Android GridView first button not working

I am having weird problems with Android GridView. I create a 3x4 grid and insert buttons into that grid. I want the background of the button to change when the user clicks that button. And this is working just fine for all buttons except the first one (the one with index 0 - top left). OnClick event listener doesn't fire at all for that button no matter what I do.

Here is the code where I create the view:

public View getView(int position, View convertView, ViewGroup parent) {
    Button imageView;

    if (convertView == null) {  // if it's not recycled, initialize some attributes
        Log.w("NOVO", "narejena nova celica");
        imageView = new Button(mContext);

        imageView.setPadding(8, 8, 8, 8);
    } else {
        Log.w("STARO", "stara celica");
        imageView = (Button) convertView;
    }

    imageView.setEnabled(true);

    int visina = parent.getHeight();
    int sirina = parent.getWidth();

    float dip = mContext.getResources().getDisplayMetrics().density;
    float margin = 10*dip;

    int view_height = (int)(visina - 3*margin)/4;
    int view_width = (int)(sirina - 2*margin)/3;

    int view_dim = 0;
    if (view_height <= view_width)
        view_dim = view_height;
    else
        view_dim = view_width;

    imageView.setLayoutParams(new GridView.LayoutParams(view_dim, view_dim));

    imageView.setId(position);
    imageView.setOnClickListener(celice.get(position));
    /*imageView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Toast toast = Toast.makeText(mContext, v.getId() + "bla", Toast.LENGTH_SHORT);
            //toast.show();
            celice.get(v.getId()).celicaVisible(4000);
        }});*/

    celice.get(position).id = position;
    celice.get(position).setButton(imageView);

    return imageView;
}

If I replace

imageView = (Button) convertView;

with

imageView = new Button(mContext);

then the onClick() gets fired but the background still doesn't change. All the other buttons are working as expected.

And here is the custom class "Celica" that takes care of the actual work - changing the background...

public class Celica implements OnClickListener {

public boolean odkrit;
public boolean najden;
public int id;
public Drawable slikca1, slikca2;
public Celica par;
private Timer tim;
public Button but;
public Context con;
static int buttonsVisible = 0;

Celica(Drawable s1, Drawable s2) {
    this.slikca1 = s1;
    this.slikca2 = s2;
}

void celicaVisible(int millis) {
    if (odkrit)
        return;

    Log.w("TEST", "prizganih " + buttonsVisible);
    if (buttonsVisible >= 2)
        return;

    odkrit = true;
    buttonsVisible++;
    tim = new Timer();
    tim.schedule(new timerDone(), millis);

    ((Activity)con).runOnUiThread(new Runnable() {
           @Override
           public void run() {
               but.setBackground(slikca2);
           }
    });

}

void setButton(Button b) {
    but = b;

    ((Activity)con).runOnUiThread(new Runnable() {
        @Override
        public void run() {
           but.setBackground(slikca1);
        }
 });
}

class timerDone extends TimerTask {

    @Override
    public void run() {
        if (!najden) {
            odkrit = false;

            ((Activity)con).runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       but.setBackground(slikca1);
                   }
            });
        }

        buttonsVisible--;
        tim.cancel();
    }

}

@Override
public void onClick(View v) {
    celicaVisible(4000);
}

}

Upvotes: 2

Views: 1051

Answers (3)

Wolfgang Schreurs
Wolfgang Schreurs

Reputation: 11834

In my situation (in Android, with MAUI) the first button is not clickable due to layout parameters being set in the GetView() method of the GridView adapter, while at that time the GridView itself still has a height and width of 0.

In order to resolve this, I assign the GridView adapter to the GridView only after OnSizeChanged() in my parent CoordinatorLayout has been called with a width and height greater than 0.

Upvotes: 0

roli
roli

Reputation: 301

I actually figured it out. Everything works if I use the view that gets provided by the onClick() method instead of saving the actual button at the creation of the Celica object.

So basically adding:

but = (Button) v;

to the onClick() method solved the problem.

Upvotes: 0

Gopal Gopi
Gopal Gopi

Reputation: 11131

In Android, ID of any View must be non zero and non negative number. means View ID should be > 0. and there is problem, when you are setting ID to the Button like

 imageView.setId(position)

here ID of a button will be zero when position is zero(means first item). may be due to this, First Button's OnClickListener is not getting fired...try setting a ID that is greater than zero to Button and try once...

you can write like

imageView.setId(position+1) to ensure ID > 0

Upvotes: 0

Related Questions