Keeping history of buttons id using android - java

I am learning android programming so be cool with me.
My question is, i am having for example 5 buttons.User clicks any one button out of 5 and clicks another button.So how to keep track of previously clicked button ids.So the 2nd button clicks output is based on the previous button.
Can any one throw me pointers on that.I am new to java and android.

I will recommend you to follow (Screen State) base approach; as below:
1.) Create Screen State class and assign each state a unique value.
2.) You should mention Screen state stack and the current screen state.
3.) when you press a button, call a OnstateChange() function. Which should determine the next screen (based on current screen and whether user moved forward or backward). If user moved backward, pop the screen from stack and mark it as current screen.

If you need to remember only the very last button you clicked, use a class member variable to remember something about it (the button's ID, or a numeric index, or something). You should set this member variable in the onClick() call corresponding to the button.
If you need to remember the entire history of clicked buttons, use a List implementation (ArrayList should be fine) and add() the information about the button (the button's ID, or a numeric index, or something) in the onClick() method corresponding to the button.
If you have different OnClickHandler's for each button, this is pretty straightforward. If you share an OnClickHandler, you will need to use some identifying property of the View you get passed in as an argument to onClick().

Related

How can I create a popup menu that allows user to choose a certain elements from a list on Java?

I am writing a Java program to record some events and their status, i.e., done or not. I have implemented the recording and calculations, but I also want to have a button that will open another window that will list the events with a checkbox corresponding to each of them, so I can pick which ones' statuses I will change to done. I have only used Swing libraries so far.
I want this window to contain one event per line, with a checkbox next to the event. Each checkbox should determine whether the status of the even should be changed or not. I had trouble even making such a window open and list the items, let alone put the checkbox.
Note: The design is not too important. This won't be a published project, I will mostly use it for personal work. I can do with buttons instead of checkboxes, where any click changes the status of the event next to it.
Thank you in advance for your valuable responses.

Android Studio, change the behavior of the back button to affect items in a RecyclerView

I have a problem regarding the back button feature together with RecyclerView, my goal is to emulate the behavior in the Contacts application, where you press and hold (long press) an item in the RecyclerView and a check box appears behind the item(s) (all of them). Then, when you press the back button, all the check boxes disappear.
Regarding the information I provided we can break the problem in two parts:
First, we need to solve the long click problem, which I suspect we can use
NotifyDataSetChanged() together with hiding the check boxes in the XML so that we can switch between checkbox.setVisibility(VISIBLE) and checkbox.setVisibility(GONE).
Lastly, and this is the hardest part for me, I would like that, when back button is pressed, all the check boxes disappear, instead of leaving the app.
Thank you in advance and sorry if the problem is not understandable, as this is the first time I am posting a problem and English is not my native language.
What you are describing is known as Contextual Action Mode.
You'll notice, that when you long press the item the toolbar (action bar) on top changes and shows the number of selected items and a set of action you can apply to the selected items.
Pressing the back button then cancels the action mode.
It is beyond Stackoverflow's scope to explain the whole action mode system, but you can simply search for it in the internet.
Here is a tutorial for beginners, for example.
I can give you a better answer if you post your code.
You'd probably want your adapter to contain a list of selected items. If the list has any elements, you show the checkboxes and check the ones that correspond to the list. If it is empty, you don't do that. You're correct about notifying data set changed. Long press on an item would add that item to this list. Then when your adapter re-laid out your items, it would show the checkboxes since that list would not be empty.
For the second issue, you'd want to override onBackPressed() in your activity. Then you can have some code like:
public void onBackPressed() {
if (adapter.hasItemsSelected()) {
adapter.clearSelection()
} else {
super.onBackPressed()
}
}
You'll need to write these adapter methods. hasItemsSelected should check to see if there are elements in the selected items list and clearSelection should clear the list and notify data set changed.

I'd like to select an Image View with one click and then add a duplicated image view to another view with a second click.

For our purposes, let's think of it as a chess game. So I touch the queen and click it, then I touch the square that I'd like to move the queen to and the queen moves to that square.
I think this is a pretty simple task, but I've searched high and low for an answer but yet to find one.
Original I was using an OnTouchListener and Drag and Drop, but it will work much better with OnClick so that the user clicks twice to move the object. (I think)
Thanks for the help.
Setup an OnClickListener and a variable to hold the selected field. onClick, check if there is already a field selected, if not, store the clicked field in the variable. If there is already a selected field stored, move the piece from the stored field to the clicked field and clear the variable to allow a new move. You can apply your game logic/rules in the second branch too.

Different map markers for every button

I would like to create a main intent(activity) in which someone can choose a track to follow. When a person clicks a button for a certain track, another intent will appear showing the markers along that track on a google map. There will be a "back" button which will bring you to the previous activity.
My question is... can I use a single map and, depending on the button that is clicked, to add specific markers to the map(and erase the previous ones) or highlight that track? How can I transfer a value from one intent to another?(I thought that all the buttons can start the same intent and depending on the value a specific track will be shown).
Any other ideas are welcomed :)
yes you can achieve this using single map.
just call clear() on google map object.
and secondly, on button click pass data using intent.putExtra(string,string)
method and read data by parsing bundle object in map activity
and accordingly add markers to your google map.

Appropriate way to invert/highlight a button in android

In my app I dynamically create a list of buttons. The user should be able to click on them. A short click would select the buttom as the current item, while a long click should enter the editor for this item.
Now I wonder how I can determine and set the colors that i should use. My first idea was to simply read the background and textcolor and switch them, however I'm not sure if this would be really the appropriate way to do this. So I was wondering if there is a an appropriate way of how to retrieve colors.
Of course I could hardcode some colors, but I don't know what color scheme the user has set and they might not be visible in a good way.
Please note, that, since I have to create the buttons dynamically, I can not set it in the XML.
You could simply use a ToggleButton, so android will take care of marking a "clicked" button as selected.
Since ToggleButton is a View, it has a setOnLongClickListener(Listener)-method, which can be used to make the ToggleButton long-clickable.

Categories

Resources