Add android:descendantFocusability="blocksDescendants"
attribute in parent layout of row_grid.xml
using setOnItemClickListener and new intent not working
-
17-07-2023 - |
Question
I have a grid view with images in it. Once one is clicked it should go to PlayVideoActivity
but onItemClick
never get called.
I followed in the debugger and tried clicking the image but it never fired onItemClick
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videos);
gridView = (GridView) findViewById(R.id.gridView);
customGridAdapter = new GridViewAdapter(this, R.layout.row_grid,
getData());
gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
Toast.makeText(videoChannelActivity.this, i + "#Selected",
Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(videoChannelActivity.this, PlayVideoActivity.class);
startActivity(myIntent);
}
});
}
row_grid.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:background="@drawable/grid_color_selector"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="@+id/image"
android:layout_width="280dp"
android:layout_height="158dp"
android:contentDescription="@string/desc"
android:scaleType="centerInside" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="12sp" >
</TextView>
Solution
OTHER TIPS
You should remove
android:clickable="true"
android:focusable="true"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow