I have completed the Hello Views tutorial from android developers (http://developer.android.com/resources/tutorials/views/hello-listview.html)
I understood the concept, but it doesn't fit my project. I'm trying to make a list and when clicked I want it to open a webView with different URL's depending of which item is clicked.
I can't figure out which code I need to use...
you just need to set up an OnItemClickListener for the listView:
http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html
Declare a new Webview at the top then within this OnItemClickListener set up a switch statment to assign the URL to the WebView depending on which item in the ListView was clicked. Then out side of the switch statment, but inside the OnItemClickListener, call on the WebView.
Hope this helps.
Related
I'm newbie on android development and developing a app to improve myself.
My app is listing students from db with ListView. I want to do"When I click the name of student, it will show the exam grades" I mean,I want to add new listview to onclick of list item.
Is it possible? What I need to do that(in xml and code),Do you have any code sample?
Create a custom listview (http://www.vogella.com/articles/AndroidListView/article.html) Add a listview to the listview item and set its visibility to GONE. When they click the item set the visibility to VISIBLE.
Add an OnItemClickListener listener in the current ListView and when you get the click event query your data base for that particular student to get result. Once you have result you can load a new ListView in same Activity using Fragment or in a totally new fragment. (You are already using a ListView, so obviously you know how to load data in ListView).
If you want to show the grades in a separate screen, I would suggest that you'll create a new Activity with it's own layout (that will contain the grades ListView) and when a click on a student name will occur you will start the new Activity to display grades (you can pass the student id on the new Activity Intent).
You can also consider using Fragments. This way you will be able to change the layout to master-detail view (like in mail apps) easily on tablets. You can start by looking at this link.
I really need horizontal scrolling in my app, so I overrode Gallery and I'm trying to do something with an OnItemSelectedListener, and a GestureDetector I'm calling from an OnTouchListener I've set on the Gallery. What I want is to remove the auto-selection of Gallery items as I scroll, but I want to be able to click an item to select it.
If I set an OnClick listener on a view that is populated in the adapter, the Gallery fails to scroll. Also, the OnItemClicked event is never called for the listener I set for that on the Gallery.
To remove selection from all items you should do this:
youGallery.setUnselectedAlpha(1);
Maybe my answer to this question will help you. It's something which wasn't intended to be ever realized...
For your case why dont you just use a HorizontalScrollView. Just put a linear layout inside and add as many imageview's as you need if your using images, or dynamicaly add it in java code.
This will elemenate the selection of gallery by default. This will be easier than trying to Override the Gallery widget default methods.
Why not you set some tag while you click on an item in the Gallery to select.
Now once again when you click after the item is selected just check for the tag that you had set, and based on the condition you can move to new screen and whatever task you want to perform.
But keep one thing in mind, you have to set the tag with every gallery item while building the gallery view.
I hope you will get what I want to say.
If still you have any problem then let me know.
I had the same scenario and I solved that.
I have a View with a ListView appended, which actually uses a custom view item done programatically. Inside each list item I have a button which I need to track, to update another item in the view. It is only invalidated when you click on the list item button, not anywhere else in the list item.
So I thought of creating a custom Listener. I trigger it when the button is clicked, but I have no way to access it from the ListView activity.
Is there a way to simulate those setOnItem...Listener's using a custom listener? Thanks in advance
Please see this link to help with how to set onClickListener from within custom ListAdapters.
You can use the myButton.getTag() and myButton.setTag() to put and get data to/from your View/Button.
See this link as well to help with custom ListAdapters.
Here is another good example I found of how to use a custom adapter.
My application should have an activity to show a list of elements. These elements can be selected using some checkbox mechanism AND if it is clicked another list on next screen with the sub elements should be shown.
Need desperate help.
link to snippet would be preferable. thanx.
Create two activities extending from ListActivity. Check this link to create a listActivity and implement click listener for list items and on click event start second activity...
I have custom ListView item with ImageView and TextView. TextView contains HTML string with urls and some regular text. Im my adapter I have code similar to
tv.setText(Html.fromHtml("<a href='http://google.com'>google</a>"));
tv.setMovementMethod(LinkMovementMethod.getInstance());
and this works great but onListItemClick isn't executed when I click on item outside url, whole item looks like inactive.
When I click on url I want to fire default action form urls and when I click on regular text or on ImageView I want to execute onListItemClick is it possible?
Second question, is it possible to start activity using start some activity?
You can't make an intent form an anchor.
It's ok and recommended that you use onListItemClick (it saves you a lot of work), and if you want to open the link in the browser (without using a webview) you can use this an intent, this is an example:
Intent myIntent = new Intent(Intent.ACTION_VIEW,ContentURI.create("http://www.google.com"));
startActivity(myIntent);
Hope this helps.
You need to specify a callback for each of the views that you wish to select. I believe you have three different items: The Listview object, which contains a TextView and an ImageView.
If you would like to have different behavior for each different piece, you need to create a callback for each different piece. You can set a callback in the XML declaration of the view by setting
android:onClick="myCallback"
http://developer.android.com/reference/android/view/View.html#attr_android:onClick