how to show new image from previous activity index value in android - android

I am working on gridview but when I use all drawable images to show in gridview. I am getting out of memory error. Its obvious. Now I am looking for workaround condition. I am taking help from android hive example. I want to show grid images in thumbnail and on click on that image I want to send the index of that to new activity. And in new activity I have new drawables array I want to set image based on index value I got from previous activity.
FullImage Activity-
public class FullImageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// 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]);
}
}
Here I want to assign new image in above code-
imageView.setImageResource(imageAdapter.mThumbIds[position]);
New Image array that I want to include in above code-
public Integer[] nImages = {
R.drawable.kri1, R.drawable.kri2,
R.drawable.kri3, R.drawable.kri4,
R.drawable.kri5, R.drawable.kri6,
R.drawable.kri7, R.drawable.kri8,
R.drawable.kri9
};
Edit-
Gridview showing mThumbIds images to show. On click on any image I want to show full image So I am passing index to full size activity. Now I want to use that index and image from new array nImages that I am creating in this activity. So index value from previous activity and new image from full size activity to open.
Please help me.

Get the position when you click on the GridView. Then pass that position in put extra like this:
intent.putExtra("imageResourceID", nImages[position]);
mContext.startActivity(intent);
Get that int in fullimage activity like this:
int position = getIntent().getIntExtra("imageResourceID", 0);
Now from new array nImages pass this position in new one like
nImages[position] and get that image resource and set in ImageView resource.

I assume you are sending position value to your full image activity, instead send Image resource ID.
For example:
intent.putExtra("imageResourceID", nImages[position]);
mContext.startActivity(intent);
And inside full Image activity, get the image resource Id and display it in a simple way by using image.setImageResource().

Related

Android: Display images in full screen mode using Picasso

Hi i have some images displayed in gridView using Picasso, when i try to open each image (after onItemClickListener) in full screen mode (inside new acivity) it display only the first image in my gridView whatever the image i click, this is my problem.
This is my code where i display the images from the url inside gridView and OnItemClickListener.
Picasso.with(ToolDescription.this)
.load(SaveSettings.ServerURL +"Images/"+ imagepath)
.into(imageView);
ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(ToolDescription.this, ImageFull.class);
intent.putExtra("imagepath", SaveSettings.ServerURL +"Images/"+ imagepath);
startActivity(intent);
}
});
return myView;
This is my second activity (named ImageFull) where i want to display the clicked image:
ImageView imageView = (ImageView) findViewById(R.id.fullImage);
String imagepath = getIntent().getStringExtra("imagepath");
Picasso.with(ImageFull.this)
.load(imagepath)
.noFade()
.resize(800, 800)
.centerCrop()
.into(imageView);
Either set an individual click listener to each imageview or use the int position to pull the correct imagepath from your gridview item array.
Grid View Android Doc
This link has a easy to follow code for exactly what you're trying to do.. Use the position to find which element you are clicking on an get the respective image url
Looks like what's happening is that you're calling ls.setOnItemClickListener() every time getView() returns.
AdapterView.OnItemClickListener is a single listener for the entire AdapterView, and you're intended to differentiate which item was clicked on by using its position argument.
I'm not sure where imagepath is coming from inside getView(), but I assume it's something like String imagepath = getItem(position).getImagePath(). If that is the case, you can fix your problem by:
1) Move ls.setOnItemClickListener() out of getView()... probably to your onCreate() or whever you set up your GridView.
2) Change
intent.putExtra("imagepath", SaveSettings.ServerURL +"Images/"+ imagepath);
to
String currentImagePath = getItem(position).getImagePath();
intent.putExtra("imagepath", SaveSettings.ServerURL +"Images/"+ currentImagePath);
Probably there will be syntax errors because I can't see the rest of your code, but hopefully this is enough for you to be able to work through them.

Click on first item in a GridView is not working

I have an activity which contains a GridView, each element of the GridView contains an ImageView and when the activity starts I want to perform a click on the first item's ImageView. I have tried several things which did not work:
gridView.performItemClick(gridView.getChildAt(0), 0, gridView.getAdapter().getItemId(0));
and with a performClick() directly.
However I think a found a way to do it by calling a function (performEmptyClick()) inside my custom GridViewAdapter which performs a click on the first item's ImageView.
My problem is that performEmptyClick() is called before the adapter's getView() is called (from debugging) and therefore the onClickListener() of the ImageView is not set up yet. Here is the code:
gridView = (GridView) findViewById(R.id.folderGridView);
folderViewAdapter = new FolderViewAdapter(this, R.layout.folder_item_layout, folderItems);
gridView.setAdapter(folderViewAdapter);
Bundle extras = getIntent().getExtras();
noFolder = extras.getBoolean("noFolder");
if (noFolder) {
folderViewAdapter.performEmptyClick();
}
Can anyone help me fix this?
Try
gridView = (GridView) findViewById(R.id.folderGridView);
folderViewAdapter = new FolderViewAdapter(this,
R.layout.folder_item_layout, folderItems);
gridView.setAdapter(folderViewAdapter);
Bundle extras = getIntent().getExtras();
noFolder = extras.getBoolean("noFolder");
if (noFolder) {
gridView.getChildAt(0).performClick();
}

