0

I've made a simple app in which I am using 3 tabs. Each tab is having separate classes and layouts. First tab shows map. Second tab will show the camera from which the user can take picture(s) and third tab is off Pictures in which the captured images are viewed in the GridView. All tabs are working good. Now I want is to show full ImageView when a user tap on any image.

For all of this I am using this tutorial. So I followed each and every step in it and the things are going good. But for full ImageView the code is not working.

Below is my picture Fragment

Pictures.java

public class Pictures extends Fragment implements MainActivity.Updateable {

private Utils utils;
private ArrayList<String> imagePaths = new ArrayList<String>();
private GridViewImageAdapter adapter;
private GridView gridView;
private int columnWidth;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.pictures, container, false);

    gridView = (GridView) rootView.findViewById(R.id.grid_view);
    utils = new Utils(getContext());

    // Initilizing Grid View
    InitilizeGridLayout();

    // loading all image paths from SD card
    imagePaths = utils.getFilePaths();

    // Gridview adapter

    adapter = new GridViewImageAdapter(getActivity(), imagePaths, columnWidth);
    /*adapter = new GridViewImageAdapter(Pictures.this, imagePaths,
            columnWidth);*/

    // setting grid view adapter
    gridView.setAdapter(adapter);

    //((BaseAdapter) adapter).notifyDataSetChanged();
    //adapter.notifyDataSetChanged();


    return rootView;
}

private void InitilizeGridLayout() {

    Resources r = getResources();
    float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            AppConstant.GRID_PADDING, r.getDisplayMetrics());

    columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);

    gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
    gridView.setColumnWidth(columnWidth);
    gridView.setStretchMode(GridView.NO_STRETCH);
    gridView.setPadding((int) padding, (int) padding, (int) padding,
            (int) padding);
    gridView.setHorizontalSpacing((int) padding);
    gridView.setVerticalSpacing((int) padding);
}


@Override
public void update() {
    if(adapter !=null)
    {
        adapter.notifyDataSetChanged();

    }
    else
    {
        Log.d(getTag(),"null");
    }
}}

Now for full screen view, I have made a new Activity a two separate Layouts one for screen and one for image as below

fullscreen.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" /></LinearLayout>

fullscreenImage.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
    android:id="@+id/imgDisplay"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitCenter" />

<Button
    android:id="@+id/btnClose"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="15dp"
    android:layout_marginTop="15dp"
    android:paddingTop="2dp"
    android:paddingBottom="2dp"
    android:background="@drawable/button_background"
    android:textColor="#ffffff"
    android:text="Close" /></RelativeLayout>

Now i created a Adapter class

FullScreenImageAdapter.java

public class FullScreenImageAdapter extends PagerAdapter {

private Activity _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;

// constructor
public FullScreenImageAdapter(Activity activity,
                              ArrayList<String> imagePaths) {
    this._activity = activity;
    this._imagePaths = imagePaths;
}

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

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((RelativeLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    ImageView imgDisplay;
    Button btnClose;

    inflater = (LayoutInflater) _activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
            false);

    imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgDisplay);
    btnClose = (Button) viewLayout.findViewById(R.id.btnClose);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
    imgDisplay.setImageBitmap(bitmap);

    // close button click event
    btnClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            _activity.finish();
        }
    });

    ((ViewPager) container).addView(viewLayout);

    return viewLayout;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((RelativeLayout) object);

}}

Now the full screen Activity

FullScreenViewActivity.java

public class FullScreenViewActivity extends Activity {

private Utils utils;
private FullScreenImageAdapter adapter;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fullscreen_view);

    viewPager = (ViewPager)findViewById(R.id.pager);

    utils = new Utils(getApplicationContext());

    Intent i = getIntent();

    int position = i.getIntExtra("position",0);

    adapter = new FullScreenImageAdapter(FullScreenViewActivity.this,
            utils.getFilePaths());

    viewPager.setAdapter(adapter);

    // displaying selected image first
    viewPager.setCurrentItem(position);

}}

Now when i click on any image nothing happens, I got no errors on logcat, no any warnings nor any app crash.

