Android detect tap or move - for chess piece interaction - android

I want to implement touch listener that detects
Click/tap on chess piece in order to select ot
Swipe/move of chess piece immediately.
How to do it?

Unfortunately, I am not sure what you mean by a "chess piece", but let's assume that the board is a view in the background, and inside you have inner views which are chess pieces.
To achieve the select/unselect given piece effect you should add a simple View.OnClickListener interface to your view. Each of your pieces should have a view class and some data connected to it (like a position, a type of piece, id, etc.). After clicking the piece update the state of some global variable, for example, selectedPiece with an ID assigned to a given piece. You should also change the UI of a given piece, for example change the border to show the selected piece to a user. Then after clicking a new field for moving the piece you should update the UI by moving your piece view to an appropriate position. Also, the data of a piece or a game should be updated - depending on the place of storing the pieces' positions.
For this you should implement the drag&drop feature which is described here: https://www.tutorialspoint.com/android/android_drag_and_drop.html. You should determine the places where the user can move a piece and in case of dropping the piece in the wrong place, cancel the move and go with the piece to a start position.

Basic response to touch events using event.x and event.y works:
https://developer.android.com/develop/ui/views/graphics/opengl/touch
https://suragch.medium.com/how-touch-events-are-delivered-in-android-eee3b607b038
...more info:
Android Touch Listener

Related

Change content of view on swipe

I'm making an android app that will essentially show the user a word, definition and example for each element in the database. It's sort of like a dictionary. I have already made it so that all the words, along with their definitions and examples, have been entered into the database but now I am trying to figure out a way to display them.
What I would like is to have 3 textviews which would contain the word, definition and example respectively in a view and every time the user swipes down to go to the next word entry in the database. Every time the user swipes back I'd like the view to repopulate the textviews with the previous entry in the database.
What I'm wondering is what is the best way to do this? Is this there a particular class that deals specifically with this type of thing? Bear in mind that I don't want to switch views since I would have to make over 300 views but rather just repopulate the values of the textviews with the new values from the database. Bonus points if it's easy to make a flip/slide animation with each swipe but it's not really necessary.
You can use 'onFling' method. You can detect the swipe direction and with a listener for the right direction swipe, you can call your methods for the data source.
here is an example : How to detect swipe direction between left/right and up/down

Click button and that button waits for second button on activity to be pushed. Game board. How to accomplish?

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.

iOS: TableView with expanding cells

I'm currently developing an iOS version of an Android app. On one screen I have a series of rows that, when clicked, expand to provide for information.
Before click:
After clicks:
How can I replicate this behavior in iOS? I don't know whether I should use a table view or just construct generic UIViews. I am using Auto Layout.
You can reload UITableVIewCell at particular NSIndexPath and calculate new height for cell.
Check:
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
from UITableView and
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
from UITableViewDelegate.
Simply customize your header views for each group in your UITableView. Use viewforheaderinsection method to do that, and return UIButton for example. And if you tap that button, you should insert UITableViewCells in this selected group. I cant give you a code because I'm on windows now, but it's very simple to do.
UPD: look at this sample code by Apple: http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010139
When you receive the touch event for expanding your table cell, use the following calls in your table controller
[self.tableView beginUpdates];
[self.tableView endUpdates];
Though the animation block is empty, calling beginUpdates the endUpdates causes the table view to refresh cell layout and animate changes. This will cause your table view delegate to be re-queried - so ensure you implement the following method in your UITableViewDelegate (Usually you will have set this to be the UITableViewController):
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Ensure this method will return the appropriate expanded cell size after the touch event is received. The result will be a nice smooth cell expansion animation :)

Gesture Detection with more Items android

In my application, I save data in my sqlite db a variable number of notes composed of: (IDNote,title,body, date of creation, owner of note, icon_name). When i save and query the DB, my data are all ok, so my question is, Is possible to implement GestureDetection for moving and restore a single icon that correspond at a single note ? Because a found an example but all the icons are moved, but i want to move only one icon at time simulating all the gesture of the icons of Desktop computer.
Thanks in advance.
Marco
Set an OnTouchListener on the view that holds your icon. This listener is called when an ACTION_DOWN event on the view is caught.
Here register it somewhere as the 'currently dragged object'. Make sure onTouch() returns false, so the event is propagated further to your views parent.
This parent is responsible for checking whether a view has been registered as the 'currently dragged object'. Also, it has to call your Gesturedetector, which in onScroll will do the actual scrolling and moving.
This is just an idea, tell how it went!

Drag&Drop to put Items from List1 to List2

I search for a good and handy user interface to put items from list 1
to a empty list 2 with drag&drop. I need this function for Android 2.2.
Have anybody heard about this feature?
EDIT
I found this website and i think it is also interesting.
http://techdroid.kbeanie.com/2010/04/simple-drag-n-drop-on-android.html
Look also at the comments.
It may also be helpful to reference my simple Drag and Drop list. You can find here
if u don't getting then use this
An example of this in github https://github.com/mtparet/Drag-And-Drop-Android
It could help you.All contribution are welcome
Take a look at this sample that Drag and drop inside a List View..
Refer to the drag and drop API launched in API Version 11
http://developer.android.com/guide/topics/ui/drag-drop.html
A drag and drop operation starts when the user makes some gesture that you recognize as a signal to start dragging data. In response, your application tells the system that the drag is starting. The system calls back to your application to get a representation of the data being dragged. As the user's finger moves this representation (a "drag shadow") over the current layout, the system sends drag events to the drag event listener objects and drag event callback methods associated with the View objects in the layout. Once the user releases the drag shadow, the system ends the drag operation.

Categories

Resources