Creating a custom Gallery - overriding onFling - android

So, I've followed this particular thread (How to stop scrolling in a Gallery Widget?) yet I am unable to get it to work properly.
I've created a custom MyGallery class extending Gallery. I've added the code in the link above...am I supposed to add <com.example.mygallery to the XML file? If so, do I also add the import to the java file or is this not needed because of the XML file? I'm so very confused.
I want to simply make the gallery move one image at a time per fling.
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/carlot_background"
>
<com.gallerytest.mygallery
android:id="#+id/thisgallery"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
mygallery.java:
package com.gallerytest;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;
public class mygallery extends Gallery {
public mygallery(Context ctx, AttributeSet attrSet) {
super(ctx);
// TODO Auto-generated constructor stub
}
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
return e2.getX() > e1.getX();
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
int kEvent;
if(isScrollingLeft(e1, e2)){ //Check if scrolling left
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
}
else{ //Otherwise scrolling right
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(kEvent, null);
return true;
}
}
main.java:
package com.gallerytest;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mygallery gallery = (mygallery) findViewById(R.id.thisgallery);
gallery.setAdapter(new AddImgAdp(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(main.this, "Position=" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
private Integer[] Imgid = {
R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5};
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.Gallery1);
GalItemBg = typArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return Imgid.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
imgView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imgView.setBackgroundResource(0x0106000d);
imgView.setLayoutParams(new mygallery.LayoutParams(300, 240));
return imgView;
}
}
}
I'd love some help. Thanks!!
~Rick

Just add the attrSet param to the constructor of your custom gallery:
super(ctx, attrSet);
This worked for me.
Leo Vannucci

Yes. You should just use com.gallerytest.mygallery instead of Gallery in XMLs. Everything will be working fine becuase mygallery is a subclass of Gallery. No need for imports in XML.

Changing the XML was key...i had been getting TypeCastException for sometime and couldn't find the reason in my code. Finally found in this post " You should just use com.gallerytest.mygallery instead of Gallery in XMLs" and solved my problem. thanks a lot.

Related

Android Grid View Images : Show Text

I have prepared a grid view in a fragment which shows images and when you click on them it shows the image in another Activity. I want the name of the image to be shown below it for that i took a textview but it shows position. How to show the name of the photo choosen from grid view on that textview. Below is the code fragments.
full_image_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="500dp"
android:layout_height="500dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/image_title"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
FullImageFragment
package com.androidbelieve.HIT_APP;
import android.app.Activity;
/**
* Created by Akash on 2/13/2016.
*/
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class FullImageFragment extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image_layout);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
TextView textView = (TextView) findViewById(R.id.image_title);
textView.setText(String.valueOf(position));
}
}
GalleryFragment
package com.androidbelieve.HIT_APP;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
/**
* Created by Ratan on 7/29/2015.
*/
public class GalleryFragment extends Fragment {
private GridView gridView;
private ImageView imageView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.fragment_gallery_grid, container,
false);
GridView gridView = (GridView) view.findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(view.getContext()));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getActivity(),FullImageFragment.class);
i.putExtra("id",position);
startActivity(i);
}
});
return view;
}
}
Please Help Me Out!!!
ImageAdapter
package com.androidbelieve.HIT_APP;
/**
* Created by Akash on 2/13/2016.
*/
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import java.util.ArrayList;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.akash, R.drawable.akash,
R.drawable.akash , R.drawable.akash,
};
// Constructor
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(200, 200));
return imageView;
}
}
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getActivity(),FullImageFragment.class);
i.putExtra("id",position);
i.putExtra("image_name",arrayImageName[position]); // get your selected image name and pass it
startActivity(i);
}
});
In your image display class,
String name = i.getExtras().getString("image_name");
TextView textView = (TextView) findViewById(R.id.image_title);
textView.setText(name);
You can do this way:
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.akash, R.drawable.akash,
R.drawable.akash , R.drawable.akash,
};
public String[] mThumbNames = {
"akash 0", "akash 1","akash 2", "akash 3",
};
In Next Activity:
TextView textView = (TextView) findViewById(R.id.image_title);
textView.setText(imageAdapter.mThumbNames[position])
Edit 1:
private class CustomClass{
public int drawable;
public String title;
public String information;
}
ArrayList<CustomClass> listCustom = new ArrayList<>();
Integer[] mThumbIds = {
R.drawable.akash, R.drawable.akash,
R.drawable.akash , R.drawable.akash,
};
String[] mThumbNames = {
"akash 0", "akash 1","akash 2", "akash 3",
};
String[] mThumbInfos = {
"akash 0 Info", "akash 1 Info","akash 2 Info", "akash 3 Info",
};
for (int i = 0; i < mThumbIds.length; i++) {
CustomClass customClass = new CustomClass();
customClass.drawable = mThumbIds[i];
customClass.title = mThumbNames[i];
customClass.information = mThumbInfos[i];
listCustom.add(customClass);
}
Hope this will help you.
I think you want something like this?
Hiren Patel has already answered that. I am just putting it in steps.
1) create 'CustomClass' which will act as POJO class to structure your image name, resource reference.
2) Create an adapter with extends ArrayAdapter and take 'CustomClass' as argument
3) In your activity, create a list of that class and load the data (Image name, resource id etc)
4)Initialize your adapter with above list (can be array as well)
5) access that values on getView method of adapter

How To Load Separate Pages In Android Gridview While Clicking Blocks or Items?

