Skip to content Skip to sidebar Skip to footer

Gridview Not React For Onclicklistener

going with googleDevelopers example i create gridView with buttons, Everythink working, but when i create TOAST to show me the clicked position im no get any message. Can u guys lo

Solution 1:

You are using clickable(Button) item as row item in GridView Adapter, that is the reason your are not getting onItemClick called.

set your button clickable false and then try for the GridView onItemClick

This is common issue for ListView and GridView, if you have used any clickable control as a row item then your onItemClick will not be performed.

OR

Apply below attribute in your root layout in which GridView/ListView declared in layout xml

android:descendantFocusability="blocksDescendants"

Also apply below to your clickable control in row xml if any

android:focusable="false"android:focusableInTouchMode="false"

OR

In your case you are adding button programatically into GridView adapter so, button is covering the whole area of a cell of GridView and because of this you will not have your problem resolved by above two solutions, you need to update your code as shown below.

Create new layout xml for GridView row

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="40dp"android:focusable="false"android:focusableInTouchMode="false"
        /></LinearLayout>

This is you activity

publicclassMainActivityextendsAppCompatActivity {

    privatefinalStringTAG=this.getClass().getSimpleName();
    private GridView gridViewCurrency;

    @OverrideprotectedvoidonCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gridViewCurrency = (GridView) findViewById(R.id.gridview);

        configureGridViewCurrencySelect();
    }

    privatevoidconfigureGridViewCurrencySelect() {
        gridViewCurrency.setAdapter(newButtonAdapter(this));

        gridViewCurrency.setOnItemClickListener(newAdapterView.OnItemClickListener() {
            publicvoidonItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                Toast.makeText(getApplicationContext(), "klick " + position,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    publicclassButtonAdapterextendsBaseAdapter {
        private Context mContext;

        publicButtonAdapter(Context c) {
            mContext = c;
        }

        publicintgetCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            returnnull;
        }

        publiclonggetItemId(int position) {
            return0;
        }

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

            Holderholder=null;


            if (convertView == null) {
                holder = newHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.row_button, null);
                holder.button = (Button) convertView.findViewById(R.id.button);

                convertView.setTag(holder);

            } else {
                holder = (Holder) convertView.getTag();
            }

            holder.button.setText(mThumbIds[position]);
            return convertView;
        }

        publicclassHolder {
            private Button button;

        }

        private String[] mThumbIds = {"EUR/USD", "EUR/USD", "EUR/USD", "EUR/USD",
                "EUR/USD", "EUR/USD", "EUR/USD", "EUR/USD",};
    }
}

This is your layout xml having GridView declared

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutandroid:id="@+id/activity_currency_select"xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:descendantFocusability="blocksDescendants"
    ><TextViewandroid:id="@+id/pick_currency"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="30dp"android:text="Pick Currency"android:textColor="@android:color/black"android:textSize="40dp"/><GridViewandroid:id="@+id/gridview"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@+id/pick_currency"android:layout_marginTop="50dp"android:background="@color/colorAccent"android:columnWidth="90dp"android:gravity="center"android:horizontalSpacing="10dp"android:numColumns="2"android:stretchMode="columnWidth"android:verticalSpacing="10dp"/></RelativeLayout>

Solution 2:

I think you should only remove this attribute from your GridView:

android:clickable="true"

and (even if it's not necessary, only if you want a single choice behavior) add this:

android:choiceMode="singleChoice"

Solution 3:

Instead of having ClickListener on gridView, you can remove android:clickable="true" on GridView and apply OnClickListener directly in your adapter.

if (convertView == null) {
     buttonView = newButton(mContext);
     buttonView.setLayoutParams(newGridView.LayoutParams(220, 160));
     buttonView.setPadding(8, 20, 8, 8);
     buttonView.setOnClickListener(newView.OnClickListener() {
         @OverridepublicvoidonClick(View view) {
             Toast.makeText(mContext, "klick " + position,
                    Toast.LENGTH_SHORT).show();
         }
      });)
} else {
    buttonView = (Button) convertView;
}

To get the ripple effect, you need to set the background of the button with R.attr.selectableItemBackground. You can refer to this answer

Post a Comment for "Gridview Not React For Onclicklistener"