How to get clicked Image from ImageView and send to other activity - android

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

Related

First Activity to Second Activity (Intent, Database and Adapter)

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

How to show clicked image in viewpager

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.

How get the calling activity of an intent?

I have 2 list views :
- The first one is instantiated in a fragment
- The second one in a activity.
They display the same information
On click on a list item, I open another activity to display item detail.
Depending on the calling activity/fragment, I want to render a different layout, so I need to know in the activity that render the detail of a item, which activity created the intent ?
getCallingActivity and getParentActivityIntent seems to be declarative and fixe, isn"t it ? in my case there are both null.
How can I do that ?
You can pass an argument with your intent as:
intent.putExtra("ParentActivity", "ActivityA");
or
intent.putExtra("ParentActivity", "ActivityB");
In your next activity, use this:
String parentActivity = getIntent().getStringExtra("ParentActivity");
And render the layout according to result.
Hope it helps.

Load Activity with dynamic view and content in Android

I am very new to Android development and I'm trying to figure out the best way to achieve the following goal:
I want to have a main menu that holds several buttons. Each of those buttons should load a second Activity and pass some data to this Activity.
The Secondary Activity should display a tabbar that switches between three different views or activities:
1. A static view created for each element (button on main menu)
2. An image gallery with a set of images associated with that element
3. Another page with dynamic content like a map and images.
My thought: Create a json file for each element and pass the name of that file to the Secondary Activity with could then create the views using that data.
Problem: Can you store the name of a layout.xml file as a string and then load it?
So what would be the best Practice to approach this?
Pass the information of which layout/which views you want to display in the second activity as an extra on the intent used to start the activity:
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("layout_identifier", 5);
startActivity(i);
Then extract this information in the receiving activity onCreate():
Bundle b = getIntent().getExtras();
int layout = b.getInt("layout_identifier", 0);
switch (layout) {
case 5:
setContentView(R.layout.activity_second_layout5);
break;
Note that the datatype passed does not necessarily have to be int, you can pass all primitives, strings and a third option called Parcelable (https://developer.android.com/reference/android/os/Parcelable.html) which can be thought of as a container for objects.

Android: Dynamically Change Activity Title to Text of Selected List Item

I'm new to Android development.
I created a simple master-detail app that starts with a simple, vertical scrolling list of topics.
When the user selects a topic, a details screen appears, replacing the first screen, with a list of details that pertain to the selected topic.
I want the title for the details screen to show the topic the user has selected on the first page, but haven't been able to solve the problem after working for almost a week.
All I need to know is, Can this be done? Not looking for someone to solve this for me, but maybe a hint or a link to a tutorial that shows how this can be done.
Note: I'd post a drawing of what I want to do, but I'm new here and don't have 10 reputation yet.
Thanks,
SonCoder
Not exactly sure what you want but either way..
-You have a listview. Each view (the data) in the listview should be a represented by a model. (aka a separate class containing specific information that you want to represent for each listitem.
-Write a custom list adapter (extend from base adapter).
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
In the getView method of this class you load the the String field of the model that you want in the textview.
-Make sure to use the viewholder pattern in the adapter above. I noticed the example doesnt use one. This speeds up scrolling in the list because there are much fewer calls to findViewById.
-in the list activity set up a View onClick listener. This should create an intent (for launching an activity) or a fragment transaction (for fragments). Send the instance of your entire model (will get from
parent.getAdapter().getItem(position);
in the on click method) into the detail activity.
-if you want to set a textview title just get the textview and set it from the model. It will be the same filed you inflated in the getView method of the adapter.
-if you want to set the titile in the actionbar set:
this.getActionBar().setTitle(title)
This is simple. Just send extra data in the intent that starts the activity and then in the activity's onCreate read the data and then use the setTitle(myString) method from the activity.
setTitle(String title) can be called from anywhere using the activity by the way.
So, your in your listadapter, then you set a listener on your view right? A simple onClickListener on the whole "root" view is just fine.
In the listener you say something in the ways of this:
Intent intent = new Intent(myActivity, MySubActivity.class);
intent.putExtra(key, titleName);
myActivity.startActivity(intent);
Note that the activity reference should be set in the constructor of the adapter and that the "key" String is something you get from your strings.xml. Do not duplicate these in code since if you change one and forget to change the others you might get some wierd NPEs.
Continue in your MySubActivity's onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String key = getString(R.string.my_title_key);
String title = intent.getString(key);
setTitle(title);
}
NOTE: I'm not sure of all method names are correct and such but something like this.

Categories

Resources