I want to load separate pages while clicking on Gridview blocks. By clicking Gridview Items , i want to load a full page that will be contained image or some description. I have set the gridview basic settings. Is it possible or how can i do that for a descriptive page viewing while clicking gridview blocks-items in android.
package com.android20.personalities;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.app.Activity;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gv = (GridView) findViewById(R.id.gridView);
gv.setAdapter(new ImageAdapter(this));
gv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
}
});
}
}
The Java class of ImageAdapter
package com.android20.personalities;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
Context imgCntxt;
public ImageAdapter(Context c)
{
this.imgCntxt= c;
}
#Override
public int getCount() {
return images.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgview;
if(convertView==null)
{
imgview = new ImageView(imgCntxt);
imgview.setScaleType(ImageView.ScaleType.FIT_XY);
}
else
{
imgview = (ImageView) convertView;
}
imgview.setImageResource(images[position]);
return imgview;
}
public Integer[] images =
{
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6,
R.drawable.image7,
R.drawable.image8,
R.drawable.image9,
R.drawable.image10
};
}
Onclick of gridview item you can start new activity load content by passing griditem position., or by checking position add content in intent
you can add onClickListner in Image adapter itself and do the stuff in your coding to go to other activity with position and write code for showing image with full screen .
Check this might be helpful http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/

how to add images in array from sd card instead of drawable

i am working on a image GridView app in which i am adding images in array from drawable, but now i want to add images in array from sd card.
note: i want to add images from specific folder like: "sdcard/android/data/com.example.images"
GridView Class
package com.example.androidhive;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
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));
/**
* 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);
}
});
}
}
ImageAdapter Class:
package com.example.androidhive;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
// R.string.http_icons_iconarchive_com_icons_martz90_circle_512_android_icon_png
R.drawable.pic_1, R.drawable.pic_2, R.drawable.pic_3,
R.drawable.pic_4, R.drawable.pic_5, R.drawable.pic_6,
R.drawable.pic_7, R.drawable.pic_8, R.drawable.pic_9,
R.drawable.pic_10, R.drawable.pic_11, R.drawable.pic_12,
R.drawable.pic_13, R.drawable.pic_14, R.drawable.pic_15 };
// Constructor
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(70, 70));
return imageView;
}
}
example of what i want:
// Keep all Images in array
public Integer[] mThumbIds = {
// how to add images from sd card here
};
how do i add images to array from sd card instead of drawable.
Just put all filenames from that specific folder in a ArrayList<String>. Then change getView() a bit to let the ImageView load from file.
You can create a Drawable from a Bitmap, you should check out a library like "Universal Image Loader" and then use
Drawable d = new BitmapDrawable(getResources(),bitmap);

Out of MemoryError when creating gallery on android

I am getting this error while creating an android gallery in eclipse. When I add only two images, it runs fine but when i add more images to gallery, it gives this error. Any help would be appreciated!
package com.hospital;
import com.hospital.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class myGallery extends Activity {
private final Integer[] pic = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3
};
ImageView imageview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Gallery gallery = (Gallery)findViewById(R.id.pics);
gallery.setAdapter(new ImageAdapter(this));
imageview = (ImageView)findViewById(R.id.ImageView01);
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView <?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(myGallery.this, "" + arg0, Toast.LENGTH_SHORT).show();
imageview.setImageResource(pic[arg2]);
}
});
registerForContextMenu(gallery);
}
public class ImageAdapter extends BaseAdapter {
private final int mGalleryItemBackground;
private final Context mContext;
private final Integer[] pic = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray attr = c.obtainStyledAttributes(R.styleable.photoGallery);
mGalleryItemBackground = attr.getResourceId(
R.styleable.photoGallery_android_galleryItemBackground, 1);
attr.recycle();
}
public int getCount() {
return pic.length;
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(pic[arg0]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
}
OutOfMemory is a usual error coming when using large number of bitmaps is used.The only way to reduce the error is to Call System.gc();.System.gc() in your code the Java VM may or may not decide at runtime to do a garbage collection at that point. Try calling "Bitmap".recycle() on your bitmaps after you finish your business with them. This will help you to reduce this error to the most.
Google has provided the lessions to deal with the issue you are facing you can visit it in the following link.
http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
Hope this helps.

Displaying pictures in a gridview

I am trying to display pictures that I had placed in the drawable folder using a Gridview. I am getting some errors. My code and the errors I am getting are shown below.
package com.newapp;
import android.R;
import android.R.drawable;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;/*Error here: main cannot be resolved or is not a field*/
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);/*Error here: main cannot be resolved or is not a field*/
GridView gridview = (GridView) findViewById(R.id.photogrid);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_0, /*Error here: sample_0 cannot be resolved and is not a field*/
R.drawable.sample_1,/*Error here: sample_1 cannot be resolved and is not a field*/
R.drawable.sample_2,/*Error here: sample_2 cannot be resolved and is not a field*/
R.drawable.sample_3,/*Error here: sample_3 cannot be resolved and is not a field*/
R.drawable.sample_4,/*Error here: sample_4 cannot be resolved and is not a field*/
R.drawable.sample_5/*Error here: sample_5 cannot be resolved and is not a field*/,
R.drawable.sample_6,
/*Error here: sample_6 cannot be resolved and is not a field*/
};
}
So can somebody tell me what is causing these problems? Any help will be much appreciated.
Thanks in advance
Here is your first problem: import android.R
Do not import this as this is the android system's R package, and not your own project's R file, hence main cannot be identified, as the R you are referring to for main in the android.R package does not exist.
If you remove this import, "main" will be recognized, as your own R file will be referenced. The same rule applies for the ImageAdapter. If your remove the import: import android.R.drawable you will avoid the problems you have in this class.

Categories

Resources