I have a recycler view in activity_main.xml with a few images. When I click any of the image I go to another activity which has a viewpager. The problem is whenever I click any image the viewpager show images from the starting. I want to show the image that has been clicked just like in google photos or any other gallery app. It will be great if anyone help me out.
Thanks in advance
I guess (because you didn't post any code) that you are loading your images from an url, so when you click an image you start a new a activity. If that guess is correct then I would put the url of the selected image into an extra param and then add it to the intent, so when the new activity comes out I can get that value again and just show that image.
Extra parameters for intents work like this, first add the value that you want to send to the other activity:
// MainActivity
Intent intent = new
Intent(getContext(),
pictureActivity.class);
intent.putExtra("key",
"website.com/image.jpg");
getContext().startActivity(intent);
Then in the other activity you have to get the value that you put into the extra:
// PictureActivity
if (getIntent().hasExtra("key")) {
String url =
getIntent().getStringExtra("key");
}
The next thing I would is just take the url and inflate the image into the image view or wherever you want it to be shown. I hope that I can be helpful enough to put you in the right direction.
Related
I have a database and adapter for the first activity showing the name and description. There is a button on each list item which takes you to the second activity displaying a unique image related to that item.
I have included an Intent from the first activity to the second activity.
So on the second activity I would like to add the image related to the item clicked.
Question:
(a)Do I include the image in the same database for the first activity or do I need a separate database and adapter for the second activity?
(b)Also do I need to create a separate intent for each item in the first activity as each item has a separate image that it will link to via the button which will be displayed on the second activity.
Your click listener will be one and generic as same lite item view is inflated for all items of adapter.
2.on click you need to pass the Uri string to second activity via intent and display the image Uri in second activity after receiving it from getIntent() in oncreateview() of second activity.
I would like to answer this in two parts.
Part 1: the database: You should use the same table for the image as well. You can always talk to the same database and all the tables from any activity, so no need to have a separate database. So the name, description and image in the same activity.
Part 2: The intent
If you are in a scenario where you have to add any action on click of an adapter item, always use a callback.
When you click on any item, this callback will tell you in the activity that which item in the adapter is clicked.
This medium blog is a very good example to demonstrate this.
In this blog's code, there is a block in adapter where you pass the values from the activity. It is the constructor.
RecyclerViewAdapter(RecyclerViewClickListener listener) {
mListener = listener;
}
If you had added some code, it would help, but I am sure you also have this constructor in your code, so add this listener along with the other data and see how it works out.
Thanks
I am statring programming in Android and i have problem. I must create something like this : https://www.google.pl/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&uact=8&ved=0CAcQjRw&url=http%3A%2F%2Fwww.technotalkative.com%2Fandroid-asynchronous-image-loading-in-listview%2F&ei=R3UKVdTXHoOGzAPO2oKQBw&bvm=bv.88528373,d.bGQ&psig=AFQjCNFkuC6H_DmyQz44Xy2xYZOnb7fAtA&ust=1426835140929053
but after clicked i need to go to second activity with bigger clicked image and descriptiont (e.g On the image is some place and under is description this place)
For this moment i have first activity ImageView+TextView(title) but i do not know how i can get something what let me identifiers clicked image and send send to second activity.
Any ideas ?
I found topic like this :
How can i pass image view between activities in android but this not resolve my problem.
Edit:
I have 2 xml activity for now :
First main activity with listview and second with linearlayout+Imageview+TextView. I use this tutorial for create firs screen :
http://javastart.pl/static/programowanie-android/wlasny-widok-listowy/
Create an OnItemClickListener and set it on your listview. The listener's onClick method is then called whenever you click an entry in the list. It has a parameter 'position' which tells you which row of the list got clicked. Now you can get the URL of the clicked image using imageUrl[position] (following your example).
Now start your second activity and pass that URL along like so:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("imageUrl", imageUrl[position]);
startActivity(intent);
In your second Activity, put this in the onResume method:
String imageUrl = getIntent().getStringExtra("imageUrl");
Voila, you've passed the url of the clicked image along to the second activity. Now just load it into your ImageView and you're done.
you can send bitmap of image in list via intent to other activity , so you will not need to load it again check attached answer
Or Try to use cached image library like that leocadiotine/WebCachedImageView
and pass link string via intent
I'm not so good at Android but I'm working on my project. So please forgive my ignorance.
I'm making a set of similar items with need prefenrences setting, so I made them preference fragments for each, and put those preference fragments in a viewpager, for easier use. I want to go to fragment No.1 if I clicked item No.1, then maybe fragment No.3 if I clicked on item No.3. Simply put, I want the items can link to its own preference fragment in the viewpager. The items are in MainActivity, the viewpager is in its own activity and this viewpager activity would not start as the app starts unless an item was clicked.
I learnt that viewPager.setCurrentItem(position); should be used when I want to get to a specific page of a viewpager. But my items are in MainActivity, not inside the viewpager activity. I set the viewpager as static, and put viewPager.setCurrentItem(position); inside onClick method in MainActivity, but when I click a item, the app crashes. It shows error: java.lang.NullPointerException. The app had worked fine before I dicided to link those items to their own settings, and now I really have no idea...
Could you please tell me whether it is possible to get to a specific page of a viewpager from other activity? If it is possible, how to do? Thank you so much!
Renew:
I tried to mess with the code, I changed the adapter of the viewpager into protected static MyAdapter adapter;. There was no crash then, but I always went to the first page of the viewpager, whichever item I clicked...
I think you need to pass the data to the activity.
When you call the ViewPager in the MainActivity, set a parameter.
Intent intent = new Intent(getBaseContext(), ViewPagerActivity.class);
intent.putExtra("EXTRA_PAGE", (String) position);
startActivity(intent);
Then in the start of the View Pager Activity, get the object and change the currentItem.
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("EXTRA_PAGE");
int position= Integer.parseInt(value );
viewPager.setCurrentItem(position);
}
Android can only have 1 visible activity at the same time, so i think it's impossible in your way.
Another approach will be the use of Fragment, but it depends on your app design and structure.
You can check more about Fragments here (Android documentation).
Hope it helps ;)
Best Regards
I have a layout with image view . On SWIPE LEFT of layout I want to call a new activity.
Intent right_intent = new Intent();
right_intent.setClass(this, mainScreenClass.class);
right_intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(right_intent);
I am able to do this but calling of new activity is taking long pause. Which seems nothing is going on for fraction of second.
Can i do anything so that swipe of image calls new activity without any time.
why do you use Intent.FLAG_ACTIVITY_CLEAR_TOP? this will clear the stack of the activity
Are you using gallery view kind of control, Delay may happen if your downloading images from server and showing that in this imageview so when you swipe an image next image starts loading in next imageview from server. If this is the case you can try to use fragments, So that gallery view and another new activity you calling can be made as separate fragments and can be attached to Single FragmentActivity.
So that while swiping image in single fragment you can replace it with another fragment(your new actiivty contents) or use indicator.onPageSelected(arg0) to keep the another fragment selected(your new activity contents), it will be having somne reduced delay when compared of calling new Actiivty
I have used a tabHost in my android application. The main ActivityGroup class has a listview. When a listview item is clicked, it displays the details in the same tab. I have used a simple activity class for the detail view. the detail view contains a save button which sves the data and returns to the listview(main) screen.
My problem is that for the first time the save button is pressed it works as expected but if the list item is selected again and in the details view if save button is pressed, the application throws error.
Please help. thanks in advance.
The error could be caused by many different things. Check to see if you have implemented your Intent to switch between activities correctly. Like so...
/** Called when the user clicks the save button */
public void save(View view) {
Intent intent = new Intent(this, Main.class);
startActivity(intent);
}
If this isn't what you were looking for, can you provide some examples of your code?