I have one GridView with 3 Column and 3 Rows I want to change Image when User Click any two Images.
for Example I Click First Row 1 and Column 3 Image and Secondly I Click on Row 3 and Column 2 show now i want to change this two Images like Swap the Image How is it Possible ?
public class MainActivity extends Activity {
/** Called when the activity is first created. */
GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView)findViewById(R.id.gridviewmy);
gridView.setAdapter(new ImageAdapter(this));
final ImageAdapter im = new ImageAdapter(this);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
int i=0; int j=0;
if( i != 0){
j=arg2;
System.out.println("First Click "+j);
}else{
i=arg2;
System.out.println("Second Click "+i);
}
im.getItem(arg2);
//im.changeImage();
Toast.makeText(MainActivity.this, ""+arg2, Toast.LENGTH_SHORT).show();
System.out.println("AdapterView "+arg0);
System.out.println("View "+arg1);
System.out.println("Integer "+arg2);
System.out.println("long "+arg3);
}
});
}
}
class ImageAdapter extends BaseAdapter{
private Context mContext;
ImageView iView;
public ImageAdapter(Context c){
this.mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
System.out.println("Item Is :-"+mThumbIds[position].toString());
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
System.out.println("Geting Id of Item "+mThumbIds[position]);
if(iView != null){
iView.setImageResource(mThumbIds[0]);
Toast.makeText(mContext, "Call", Toast.LENGTH_SHORT).show();
}
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if( convertView == null){
iView = new ImageView(mContext);
iView.setLayoutParams(new GridView.LayoutParams(85, 85));
iView.setScaleType(ImageView.ScaleType.CENTER_CROP);
iView.setPadding(8,8,8,8);
}else{
iView = (ImageView)convertView;
}
iView.setImageResource(mThumbIds[position]);
return iView;
}
private Integer[] mThumbIds = {
R.drawable.a_bhaibij, R.drawable.a_dashera, R.drawable.a_dipawali,
R.drawable.a_gandhi, R.drawable.a_holi, R.drawable.a_indepe,
R.drawable.a_janmastmi, R.drawable.a_kite, R.drawable.a_newyear
};
public void changeImage(){
iView.setImageResource(mThumbIds[5]);
}
}
Swaping the images in the GridView is very simple.What you have to do is
1* Store the cliked position,where you want to perform the swaping .
2* By using those two values perform the swap operation on mThumbIds array.
3* Finally invoke the notifyDataSetChanged() method on the Adapter object i.e im.notifyDataSetChanged();
public class MainActivity extends Activity {
/** Called when the activity is first created. */
int i=0;
int firstClick,secondClick;
GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView)findViewById(R.id.gridviewmy);
gridView.setAdapter(new ImageAdapter(this));
final ImageAdapter im = new ImageAdapter(this);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
i++;
if( i %2!=0){
firstClick=arg2;
}else{
secondClick=arg2;
Integer help=new Interger(mThumbIds[firstClick]);
mThumbIds[firstClick]=mThumbIds[secondClick];
mThumbIds[secondClick]=help;
notifyDataSetChanged();
System.out.println("Second Click "+i);
}
}
});
}
}
class ImageAdapter extends BaseAdapter{
private Context mContext;
ImageView iView;
public ImageAdapter(Context c){
this.mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
System.out.println("Item Is :-"+mThumbIds[position].toString());
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
System.out.println("Geting Id of Item "+mThumbIds[position]);
if(iView != null){
iView.setImageResource(mThumbIds[0]);
Toast.makeText(mContext, "Call", Toast.LENGTH_SHORT).show();
}
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if( convertView == null){
iView = new ImageView(mContext);
iView.setLayoutParams(new GridView.LayoutParams(85, 85));
iView.setScaleType(ImageView.ScaleType.CENTER_CROP);
iView.setPadding(8,8,8,8);
}else{
iView = (ImageView)convertView;
}
iView.setImageResource(mThumbIds[position]);
return iView;
}
private Integer[] mThumbIds = {
R.drawable.a_bhaibij, R.drawable.a_dashera, R.drawable.a_dipawali,
R.drawable.a_gandhi, R.drawable.a_holi, R.drawable.a_indepe,
R.drawable.a_janmastmi, R.drawable.a_kite, R.drawable.a_newyear
};
}
I think this may solve you problem.
All the best.
Also do the following for updating the grid view images to complete the swap operation:
im.notifyDataSetChanged();
gridView.setAdapter(im);
gridView.invalidateViews()
notifyDataSetChanged(); did not work for me. eclipse gave an error.
so instead of searching for the real solution, if there is one, I just reloaded the java page.
Of course I am saving the state of the images in the gridview (adapter) in internal storage in a file named graphics. So on reload of the java page it repaints with correct images.
It works.
Related
i want when i click image from gridview, image send to new layout (Details Image)
this is MainActivity
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent i = new Intent(getApplicationContext(), SingleViewActivity.class);
i.putExtra("arg3", arg2);
startActivity(i);
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "okey", Toast.LENGTH_SHORT).show();
}
});
}
ImageAdapter
public class ImageAdapter extends BaseAdapter {
public static final String URL ="http://api.androidhive.info/json/movies/";
Context mContext;
int mThumbIds = 18;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds;
}
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(95, 95));
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 4, 4, 4);
} else {
imageView = (ImageView) convertView;
}
Picasso.with(this.mContext)
.load(URL + position +".jpg")
// .placeholder(R.drawable.loader).error(R.drawable.ic_launcher).fit()
.into(imageView);
// imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images }
in here i try get image from gridview, i try use intent, it's not work
Details Image
public class SingleViewActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.single_view);
Intent i = getIntent();
int position = i.getExtras().getInt("arg3");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.SingleView);
imageView.setImageResource(imageAdapter.getItemViewType(position));}}
can anyone help me
Two points:
Pass the URL of the image you selected from MainActivity to SingleViewActivity.
Use ImageLoader, and do not modify the width/heigth otherwise the memory cache will miss.
Others the cache will do everything for you.
I am new to android development. In my android activity I have a GridView and a Button. The GridView adapter class is displaying a set of icons in the GridView. What I want is, if the Button in the activity is clicked, the set of icons in the GridView should be replaced with another set of icons. And every Button click in the activity should switch the two sets of icons in the GridView.
So, how to do this using notifyDatasetChanged()? Please Help.
GridView Adapter class:
public class CustomAdapter extends BaseAdapter{
boolean imageSetChange = false;
// set 1
public Integer[] mThumbPics = {
R.drawable.pic1, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4,
R.drawable.pic5, R.drawable.pic6,
R.drawable.pic7, R.drawable.pic8,
R.drawable.pic9, R.drawable.pic10,
R.drawable.pic11, R.drawable.pic12,
R.drawable.pic13, R.drawable.pic14,
R.drawable.pic15, R.drawable.pic16,
R.drawable.pic17, R.drawable.pic18,
R.drawable.pic19, R.drawable.pic20,
R.drawable.pic21
};
//set 2
public Integer[] mThumbEng = {
R.drawable.eng_pic1, R.drawable.eng_pic2,
R.drawable.eng_pic3, R.drawable.eng_pic4,
R.drawable.eng_pic5, R.drawable.eng_pic6,
R.drawable.eng_pic7, R.drawable.eng_pic8,
R.drawable.eng_pic9, R.drawable.eng_pic10,
R.drawable.eng_pic11, R.drawable.eng_pic12,
R.drawable.eng_pic13, R.drawable.eng_pic14,
R.drawable.eng_pic15, R.drawable.eng_pic16,
R.drawable.eng_pic17, R.drawable.eng_pic18,
R.drawable.eng_pic19, R.drawable.eng_pic20,
R.drawable.eng_pic21
};
private Context mContext;
View MyView;
ImageView imageView;
public CustomAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbEng.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbEng[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(imageSetChange==false)
{if(convertView == null){
LayoutInflater li=((Activity) mContext).getLayoutInflater();
MyView=li.inflate(R.layout.menuitem, null);
}
else{
MyView=(View)convertView;
}
imageView =(ImageView)MyView.findViewById(R.id.image);
imageView.setImageResource(mThumbEng[position]);
return MyView;
}
else{
if(convertView == null){
LayoutInflater li=((Activity) mContext).getLayoutInflater();
MyView=li.inflate(R.layout.menuitem, null);
}
else{
MyView=(View)convertView;
}
imageView =(ImageView)MyView.findViewById(R.id.image);
imageView.setImageResource(mThumbUrdu[position]);
return MyView;
}
}
/** public void changeImages(boolean change){
this.imageSetChange = change;
cda.notifyDataSetChanged();
}
**/
}
Button onclick listener:
Button lang_change_btn = (Button) findViewById(R.id.button1);
lang_change_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
//cda.changeImages(true);
}
});
Getting images from SD-CARD for a custom Dialog
What I am trying to do:: I am trying to learn to use dialogs, OnClick of imageview i want to display a custom dialog which has images stored in sdcard. then on-select of any one image, I want to set the image for imageview
What i have done:: I have achieved above specifications for images from drawable
DataAcceptActivity.java
public class DataAcceptActivity extends Activity {
InputStream is;
EditText name;
ImageView imageView;
int[] image_array={R.drawable.index,R.drawable.image1,R.drawable.image5,R.drawable.image6,R.drawable.image7,R.drawable.image8,R.drawable.image9,R.drawable.image10};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showImages();
}
});
}
protected void showImages() {
// TODO Auto-generated method stub
final Dialog dlg=new Dialog(DataAcceptActivity.this);
dlg.setContentView(R.layout.grid_view);
GridView GV=(GridView) dlg.findViewById(R.id.gridView_id);
GV.setAdapter(new ImageAdapter(DataAcceptActivity.this));
GV.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
imageView.setImageResource(image_array[arg2]);
dlg.dismiss();
}
});
dlg.show();
}
class ImageAdapter extends BaseAdapter{
Context cxt;
public ImageAdapter(DataAcceptActivity dataAcceptActivity) {
// TODO Auto-generated constructor stub
this.cxt=dataAcceptActivity;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return image_array.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView ;
if(convertView==null){
imageView=new ImageView(cxt);
imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(15, 15, 15, 15);
}else{
imageView=(ImageView) convertView;
}
imageView.setImageResource(image_array[position]);
return imageView;
}
}
}
How can i modify code to achieve my goals !
Thanks
This are Arraylist that hold the bitmap that create from image path
ArrayList<Bitmap> bitmaps= new ArrayList<Bitmap>();
This is your Image folder path
File file = new File(Environment.getExternalStoragePath()+"/Stampii/");
This is list of image in folder
file imageList[] = file.listFiles();
This code take image from path and store in bitmap arraylist.
for(int i=0;i<imageList.length;i++)
{
Log.e("Image: "+i+": path", imageList[i].getAbsolutePath());
Bitmap b = BitmapFactory.decodeFile(imageList[i].getAbsolutePath());
bitmaps.add(b);
}
Now, use this Bitmap array within int[] image_array and use imageView.setImageBitmap(bitmaps.get(position)); for imageView.setImageResource(image_array[position]);
Thanks
I have a gridview which I am inflating in a Dialog view.
Now when I open the dialog ( with Grid View ) it shows a set of 10 images.
when dialog is dismissed the image which I selected is set on the ImageView.
Now my query is can I get the name of the image?? which is set on the ImageView???
I have to save it somehwere.
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
final Dialog groupIconsDialog = new Dialog(CreateGroupActivity.this);
groupIconsDialog.setTitle("Choose Group Icon");
groupIconsDialog.setContentView(R.layout.group_icons_layout);
//calling and setting the image icons to the grid view adapter
GridView groupIconsGrid = (GridView)groupIconsDialog.findViewById(R.id.grid_groupIcons);
groupIconsGrid.setAdapter(new GroupIconAdapter(CreateGroupActivity.this));
groupIconsGrid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
group_icon.setImageResource(new GroupIconAdapter(CreateGroupActivity.this).mThumbIds[position]);
groupIconsDialog.dismiss();
}
});
groupIconsDialog.show();
}
});
ImageAdapter
public class GroupIconAdapter extends BaseAdapter {
private Context mContext;
//icons image array
public Integer[] mThumbIds = {
R.drawable.mogra,R.drawable.rhino,
R.drawable.zebra,R.drawable.lion,
R.drawable.mogra,R.drawable.rhino,
R.drawable.giraffee,R.drawable.giraffee,
R.drawable.lion,R.drawable.rhino
};
public GroupIconAdapter(Context c) {
// TODO Auto-generated constructor stub
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbIds[position];
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
return imageView;
}
}
Yes you can get the name of the Image if you have the resource id of the Image. You can use getResourceEntryName(resourceId)
String name = getResources().getResourceEntryName(mThumbIds[position]);
I am providing my code here.i have breaked an image into 9 parts and shown in grid view how to change images by clicking on them i.e.,(first clicked image should be replaced with second clicked image and vice versa)i have used bitmap array for splitting images and placed them in grid view. so how to change images in grid view by clicking two images the swapping of images should be done how can any one help me.
public class Imagepieces extends Activity {
ArrayList<Bitmap> breakedimages,duplicate;
GridView g;
int i=0,temp,temp2,rpos;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
breakedimages = getIntent().getParcelableArrayListExtra("breaked image");
duplicate = new ArrayList<Bitmap>(breakedimages);
Collections.shuffle(duplicate);
g = (GridView) findViewById(R.id.gridView1);
g.setAdapter(new CutAdapter(this, breakedimages));
g.setNumColumns((int) Math.sqrt(breakedimages.size()));
g.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//=================================================
{
}
});
}
class CutAdapter extends BaseAdapter {
int iwidth, iheight;
Context context;
public CutAdapter(Imagepieces ipieces, ArrayList<Bitmap> breakedimages) {
// TODO Auto-generated constructor stub
iwidth = breakedimages.get(0).getWidth();
iheight = breakedimages.get(0).getHeight();
context = ipieces;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return duplicate.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return duplicate.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView i=new ImageView(context);
i.setLayoutParams(new GridView.LayoutParams(iwidth +5,iheight +5));
i.setPadding(0, 0, 0, 0);
i.setImageBitmap(duplicate.get(arg0));
return i;
}
}
}
can any one do the need for me as i have stuck here how to move the images among themselves
first follow the #Terril Thomas's to select two image for swapping,
Here is the method for swap two Image in List,
public void swapImage(Bitmap i1,Bitmap i2){
int position1,position2;
position1 = duplicate.indexOf(i1);
duplicate.remove(position1);
position2 = duplicate.indexOf(i1);
duplicate.remove(position2);
if(position2>position1){
duplicate.add(position2, i1);
duplicate.add(position1, i2);
}
if(position2<position1){
duplicate.add(position1, i2);
duplicate.add(position2, i1);
}
}
change in onCreate like this
CutAdapter ca = new CutAdapter(this, breakedimages);
g.setAdapter(ca);
than refresh your adapter
ca.notifyDataSetChanged();
Another way we can implement.
In onItemClickListener()
i++;
Bitmap b = null;
if (i % 2 != 0) {
temp = arg2;
b = duplicate.get(temp);
}
if (i % 2 == 0) {
temp2 = arg2;
duplicate.set(temp, duplicate.get(arg2));
duplicate.set(temp2, b);
}