Reputation: 49
I'm able to show the ID of the item that gets clicked in a gridview, however what I want to achieve is that when I click on any of the images inside the Gridview it appears as an image, also, when I click on any of the images using the below code it just shows the ID, however when I try to display the image itself the app crashes and comes up with LinearLayout cannot be casted to ImageView
gifts_layout_2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_history_menu_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<ImageView
android:id="@+id/close"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="right"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:src="@drawable/close" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Smiles"
android:textColor="#000000"
android:textSize="17sp"
android:textStyle="bold"
android:layout_gravity="left"
android:layout_marginLeft="15dp"/>
<GridView
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="10dp"
android:numColumns="4"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center|center_horizontal|center_vertical"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:scrollbars="vertical"
android:scrollbarSize="3dp"
android:scrollIndicators="left"
android:id="@+id/grid_view2"
android:paddingLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_gravity="center|center_horizontal|center_vertical"
android:clipChildren="false"
android:clipToPadding="false"/>
</LinearLayout>
smiles_items_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="@+id/smile_image_view"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center"/>
</LinearLayout>
BottomSheetDialog_Smiles.java
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
final Holder holder = new Holder();
LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = inflater.inflate(R.layout.smile_items_layout, null);
lottieImage = inflater.inflate(R.layout.activity_streaming2, null);
holder.img = (ImageView) grid.findViewById(R.id.smile_image_view);
holder.img.setImageResource(mThumbIds[position]);
gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long
l) {
JSONDictionary imageChat = new JSONDictionary();
int imageId = (int) getItem(i);
imageChat.put("message",imageId);
Communicator.getInstance().emit("new chat message", imageChat);
}
});
return grid;
}
Upvotes: 0
Views: 489
Reputation: 2558
Try below code,OnItemClick Retrun view which you have click
gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
ImageView imageView = (ImageView)view;
//if you want to get image from imageview
imageView .getDrawable()
}
});
And If you want to get inputstream from the drawable use following:
BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
Bitmap bitmap = bitmapDrawable .getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
UPDated
You do it by 2 way
1st Way: Remove LinerLayout from xml and make ImageView Parent as LinerLayout only contain one view Like this, if you use this then no need to change java code
smiles_items_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/smile_image_view"
android:layout_width="45dp"
android:layout_height="45dp"
/>
2nd Way
if you want to keep your xml as it is then change the java code like below
LinearLayout linearLayout= (LinearLayout)view;
ImageView imageView = (ImageView) linearLayout.getChildAt(0);
//if you want to get image from imageview
imageView .getDrawable()
Upvotes: 1