I just have a question regarding changing an Activities layout.
Basicly my problem is:
I have a list view populated with a series of Strings.
I have an OnItemClick event assigned to the ListView,
When the user clicks an item in the list view, I want the current layout to disapear and an image to take its place.
From reading other posts, I understand that the recommended way is to set a separate activity for each item in the ListView, however, seeming as all the activity is doing is displaying an image I think it would be a waste of effort to set up a separate activity for each item in the ListView...
Could anyone give me some help on this?
thanksin advance.
Shaw
You really should create a new activity (ar a new fragment if you have space, but this is another question) which displays the image, for at least the following reasons
it is very, very simple to code and mantain such a solution. What if tomorrow you want to add 2 buttons and some text and maybe a menu for that image? you have your brand new activity to edit and upgrade without risking to damage your list activity.
it is more user friendly. If the user presses back when the image is shown, with 2 activities he will be back to the list, with your solution he will go back BEFORE the list, and this is not what he'd espect
remember this piece of advice: 1 activity = 1 simple task or interaction with user.
list + image display = 2 activities (or fragments)
PS: you do not need to define a different activity for each list item, just pass with the intent to the "ImageActivity" and specify there which image to show!
EDIT 2: to pass to the next activity your current selection, just use the putExtra(String key, T value) (T may vary, check documentation) method of Intent class.
example: intent.putExtra("imgCodeSelected", index) where index is fetched by the onItemClick event. You can put as many extras as you want as long as they have different keys.
Related
my problem goes like this-
this is a commercial application in which users can purchase different products they see within app..
there is one view cart button available on the main screen
there are 10 different products available in an activity and there are 3 such activities( each of them containing various different products)
these products are arranged in a linear fashion (linear layout).
every product is having an add to cart image button.
what i want is, when the user presses that button, 3 things should happen..
a new activity will be created containing that product information (when the user presses another "add to cart" button, this new product will come below the previous product and so on).
there is one cart button in the menu bar, that cart should show a number equal to the nos of product in the cart.
its simple, a small flash notification that the product has been added to the cart.
any help will be deeply appreciated.
You would see some benefit by using Fragments instead. Use a main Activity as a "switchboard" and multiple Fragment instances to show the per item details.
You don't need to dynamically create a new Activity. Taking into consideration that you call yourself a newbie in android development, I can only tell you what to study to accomplish your plan.
First, learn to make an Activity that will contain a ListView. Make an XML layout for your ListView items, containing TextViews, Buttons, whatever you want. Then, make an adapter that will fill items with corresponding values (texts, button names, images, etc).
And second, make them all clickable (onItemClickListener or an onClickListener for every button) and make your Activity get inflated with a Fragment containing TextViews, ImageViews, etc... whatever you need to represent information about the item selected.
The best practical way is to make an empty Activity with an empty Fragment. The last one will be either inflated (populated) or even replaced with whatever things you need.
Sorry, but teaching you how to make all these things technically is beyond the scope of any sensible answer that someone can post here. It's all the matter of study and practice. But I hope the vector of study I gave you is transparent and affordable.
The title doesn't really cover it all. It's basically the following use case:
Select a bunch of items in a listview
Move to the next activity
Show the selected items in this new activity in a listview
If the user decides to move to the previous section the same items should still be checked
If I were using POJOs I could simply add an OnItemClickListener to the ListView and add the items to an array each time user clicks an item and ship this array to the new Activity. However the ListView is populated using a Loader which gets the data from a ContentProvider and puts it in a SimpleCursorAdapter.
In the current state I can think of a few possible solutions:
1) Add an extra column to the relevant table of the objects in the ListView. Each time the user selects an item this column is updated to indicate it is selected.
This has a few advantages:
Once I have moved to the next Activity I can simply query for all selected items.
When moving to the previous activity I can use this column to show a selected view.
But also a few disadvantages:
Requires an update each time an item is clicked, triggering the loader.
Requires a custom adapter which uses the state of the new column to decide whether it should or should not be shown as checked. Possibly creating a delay between clicking an item and actually showing it as checked.
The default check options in the ListView will be unusable
2) Track the IDs of checked items (using OnItemClickedListener or getCheckedItemIds) and pass them to the next Activity. In the new activity I then create a selection argument of "ID = ? OR " repeating N times (and excluding the last OR) and use the given array as the selection arguments.
Advantages:
No need to keep updating the database.
No extra columns in the database.
Item checking has no extra delay.
Default check options from the listview still usable
Disadvantages:
Moving to the previous activity is now harder. I could return the list of selected item IDs, but the listview only has the option setItemChecked which takes a position. So I'd have to iterate over the entire adapter to find the positions of the items and set them as checked.
I'm probably capable of implementing them without any hassle. At the moment I'm gravitating towards the second option.
So ultimately I have the following questions:
Is there an easier way to do this in Android.
What would be a good way to recheck the items (see the disadvantage in the second suggestion and if there's no better way to do it).
This ListView will also get a search function which will probably again make it a bit harder because if I'm not mistaken filtering resets it every time. So I'd also have to recheck items every time (ideally during filtering).
Your disadvantage "Requires an update each time an item is clicked, triggering the loader." is not quite true it will trigger only if your ContentProvider calls notifyChange you could have it not to call notifyChange in some specific cases
and pressing back by going to the previous activity shouldn't be that hard only if you explicitly call finish() on your activity, otherwise it should be able to save its state onSaveInstanceState
Either way i would use a third approach and simply wouldn't use a second activity but would keep everything inside a single activity use pearhaps two Fragments one for the initial check list and the second one for your second preselected list and would use callbacks and let the activity manage all,is that possible for your use case ?
I wasn't exactly sure how to briefly paraphrase my question in the title, so please forgive me as I never post questions until now. I am new to Android/Java for starters, as the main language I have used so far is C++. My question is that I have a game board layout (similar to checkers/chess). When it is the user’s turn, they are to click the piece they want to move on the board and then the blank location they would like to move it to. How can this be accomplished? Up until this point I have implemented onClickListeners that never rely on another button in the activity to be clicked afterwards and wait for the user to do so.
Brief information on my project currently (unsure if needed):
I am currently using an ImageButtons array (of size 36) and a two dimensional integer array to hold the information of each of the buttons, as they are displayed in a GridLayout in a 6 X 6 fashion. In my MainActivity class I have implemented the OnClickListener and created a switch statement in onClick() for each of the button ids.
I am not sure how much more information is needed on my code for help or if it is completly irrelevant. I tried looking on the internet before choosing to ask finding nothing. It is always possible though I was not correctly phrasing my issue. Thank you to everyone in advance!!! :)
Without any code it is hard to say what you are doing wrong/right. But if you have the images stored in an Array then when they click on one image that can be put into a variable which is used to place on the View that they click on next. If you have a more precise question then please post relevant code and error messages from what you have tried, if any. Hope this helps
after a user click's a piece store that info in a member variable like a current piece and then on second click check if the previous is not null, the current clicked space is empty and start your animation by posting to a runnable implementation. this is the simple logic part. where are you having problem exactly
To optimize your code to change images what you can do is custom animations on the imageview. You can change the position of the image with animation without having to create or store an image. (If you use images bitmap a lot you have a very good chance of running into OutOfMemory exceptions and this will happen on top end phones very easily
How to Move a Button and Change the Button Image
I would put a single Button on every GridView position.
Then, I would use button.setBackground() and button.setVisibility() to display and hide the buttons with various images. In this way, the buttons will appear to move, but you are really just displaying a different button.
I recommend this because it is easier to change the visibility and image properties of a button than it is to actually move the button, although both methods are possible.
You will maintain a 1 dimensional array of images[n] and a 2 dimensional array of buttons[6,6].
As an example, suppose you want to move image[5] from grid position (1,2) to position (3,4):
// Hide the button at (1,2);
button[1,2].setVisibility(Button.INVISIBLE);
// Display the button at (3,4) with image #5.
button[3,4].setBackground(image[5]);
button[3,4].setVisibility(Button.VISIBLE);
Additionally, if your button images are stored in your resources, you could efficiently use button.setBackgroundResource(R.drawable.image-5-id);
The instructions above discuss how to move a Button, but now how to trigger one Button to be moved and then trigger the location into which to move the Button.
To accomplish this, you will have to define two states, such as:
private final static int STATE_PICK_BUTTON = 0;
private final static int STATE_PICK_LOCATION = 1;
private int state;
Initialize state = STATE_PICK_BUTTON;
When the system is in the first state STATE_PICK_BUTTON, all button
presses identified in your onClick() function memorize a grid
position to move from, and in some cases transition the system into
the second state: STATE_PICK_LOCATION.
When the system is in the second state STATE_PICK_LOCATION, all
button presses actually move the button from the memorized grid
position to the grid position of this button press.
Of course, you will have to do all sorts of error checking to make
sure you are allowed to move a button before triggering the state
transition.
Finally, the above suggestion may not work, because it may be impossible to click an invisible button. If this is the case, instead of changing the visibility of Buttons in empty grid locations, leave all the buttons Button.VISIBLE and use a fully transparent Button image for the buttons representing empty grid spaces.
i need some suggestions first,
My application is like, on main screen a user clicks the image button "Courses" and navigates to a list, with three items, when user touches any item, he moves into another "List view" that pick up values from the database, and then if a user clicks on any item of second list,will be navigated to final view or a webview..
suggestion i need is, is it a nice practice to navigate a user from list to another list, in a
application..
and in Iphone application they make back button to each view,(as they dont have a back button on iphone) but in many android applications i didn;t seen back button in list view layout, do i need to have a back button on the top of the list view ?
Second question i have is, do i really have to make a Database for a list,
i have to make atleast 6 or 7 Lists in my application, with some list having values nearly 16
Ad. 1: No, you don't need back button in list view. It's real back button in all android devices.
Ad. 2: No. But when data are changing or you want to perform complex queries then database is preferred. In case of not changable data use xml. In case of simple data you can even use shared preferences.
Here is short guide to android data storage:
http://developer.android.com/guide/topics/data/data-storage.html
Here is simple tutorial to use hardcoded data with listview:
http://developer.android.com/resources/tutorials/views/hello-listview.html
I have a few comments on these:
1) The application might end up having a complex navigation to achieve simple tasks if there are multiple levels of lists. Consider using a Tab layout instead of the first level list, since you mentioned there are only 3 entries in the first list. Also try to consolidate your menus so that user can view options upfront
2) Back button: IMHO in android you should only implement it iff you want to override the default behaviour of the back button.
3) Database for a list: Totally depends on whether you want to change the lists once the app is installed. If they are static then you dont need to build it from the database
i am writing a software that i have to drill down on content a lot. For example when the program starts a listview is displayed. When user clicks on an item, then a second listview must be displayed. For example:
Select Continent > Select Country > Select State > Select City > Select Address
What is the best way to do this (less memory, faster, easier to code etc)? To create multiple listviews with multiple adapters? Or 1 listview with multiple Adapters? Lists are loaded from an external XML File.
So far i am creating a new adapter and setting it to the listview. How do i create a second listview and after clicking on 1st listview displaying the second one, with animation. Any examples?
Extend my class to ListActivity or Activity?
I would make multiple Activities for this. The first activity to display Continent list, the second the Country list, Third State list, etc...
When the user than click the back button. It will go back to the previous activity (list). Even the scrollstate would be remembered.
This will also add the OS animation between the activities. The code will also be separated and memory freed when closing the list activity.
Sending a value from one activity to another ex CountryCode tot the StateListActivty is done by setting a intent.putExtra("CountryCode", countryCode);
A second approach would be to use a ViewFlipper. Adding each listview as a child. And than setting a custom animation on the show-next and show-previous actions.
Note: Using multiple activities may use abit more memory than the ViewFlipper approach.
I would use separate activities for each list but with only one List Adapter class to be shared by all so you can retain consistency in terms of how the lists look + easy to maintain code. You can use a bundle to pass info from one activity to another.
Another thought: is the info you are referring to part of "settings" info? By that, I mean, is it info the user would put in once into your app, or would they put it in almost everytime they use the app (because each time the info would be different)? If it's an "one-off" type of info, you would be better off using a PreferenceActivity.