-2

I need to set the images to grid view dynamically. but i'm getting exception: android.widget.LinearLayout cannot be cast to android.widget.ImageView I need to get the view of the grid without onclick. So please help me.

private void setTickMark(String icon_name_val) {
    ImageView imageView = (ImageView) rootView;
    //getting clicked image
    String iconimg = icon_name_val;
    String iconimgname = iconimg;
    // int idval = getResources().getIdentifier(iconimgname, "drawable",getActivity().getPackageName());
    // Drawable drawable = getResources().getDrawable(idval);
    // Log.d("dra",String.valueOf(drawable));
    int resID = getResources().getIdentifier(iconimgname, "drawable", getActivity().getPackageName());
    Bitmap bitmapbus = BitmapFactory.decodeResource(getResources(), R.drawable.bus);

    Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), resID);
    Bitmap resized = Bitmap.createScaledBitmap(bitmap1, bitmapbus.getWidth(), bitmapbus.getHeight(), true);

    Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.tick);
    // Bitmap resultBitmap = Bitmap.createBitmap(bitmap2.getWidth(), bitmap2.getHeight(), Bitmap.Config.ARGB_8888);
    Bitmap resultBitmap = Bitmap.createBitmap(bitmapbus.getWidth(), bitmapbus.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(resultBitmap);
    c.drawBitmap(resized, 0, 0, null);
    c.drawBitmap(bitmap2, 0, 0, null);

    imageView.setImageBitmap(resultBitmap);
}



<LinearLayout
    android:layout_width="fill_parent"
    android:id="@+id/popup"
    android:layout_height="0dp"
    android:layout_weight=".90"
    android:orientation="vertical"
    android:layout_margin="20dp"
    android:layout_marginTop="20dp">



    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:columnWidth="60dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="20dp"
        android:horizontalSpacing="20dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        />
    <LinearLayout
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_marginTop="4dp"
        android:duplicateParentState="true"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add"
            android:id="@+id/save"

            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel"
            android:id="@+id/cancel"/>
    </LinearLayout>
</LinearLayout>
gStephin
  • 332
  • 3
  • 20

1 Answers1

2

Add an imageview to your gridview

then only define ImageView imageView = (ImageView) rootView.findViewById(R.id.ImageView1);

yong.k
  • 678
  • 1
  • 8
  • 22