1

Full Activity Class where the I want to display the Image in full Screen. I have all the images showing it in the gridView. I am not facing problem in that, however when I click on the image of the gridview it shows me the just the xml file which I have called. I guess I am doing wrong somewhere calling the id here in the FullImageAcitivity file.

public class FullImageActivity extends Activity {

Button download, setas;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);
    setas = (Button) findViewById(R.id.setas);
    download = (Button)findViewById(R.id.download);
    final Intent i = getIntent();
    final   List<Item> items = new ArrayList<Item>();
    final ImageAdapter imageAdapter = new ImageAdapter(this);       
    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
    imageView.setImageResource(items.indexOf(i));
    setas.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             WallpaperManager myWallpaperManager
             = WallpaperManager.getInstance(getApplicationContext());
            try {
                myWallpaperManager.setResource(imageAdapter.items.indexOf(i));
            } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
    }
    });
  }
}

here is my imageAdapter file.

public class ImageAdapter extends BaseAdapter {
List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;

public ImageAdapter(Context context) {
    inflater = LayoutInflater.from(context);

    items.add(new Item("One",       R.drawable.abstact_one));
    items.add(new Item("Two",   R.drawable.abstract_three));
    items.add(new Item("Three", R.drawable.image_two));
    items.add(new Item("Four",      R.drawable.image_four));
    items.add(new Item("Five",     R.drawable.image_five));
    items.add(new Item("Six",      R.drawable.image_nine));
    items.add(new Item("Seven",       R.drawable.image_ten));
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int i) {
    return items.get(i);
}

@Override
public long getItemId(int i) {
    return items.get(i).drawableId;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    View v = view;
    ImageView picture;
    TextView name;

    if(v == null) {
        v = inflater.inflate(R.layout.other, viewGroup, false);
        v.setTag(R.id.picture, v.findViewById(R.id.picture));
        v.setTag(R.id.text, v.findViewById(R.id.text));
    }

    picture = (ImageView)v.getTag(R.id.picture);
    name = (TextView)v.getTag(R.id.text);

    Item item = (Item)getItem(i);

    picture.setImageResource(item.drawableId);
    name.setText(item.name);

    return v;
}

private class Item {
    final String name;
    final int drawableId;

    Item(String name, int drawableId) {
        this.name = name;
        this.drawableId = drawableId;
    }
 }
}

latestTab Activity File

public class LatestTab extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.latestphotos);

      GridView gridView = (GridView) findViewById(R.id.grid_view);

        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter(this));

        /**
         * On Click event for Single Gridview Item
         * */
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
            }
        });

}
}

full_image.xml

<?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" >

<RelativeLayout
    android:id="@+id/relshare"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="#79B8B8B8" >

    <Button
        android:id="@+id/setas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="14dp"
        android:layout_toRightOf="@+id/share"
        android:gravity="right"
        android:text="Set As"
        android:textSize="25sp" />

    <Button
        android:id="@+id/download"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="34dp"
        android:text="Download"
        android:textSize="25sp" />

</RelativeLayout>

<ImageView android:id="@+id/full_image_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>

latestphotos.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridView
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="0dp"
    android:stretchMode="columnWidth"
    android:numColumns="2" 
    />
</FrameLayout>
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
Deep Rathod
  • 69
  • 3
  • 14

2 Answers2

0

In your LatestTab

Declare the list as a class member

  List<Item> items = new ArrayList<Item>();

Add the items to the list

items.add(new Item("One", R.drawable.abstact_one));
items.add(new Item("Two",  R.drawable.abstract_three));
items.add(new Item("Three", R.drawable.image_two));
items.add(new Item("Four", R.drawable.image_four));
items.add(new Item("Five", R.drawable.image_five));
items.add(new Item("Six",  R.drawable.image_nine));
items.add(new Item("Seven", R.drawable.image_ten));

Your Item class

public class Item {
String name;
int drawable;
public int getDrawable() {
    return drawable;
}
public void setDrawable(int drawable) {
    this.drawable = drawable;
}
public Item(String name, int id)
{
    this.name= name;
    this.drawable = id;
}
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

Pass the list to the constructor of adapter class

