Reputation: 1001
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 look at code, maybe i do something wrong
Here is a xml file:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/pick_currency"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:clickable="true"
/>
Here i configuring the gridView in activity, of course i call this method in onCreate:
private void configureGridViewCurrencySelect() {
gridViewCurrency.setAdapter(new ButtonAdapter(this));
gridViewCurrency.setNumColumns(4);
gridViewCurrency.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(), "klick " + position,
Toast.LENGTH_SHORT).show();
}
});
}
And here is my ButtonAdapter:
public class ButtonAdapter extends BaseAdapter {
private Context mContext;
public ButtonAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
Button buttonView;
if (convertView == null) {
buttonView = new Button(mContext);
buttonView.setLayoutParams(new GridView.LayoutParams(220, 160));
buttonView.setPadding(8, 20, 8, 8);
} else {
buttonView = (Button) convertView;
}
buttonView.setText(mThumbIds[position]);
return buttonView;
}
private String[] mThumbIds = {"EUR/USD", "EUR/USD", "EUR/USD", "EUR/USD",
"EUR/USD", "EUR/USD", "EUR/USD", "EUR/USD",};
}
Upvotes: 0
Views: 94
Reputation: 1943
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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</LinearLayout>
This is you activity
public class MainActivity extends AppCompatActivity {
private final String TAG = this.getClass().getSimpleName();
private GridView gridViewCurrency;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridViewCurrency = (GridView) findViewById(R.id.gridview);
configureGridViewCurrencySelect();
}
private void configureGridViewCurrencySelect() {
gridViewCurrency.setAdapter(new ButtonAdapter(this));
gridViewCurrency.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(), "klick " + position,
Toast.LENGTH_SHORT).show();
}
});
}
public class ButtonAdapter extends BaseAdapter {
private Context mContext;
public ButtonAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = null;
if (convertView == null) {
holder = new Holder();
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;
}
public class Holder {
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"?>
<RelativeLayout android: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"
>
<TextView
android: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"/>
<GridView
android: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>
Upvotes: 3
Reputation: 2866
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 = new Button(mContext);
buttonView.setLayoutParams(new GridView.LayoutParams(220, 160));
buttonView.setPadding(8, 20, 8, 8);
buttonView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(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
Upvotes: 0
Reputation: 2135
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"
Upvotes: 0