putting a variable inside a button or image declaration

I am trying to do a data analysis And set image view visibility based on those data. Is that possible to put a variable inside a image view declaration so that I can use an array to analyse data and call the respective image view based on the variable for example
For(inti =0,i<20;i++)
Imageview(i).setvibility(gone)
This will prevent me from calling the image view over and over again .
You can solve this by creating a list of ImageViews.
List<ImageView> list = new ArrayList<>();
list.add((ImageView) findViewById(R.id.yourImageViewID);
list.add((ImageView) findViewById(R.id.anotherID);
for (int i=0;i<list.size();i++) {
ImageView view = list.get(i);
view.visible(false);
}
Now you can iterate through your views.

I have few imageview in activity_A and when i click a image in activity_A it should be opend in activity_B how?

Sir/Madam/Friends,
I am new to android program, I created one Linear layout in this activity I put 20 images, and when i click one image that particular image will open in another activity,in second activity i put only one Image view. So please how to do that?.
i did but my programe will showing only one same image only every time.
//this is the first activity.
public class abcd extends ActionBarActivity{
public final static String VIEW_ID = "name";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abcd);
}
public void click(View view)
{
String iname=getResources().getResourceEntryName(view.getId());
Intent intent=new Intent(this,abcd_disp.class);
intent.putExtra("VIEW_ID", x);
startActivity(intent);
}
}
//this is the second activity
public class abcd_disp extends ActionBarActivity {
ImageView imageView;
int[] im=new int[]{
R.mipmap.a,R.mipmap.b
};
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent=getIntent();
String vidname=(String)intent.getStringExtra(abcd.VIEW_ID);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_abcd_disp);
if(vidname=="aa"){
imageView=(ImageView)findViewById(R.id.img_abcd_disp);
imageView.setImageResource(im[0]);
}
else
{
imageView=(ImageView)findViewById(R.id.img_abcd_disp);
imageView.setImageResource(im[1]);
}
}
The easiest way to do this is:
Suppose you have a set of images in drawable folder with which you are populating ImageView's in your Activity A .
Let the images be #drawable/image1, #drawable/image2, #drawable/image3, #drawable/image4
So once you click on the particular ImageView, you can retrieve image resource name as
imageView.getTag();
But this returns an object. So type cast it to String. This is done to pass it along with intent, since object cannot be passed directly without serializing or parceble.
String imageResourceName = (String)imageView.getTag();
Once you have this, pass this along with the Intent you are going to startActivity() with.
intent.putExtra("resource_name", imageResourceName);
startActivity(intent);
So in Activity B, where there is a ImageView, you set the image src in ImageView using
imageView.setTag(getIntent().getStringExtra("resource_name"));
Further information of setting image using setTag()
Use Fragment instead of the Activity!

Is it possible, to use GridView for activities?

I want show activity on gridView like screenshoot.
When I click on item it open activity, when I click back it show gridView again.
When I click newItem button it create new activity and show it on gridView.
Is it posible or I need use something other, not gridView?
Of course you can, and it's quite simple too.
1 - Create an activity named "GridHome.java" for instance. Make the layout for this activity be only a GridView.
2 - Fill the gridview with pre-loaded images from your other activities. Say for instance, take a screenshot of the other activities and store them as PNG, then add them as drawables into your application, and lastly add them to the gridview via a customer adapter.
3 - Set an OnItemClickListener to the gridview so that when the user clicks on one, you can start a new intent to the activity you wish to open. For this one you can add a method like this to your custom grid adapter:
public String getItemActivityName(int pos) {
return activityNames.get(pos);
}
where "activityNames" is an Array of strings corresponding to the activities you have on the grid view.
And from the "GridHome.java" you would do this to the GridVIew:
mGridView = (GridView) view.findViewById(R.id.home_grid);
mGridView.setAdapter(mAdapter);
mGridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
String activityName = mAdapter.getActivityName(position);
if (activityName.equals(ACTIVITY_ONE) {
Intent intent = new Intent(getActivity(), ActivityOne.class);
startActivity(intent);
}
}
});
Where ACTIVITY_ONE is a constant that represents the name of one your activities.
private static final String ACTIVITY_ONE = "Contacts";
private static final String ACTIVITY_TWO = "Groups";
And so forth and on.

Categories

Resources