Pranay Gupta
Pranay Gupta

Reputation: 73

Disabling onClick for selected items in GridView Android

I would like to know how to disable click for a set of items in Gridview.

This is the class:

public class LevelSelectActivity extends Activity {

public GridView gridView;
public TextView Easy;
public TextView Medium;
public TextView Hard;
public Button mBackButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_level_select);

    Easy = (TextView) findViewById(R.id.EasyText);
    Easy.setTypeface(ApplicationServices.typeFaceDoubleBlast);
    Medium = (TextView) findViewById(R.id.MediumText);
    Medium.setTypeface(ApplicationServices.typeFaceDoubleBlast);
    Hard = (TextView) findViewById(R.id.HardText);
    Hard.setTypeface(ApplicationServices.typeFaceDoubleBlast);

    mBackButton = ( Button ) findViewById ( R.id. BackButton );
    mBackButton.setOnClickListener(mBack_Clicked);

    gridView = (GridView) findViewById(R.id.LevelSelectGrid);
    gridView.setAdapter(new ImageAdapter(this));
    gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            // Sending image id to FullScreenActivity
            Intent i = new Intent(getApplicationContext(), DoubleBlastGameActivity.class);
            // passing array index
            if(position >= 0 && position < 4)
            {
                i.putExtra("level", 1);
            }
            else if(position >= 4 && position < 8)
            {
                i.putExtra("level", 2);
            }
            else if(position >= 8 && position < 12)
            {
                i.putExtra("level", 3);
            }

            startActivity(i);
        }
    });
}

public View.OnClickListener mBack_Clicked = new View.OnClickListener() {

    public void onClick(View v) {

        Intent intend = new Intent(getBaseContext(), MainMenuActivity.class );
        startActivity(intend);
    }
};
public class ImageAdapter extends BaseAdapter {

    private Context context;
    public Integer[] mLevelIcon;
    public String[] mLevelId;

    public ImageAdapter(Context contextTemp) {
        this.context = contextTemp;
        mLevelId = new String[12];
        mLevelIcon = new Integer[12];
        for (int i = 0; i < mLevelId.length; i++) {
            mLevelId[i] = Integer.toString(i + 1);
            if(i + 1 <= ApplicationServices.gameConfigStructure.lastCompletedLevelInt )
            mLevelIcon[i] = R.drawable.levelbutton;
            else
            mLevelIcon[i] = R.drawable.lock;
        }

    }

    public int getCount() {
        // TODO Auto-generated method stub
        return mLevelId.length;
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

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

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {
            gridView = new View(context);

            gridView = inflater.inflate(R.layout.level_button, null);

            TextView textview = new TextView(context);
            textview = (TextView) gridView.findViewById(R.id.LevelID);
            textview.setText(mLevelId[position]);
            textview.setTypeface(ApplicationServices.typeFaceDoubleBlast);

            if(position >= ApplicationServices.gameConfigStructure.lastCompletedLevelInt )
            {
                textview.setVisibility(View.INVISIBLE);
            }

            LevelSelectButton button = new LevelSelectButton(context);
            button = (LevelSelectButton) gridView
                    .findViewById(R.id.levelSelectButton1);
            button.setImageResource(mLevelIcon[position]);

        } else {
            gridView = convertView;
        }
        return gridView;
    }

}
}

And this the screen shot for the Level Select Screen:

enter image description here

As you can see the first level is unlocked and ideally I would prefer only the unlocked levels to be click able. But currently it is allowing to enter any level.

How do I turn off the click for the locked levels?

Upvotes: 3

Views: 3107

Answers (2)

Pankaj Kumar
Pankaj Kumar

Reputation: 83028

You should modify getView() of ImageAdapter class. You need to add setEnabled(false) or View.setFocusable(false) and View.setClickable(false) to the item view, that is gridView in your case.

if (position >= ApplicationServices.gameConfigStructure.lastCompletedLevelInt) {
     textview.setVisibility(View.INVISIBLE);
     gridView.setEnabled(false); // Set enable false to these views
     // Or combination of below 
     // gridView.setFocusable(false);
     // gridView.setClickable(false);
}

Another way is to override isEnabled() of Adapter class, as user543 added in his answer, like

@Override
public boolean isEnabled(int position) {
   if (position >= ApplicationServices.gameConfigStructure.lastCompletedLevelInt) {
       return false;
   }
   return true;
}

Upvotes: 3

user543
user543

Reputation: 3633

You can override isEnabled(int position) in ImageAdapter, Like:

@Override
public boolean isEnabled(int position) {
   //check the position to be locked or not
   //if yes then return true, else return false
return true/false;
}

Upvotes: 2

Related Questions