I want to use Grid Layout (not grid view) and in each cell i want image view and below it a text view . i want to implement click listener on each cell of grid layout.Please someone tell me how to do this?
Supposing, that your cell layout's top most wrapper is RelativeLayout, here is the code, that might work:
GridLayout grid = (GridLayout) findViewById(R.id.your_layout_id);
int childCount = grid.getChildCount();
for (int i= 0; i < childCount; i++){
RelativeLayout container = (RelativeLayout) grid.getChildAt(i);
container.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
// your click code here
}
});
}
I believe then it would be better to use CardView with grid layout, and within CardView you can put image and text and you can directly set onClickListener. e.g.
cardApplication.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,ApplicationList.class);
startActivity(intent);
}
});
Where cardApplication is the name of my CardView within the grid layout.
This is what the xml of my CardView looks like:
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
android:id="@+id/cardApplication"
android:clickable="true"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/ic_apps"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="Applications"
android:textAlignment="center"
android:textColor="#000000"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With