 gridView.setAdapter(new ImageAdapter(this,items));

In the constructor

  List<Item> items;
  public ImageAdapter(Context context,List<Item> items) {
   inflater = LayoutInflater.from(context);
   this.items = items;
 } 

then in getVIew

 Item item = (Item)items.get(i);
 picture.setImageResource(item.getDrawable());

Then in item click listener

  Item item = items.get(position);
  int  id = item.getDrawable(); 
  Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
  i.putExtra("idkey", id); // pass the id
  startActivity(i);

Then in FullImageActivity

  int id = getIntent().getIntExtra("idkey"); //get id
  imageview.setImageResource(id); // set the drawable to imageview
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • First of all thank you very much for the reply. If you can make the necessary changes in the file and can send me ? will full code. Sorry to ask you beginner here..!! deeprathod2@gmail.com is my mail id. – Deep Rathod Oct 23 '13 at 20:50
  • @DeepRathod just make the changes suggested in my post and let me know if it works. It is not good that you ask fro the full code coz you won't learn. Pls try the suggestion if it does not work or you get stuck let me know – Raghunandan Oct 24 '13 at 04:11
  • Thanks again for replying..!! i Tried to replace the code and made the changes accordingly..!! Its showing error in my "LatestTab.java" file. – Deep Rathod Oct 24 '13 at 05:05
  • @DeepRathod update your post with the changes made and specify what is the error. will look at it – Raghunandan Oct 24 '13 at 05:06
  • @DeepRathod adding of items to list should be done in LatestTab. then you pass the list to the adapter class and set the data based on position in getView. in on item click you get the data based on position pass the same to fullscreenssctivity. there you retrieve the image and set it to imageview – Raghunandan Oct 24 '13 at 05:12
  • Thanks a lot..!!Error Solved..!! Thank you again..!! It worked..!! – Deep Rathod Oct 24 '13 at 10:21
  • @Raghunandan hi I am creating gridview for images. When I set images in gridview memory error. Is it possible to show small size images in gridview. And on click show same full image with higher dimensions? – John R Feb 25 '14 at 09:29
  • @JohnR yes its possible. gridview is not memory its a view – Raghunandan Feb 25 '14 at 09:30
  • HOw to do that? I am trying androidhive example. But its giving memory error. – John R Feb 25 '14 at 09:35
  • @JohnR scale down the image and it will fine. How to do that google search or Look for Lazy List – Raghunandan Feb 25 '14 at 09:36
  • @Raghunandan And you solved my android support library problem If you have time please see http://stackoverflow.com/questions/21795645/exception-outofmemoryerror – John R Feb 25 '14 at 09:36
  • And gridview example- http://stackoverflow.com/questions/21953057/gridview-example-not-working – John R Feb 25 '14 at 09:37
0

Its only showing the layout in your FullImageActivity because you have passed the value in Intent but you are not actually getting it in FullImageActivity.

In your activity get the value as below:

  final int i = getIntent().getIntExtra("id");

And in your ArrayList You have not added any values that is why its not able to get the i value from your item List. Just add the values in your items as below:

      // Selected image id
final   List<Item> items = new ArrayList<Item>();
items.add(new Item("One",       R.drawable.abstact_one));
items.add(new Item("Two",   R.drawable.abstract_three));
items.add(new Item("Three", R.drawable.image_two));
items.add(new Item("Four",      R.drawable.image_four));
items.add(new Item("Five",     R.drawable.image_five));
items.add(new Item("Six",      R.drawable.image_nine));
items.add(new Item("Seven",       R.drawable.image_ten));

After that set the image in ImageView

ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(items.indexOf(i));
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • i believe `imageView.setImageResource(items.indexOf(i))` will not work – Raghunandan Oct 23 '13 at 13:36
  • @GrlsHu, First of all thank you very much for the reply. I made the changes as you mentioned, however it still the same no change in it. I mean its still showing on the xml file without the image in it. If you can make the necessary changes in the file and can send me ? will full code. Sorry to ask you beginner here..!! deeprathod2@gmail.com is my mail id. – Deep Rathod Oct 23 '13 at 20:13
  • Do post your layout files. Will check problem and post the code. – GrIsHu Oct 24 '13 at 04:55