I have debugged the app but it doesn't goes to that point.

I'm stuck to it, and don't know what to do

Any help would be highly appreciated.

Charuක
  • 12,953
  • 5
  • 50
  • 88
Moeez
  • 494
  • 9
  • 55
  • 147
  • Do foll changes: set default image to imageview in fullscreenImage.xml, debug and test if you are getting bitmap in FullScreenImageAdapter. – Sagar Pujari Feb 06 '17 at 06:25
  • @faisal1208 what do you want to achieve ? click event or full image? you already have two downvotes can you consider making a little bit more clear so we can help . can you post your out put and expected one?? – Charuක Feb 06 '17 at 09:32
  • 2
    @faisal1208 as a side note this is my personal opinion but I do not recommend those sites , the site you have given I also followed and figured out(some time ago) that sometimes the whole block of code is not there.Not sure why but may be they needs to download the zip of there project! which is in the **DOWNLOAD CODE** you can use it and check even the author has done the same thing or not by comparing thats the easy way.But as my opinion I dont trust them ,only Android document and stack overflow will be enough to make you a good Programmer – Charuක Feb 06 '17 at 09:41
  • @faisal1208 where are u starting the `FullScreenViewActivity`? – rafsanahmad007 Feb 12 '17 at 07:40

3 Answers3

2

This might not be the answer that you are looking for! But still if your code does not work this will help you towards your solution.If you know the whole process you might be able to plug and play this.

This answer is taken from the same tutorial guy that you followed which has kind of a same pattern with your code. Note image array is hard-coded.

Have a look, Take a special note at AndroidGridLayoutActivity !

Launcher Activity : HomeActivity Other activity you need in your Manifest :FullImageActivity


grid_layout.xml >

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:fitsSystemWindows="true"
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="3"
    android:horizontalSpacing="5dp"
    android:verticalSpacing="5dp"
    android:gravity="center">
</GridView>

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

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

</LinearLayout

HomeActivity >

public class HomeActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);
        GridView gridView = (GridView) findViewById(R.id.grid_view);
        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter(this));
    }
}

AndroidGridLayoutActivity > (check the comments in this)

public class AndroidGridLayoutActivity extends Activity {

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

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

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

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                //----------------------Seems In your code you are missing this Part----------------------
                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
            }
        });
    }
}

ImageAdapter >

 public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    // Keep all Images in array - I have hard coded this
    public Integer[] mThumbIds = {
            R.drawable.casper, R.drawable.casper,
            R.drawable.monkey, R.drawable.monkey,
            R.drawable.cub, R.drawable.cub,
            R.drawable.mouse, R.drawable.mouse,
            R.drawable.casper, R.drawable.casper,
            R.drawable.monkey, R.drawable.monkey,
            R.drawable.cub, R.drawable.cub,
            R.drawable.mouse
    };

    public ImageAdapter(Context c){
        mContext = c;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(150, 150));  /// IF YOU WANT TO CHANGE IMAGE SIZE CHANGE HERE
        return imageView;
    }

}

As a separate project this will work.In case if you cant fix your issue plug and play this.Just posting as a help!

Output >

![3TVetv](http://i.makeagif.com/media/2-06-2017/3TVetv.gif)

Charuක
  • 12,953
  • 5
  • 50
  • 88
0

You can try using OnItemClickListener on your grid view like below

 gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //Get your item here
                adapterView.getItemAtPosition(i);
               //Do your code here 
            }
        });

Please make sure that you removed your click listener from getView otherwise it will make conflict.

Anuj J Pandey
  • 656
  • 1
  • 4
  • 17
0

Please make sure you have to intent single imageview class

Send position id to imageview class I used below code for full screen image view

imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Intent i = new Intent(mContext, Singleimageview.class);
        i.putExtra("pos", (Integer) itemView.getTag() + "");

        mContext.startActivity(i);
        overridePendingTransition(R.anim.pull_in_left, R.anim.pull_out_right);
    }
});
Pang
  • 9,564
  • 146
  • 81
  • 122
Venkatesh
  • 1,034
  • 13
  • 25