I'm developing a small Android app and am having trouble with one small feature. I want to have a button invoke a series of Dialogs that the user can select a series of options for. For example, pressing the button will bring up a Dialog with 2 choices, pressing either of those choices will open another dialog with 2 more choices. When the user presses a button there, I want the 2 choices to be remembered somewhere.
I initially approached it by making 2 classes that extend DialogFragment and then showing DialogFragment #2 after a choice has been made in #1 but it seems a bit messy and I can't seem to save the 'path', ie: I want an int ranging from 0 to 4 representing the possible paths they could have done with 2 buttons in Dialog #1 and 2 buttons in Dialog #2, so essentially #2 would have to know what choice was made from #1.
Any tips? Cheers.
You can use shared preferences to store say, the text if the button clicked in dialog1, so that you can access that from dialog2, and so on. Just keep appending the text unique to the dialog fragment at each stage to that shared preference.
At any stage, you can retrieve the shared preference, and thus, you would have the path taken by the user amongst the dialogs.
Here is what you looking for (hopefully!) otherwise take it as tips!!
You have best solution for your problem, Just use this WizardPager in your dailogfragment, thats it.
WizardPager:
Just use this Roman nurik's Android-WizardPager
Related
I have a question about the correct logical use of fragments in Android. I know that it was designed to be used to improve tablet experience, but does this imply that fragments should generally ONLY be used for this purpose? Or would it be poor development practice to use fragments as a replacement for a custom compound View?
For example, I am writing an app to keep tally. When the score sheet is produced, it creates a "ScoreCell," a custom compound control, for each player in a ScrollView. What I want to do(but don't see how as a View) is handle internal OnClickListeners that are activated by the containing activity: each ScoreCell has a TextView for the players' name, which I want to long-press to startActivityForResult() to pop open an input dialog to change the name of the ScoreCell. But seeing as the method for OnActivityResult() is unavailable in a View, would it be correct for me to instead make each ScoreCell a fragment?
Here is a picture of the activity to help understand the logic of what I need/am confused about
There are many questions in here, let me go through them.
No fragments are not meant just for tablet experience, in general they allow you to create more modular and adaptable layouts (think classes). They are extremely helpful in tablet layouts/landscape, but also give you reusable components.
At the same time, a fragment is not the right choice here... It is much easier to use a listview, coupled with a custom cell layout. Instead of using another activity to display a dialog simply call new Dialog() and dialog.show(). An activity should only be used as a dialog if it will be used by the user by a long period of time and provides a new "activity" (action)
Items in a list should not each be their own fragment, no. You can pop up a dialog without creating an entire Activity. Take a look at the documentation on AlertDialogs and DialogFragments for your dialogs and you can get the result from that.
Please don't delete it because its a duplicate. I am an android developer with little experience. I have an app with 2 screens. The first has a bunch of options in a TableLayout each selected using a RadioButton.
What I want to do is, when I select one option and click a Button which appears below, the View should switch to the next screen showing some related data and when a Button is clicked I want it to come back to the same screen but then the rest of the options should be available to me so as to repeat the same process with another one of the options selected.
In short I want to be able to maintain the state of the first screen. I can't seem to be able to decide between using ViewSwitcher, ViewFlipper, or multiple Activities or using a single Activity which is what I am doing right now. But as my app gets bigger its very difficult to handle it. Please suggest the best way to do this. I am confused and desperately in need of help because my job depends on it. Thanks in advance
Use 2 activities. Launch the 2nd activity when the user clicks the button. When the user clicks the "back" button (or some other button you offer him) the 2nd activity finishes and the first activity (which was underneath it) is shown.
Note: You've not given us much information, so I can't guarantee that this is the best solution. From what you've said this is the solution I would recommend though. As they say in advertising "Your mileage may vary" ;-)
I'm working on the preference activity. The matter is about the possibility of add a button that allow to confirm, and eventually (that would be nice) a discard button in order to discard the changes applied at the preferences.
Let me explain better.
As far as I've seen it is possible using the common techniques that i've found in the tutorial to set different preferences using checkboxes and so on. The normal use case that involve preferences require that the user performs selections and after that click the BACK button in order to return to the old view.
However in the usability test i've done, it seems that this step is not always straightforward for all the users, and moreover a lot of them are not sure about the fact that the changes on the preferences are comfirmed.
Now we arrive at the question. is it possible somehow to have in the preference view selections (in particular one just composed by a group of checkboxes) to have an OK and eventually CANCEL button?
You have 4 options:
1) Design a layout and use a normal activity similar to the preferences screen where you give the user 2 buttons: Save and Discard.
2) Add a menu to the preferences activity with save/discard (and of course you will have to save the previous state and revert back to it if the user decided to discard).
3) Handle back button press on the preference activity where you popup a dialog asking if they want to save the changes
4) Add 2 "actions" to the preferences where 1 is save and 1 is discard and each goes to its own activity... Complicated and ugly in my opinion...
On a side note, I really believe users are familiar with Android's UI as 99% of the apps using preferences don't have this save button so it should be straightforward to the users that when they click on a checkbox - it is saved.
I'm interested in hearing what you guys think is the most efficient/solid object-oriented design for a simple multiple choice quiz.
Basically, the app presents a series of questions with 4 choices each. If the choice you selected is correct, then a new activity will display some congrats and full details behind the correct answer and if you're wrong it'll say you got it wrong, along with the details, etc.
After that, the user goes to the next question. On each new question, you'll see your score so far. So maybe you get 50 points for each correct answer, something like that.
Here's how I thought about implementing this: Create a custom view with some radiobuttons or some other widget that could be used for choosing an answer. The custom view takes a Question object, which is just a regular old java object, with fields for choices 1-4. Then, in the custom view, I can set the text of the radiobuttons to the choices from my Question object.
So in my activity it would look something like Question myQuestion = new Question("string for choice 1", "string for choice 2"...)
And then..
Use the question with my custom view. QuestionView myView = new QuestionView(this, myQuestion);
Ok so that's all great. I'm just wondering if I'm setting myself up for trouble. For one, I've got to keep track of the score across all the question activities. Does it make sense to have all of my questions subclass some, mostly empty, activity that I create which can have a counter in it that gets incremented anytime the user gets an answer right (i.e in my superclass activity int scoreKeeper; and in each activity that subclasses this activity: if(choiceIsCorrect) scoreKeeper++). This will allow the score to persist across the activities. I realize I could hold a score between activities by passing and extra to each new activity and then simply adding to it, but that doesn't really seem to make sense to me.
Sorry for all of the blabbering..but I guess my questions are: what do you think is the simplest design for this? Also, in general, I always assumed that you should always use a new activity whenever the user is doing a "single, focused thing"...as the Android paradigm states. But sometimes it feels weird to create so many activities. I know the fact that "it feels weird" is no reason not to do it, but when does it make sense to simply reuse on activity (e.g. in this case, just swapping in a new question in my activity and updating the UI accordingly) as opposed to starting a new activity?
Also, a more detailed question - what would be a smooth way to set which choice (e.g. which radiobutton) contains the right answer, so that when the user presses submit, I can check if they have the right answer and yes/no then react accordingly?
To summarize:
What is a straightforward, object-oriented way to create a succession of multiple choice question activities?
What's the most sensible way to keep track of the score?
Does each question necessarily need to be a separate activity? (And, in general, how do you guys approach the question of whether something ought to be a separate activity?)
Also, kind of a particular: what's the easiest way for me to flag which choice is the correct one so I can check to see if the user got right? I know that with a radiobutton, for example, you can use onCheckedChangeListener and retrieve which radiobutton is currently checked, but I'm not sure how to use that in my design to check if the user got it right...
Thanks!
I would not make the "correct/incorrect information activity" its own activity. Instead, when the user selects a radio button choice, and then clicks an "OK" button, a Dialog should pop up. When they close the dialog, there is a button there to advance to the next question.
I would say you should do this all in one activity.
I'm going to use one activity with a ViewFlipper for cycling through questions.
I was in the midsts of tinkering in android with the goal of trying to make this small exploration game. I actually got pretty far so far: a nice sprite system, a title screen with a main menu that is all in game code (no UI buttons or anything) that launch various activities. An activity that loads this cool map view, that when clicked on will load up another view of a more detailed "zoomed in" action part of a map. It all works pretty well but now I am left wondering how well I am going about this.
(What happens to a view when you instantiate and then move the set the context to a new view? I am guessing as long as the activity is alive all the instantiations of various views that an activity uses are still good? And in an activities onPause is when id have to save the data thats changed in these views to persist the state in the event the user quits the game etc.. ?)
I have three menu options at the start of the game. The main one is:
New Game
The new game, I launch my main map
activity that can launch three views:
First two are a loading screen for
the second, a map screen.
Third view is a more detailed action
orientated part that uses this sprite
engine I developed.
It all works pretty good as in the map view you click on a cell it tells the Calling Activity through a listener to start the detailed action view, and passes in the ID of the cell (so it knows what objects to load in the detailed view based on the cell clicked in second view). (This took me awhile to figure out but someone helped here on another question to get this.) Reference that helped me on this part here. I also used the various information here on ViewSwitcher.
I got even a yes no dialog box in the second view that asks if they really wanna goto that cell when they click on it, a yes tells the calling activity to set the new view. What helped me to get this working was this reference here. I also had to fiddle with the calls to getContext and that, I am still not 100% sure what getResources and getContext do for me.
So is it bad practice to call and load a dialog from a view? If it is, then I don't understand how if I have an event in the view that needs to pop up a dialog how I do that? Call back to the activity and let the activity handle it the dialog response and then call a method on the view for the response?
So far all this works great, I did have a bit to learn about threads too. I spawn a different thread for my MenuView (handles the detection of button clicks and responses), my CellMap view which handles the cool big map that users can click on to see the indepth view which is my SystemView view.
So basically the activity call list is like this:
Home.Activity -> ScrollMap.activity which listens to CellMap wher ethe Yes/No dialog apperas (in the view of CellMap) and if the answer is yes to the question, then the onTouchEvent starts up the SystemView view using
private CellMap.MapClickedListener onTouchEvent=
new CellMap.MapClickedListener() {
#Override
public void onMapClick(int id) {
setContentView(new SolarSystem(theContext,id));
}
};
To help anyone else, that listener had to be defined in my CellMap class. Then in the ScrollMap activity when I am about to start the CellMap I just call a method to CellMap sets the map click listener. So far this is the only way I have been able to get data from a dialog (in this case a it was clicked so set the new view) back to the calling activity. Is this proper practice?
Now my biggest question is, I have a playerObject class I want to initialize on new game event (I know how to detect that push) and that then is available to any view in any activity for the life time of the cycle. So far the game activity has just two (3 if you count the loading progress bar) views. I want to get the players name, is that another activity or view? I cannot find a decent tutorial of just how to make a simple input dialogg box that would return a string.
How do i go about passing this stuff around? After I get the players name from wherever that edit box is to be loaded, I want to set the players name in the class and pass that class instance off to another view or activity so they may get at that data. Everything I have searched on this seemed like overkill or not the right way. Whats the best way to pass this "playerClass" around so that I can access it from other views (to say display the players name in every view).
I also have my Home Activity that waits for an onClick on 3 buttons, it then launches their activity that shows the view. I realize a continue game and new game are the same activity and will just change in data loaded off the drive to restore a save game. When the new game button is clicked I would love to get the players name they want to use and of course set the member of the playerClass to this name so it persists.
Where do I call (an edit dialog is it?) the edit dialog from? (more over how do I build one that can take an okay button and input from keyboard etc). Id also like to make it pretty, maybe I could have a simple view that has one edit box in it and a nice image background (though I haven't figured out how to place the edit boxes to fit nicely matching a background i draw for it. I guess id like an edit dialog I can skin to look how i want it to and fit the look of the game).
I am actually kinda happy what I got so far its looking not to bad, just not sure about some specifics like getting user input for a name of the player. Storing that information and passing it then to other activities and views.
Is it better to have one GameMain activity, and a ton of views:
Map view
Character sheet view
inventory view etc etc?
Or Should there be diff activities in there somewhere as well?
You've written a lot here, so I'm go gonna go ahead and answer the questions I think you're asking.
First, how can one share information between activities and views? If you're just sharing it between views in a single activity, store it in the instance of the Activity subclass.
Sharing between activities requires a little bit more. Instead of using an instance of the default Application class, you can create a subclass of Application and use that instead. Put your data in fields of that subclass. To tell your app to use your subclass, modify the app manifest like so:
<application
....
android:name=".YourAppClassNameHere">
....
</application>
As for getting user input, the simple answer is use the built-in EditText view. since you seem to want to give your game a more "custom" style, though, you may need to go about creating your own editable textbox. That and buttons should allow for most sorts of basic user input.
Now, good practice vis-a-vis activities and views. I'm not aware of a standard for this, but when designing I generally try to think of activities as more conceptually separate elements, whereas views are conceptually interwoven. the line between the two is dependent, in part, on the scope of the app; an app with a narrow focus can afford to break more actions down into different activities than can one with a much broader focus.
Your "GameMain" example is a bit on the fence, but I think you've made the right choice: the activity is "playing the game," as opposed to a menu or high-scores table, and the views present different aspects of the game being played.