d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Android Java Drag And Drop Without The Dragging?
123Next
Add Reply New Topic New Poll
Member
Posts: 2,059
Joined: Aug 16 2013
Gold: 0.80
Apr 29 2015 09:49am
so basically i have an image and i want to be able to click on an area of the screen and the image will be dropped there. the only time my finger has contact with the screen is when i drop the image to the target location.

how do i achieve this?

EDIT:

i guess i can just use an OnClickListener here...
so i'm creating a simple tic tac toe app. by default i want the the icon, X or O, to be at the top left hand corner of the screen. So all the user has to do is press inside one of the grid boxes and the image should stay put. then the X would become an O and vice versa at the corner. so no dragging involved. so im guessing my main problem is not even which listener to use but to create a new image every time the user presses on the screen.

This post was edited by iGotThatFiyah on Apr 29 2015 10:05am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 29 2015 01:28pm
why would you create a new image? just have 9 images on the board and change the display when clicked.
Member
Posts: 2,059
Joined: Aug 16 2013
Gold: 0.80
Apr 29 2015 04:48pm
Quote (carteblanche @ Apr 29 2015 12:28pm)
why would you create a new image? just have 9 images on the board and change the display when clicked.


so do something like this?



so initially, i would just set all the images (views) to INVISIBLE using the setVisibility() method.

currently, i have one imageview as the board. i'm thinking of getting the absolute values (area) of each square so the program knows where and when the particular square is clicked. is there a better way to go about this ?

This post was edited by iGotThatFiyah on Apr 29 2015 04:52pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 29 2015 05:05pm
Quote (iGotThatFiyah @ Apr 29 2015 06:48pm)
so do something like this?

http://i.imgur.com/cPxZyu5.png

so initially, i would just set all the images (views) to INVISIBLE using the setVisibility() method.

currently, i have one imageview as the board. i'm thinking of getting the absolute values (area) of each square so the program knows where and when the particular square is clicked. is there a better way to go about this ?


and do what with it? you gonna draw the image on the coordinates? if you were making a 100x100 minesweeper game, then i'd probably go that route to avoid creating 10,000 controls. but if you're gonna add a control, might as well have the control to start and just use an onClick listener.

offhand i dont know if the click event will fire if it's invisible. just set the background to transparent or same colour as your background or whatever.

This post was edited by carteblanche on Apr 29 2015 05:06pm
Member
Posts: 2,059
Joined: Aug 16 2013
Gold: 0.80
Apr 29 2015 05:45pm
Quote (carteblanche @ Apr 29 2015 04:05pm)
and do what with it? you gonna draw the image on the coordinates? if you were making a 100x100 minesweeper game, then i'd probably go that route to avoid creating 10,000 controls. but if you're gonna add a control, might as well have the control to start and just use an onClick listener.

offhand i dont know if the click event will fire if it's invisible. just set the background to transparent or same colour as your background or whatever.


when the user touches the screen, the particular space that is touched is filled...how do i achieve that without telling the program which part of the view is being touched?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 29 2015 05:56pm
Quote (iGotThatFiyah @ Apr 29 2015 07:45pm)
when the user touches the screen, the particular space that is touched is filled...how do i achieve that without telling the program which part of the view is being touched?


my question is how are you filling it?
Member
Posts: 2,059
Joined: Aug 16 2013
Gold: 0.80
Apr 29 2015 08:01pm
Quote (carteblanche @ Apr 29 2015 04:56pm)
my question is how are you filling it?


so i have something like this in onCreate:

Code
x1_view.setVisibility(View.INVISIBLE);
x2_view.setVisibility(View.INVISIBLE);
x3_view.setVisibility(View.INVISIBLE);
x4_view.setVisibility(View.INVISIBLE);
x5_view.setVisibility(View.INVISIBLE);
x6_view.setVisibility(View.INVISIBLE);
x7_view.setVisibility(View.INVISIBLE);
x8_view.setVisibility(View.INVISIBLE);
x9_view.setVisibility(View.INVISIBLE);


then i was thinking since the board is the only thing that is visible initially, depending on where the user clicks in the board, the x/o will become visible:

Code
@Override
public void onClick(View view) {
if (view.getId() == R.id.board) {
//switch case or if statement depending on location of the board being clicked/touched.
x1_view.setVisibility(View.VISIBLE);
}

}


i might be totally complicating things.

or i can split the board into 9 individual ImageButtons and then i don't evne have to worry about the location. that will save me a lot of headache.

is there a better way to approaching this?

This post was edited by iGotThatFiyah on Apr 29 2015 08:05pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 29 2015 08:07pm
so if you have 9 images, why are you trying to grab coordinates? instead of having a listener on the board, have a listener on the 9 views. when you click the view, the view tells itself to turn into an O/X
Member
Posts: 2,059
Joined: Aug 16 2013
Gold: 0.80
Apr 29 2015 08:48pm
Quote (carteblanche @ Apr 29 2015 07:07pm)
so if you have 9 images, why are you trying to grab coordinates? instead of having a listener on the board, have a listener on the 9 views. when you click the view, the view tells itself to turn into an O/X


i see what you mean now. thanks a lot.

instead of assigning those 9 with images initially i'll leave the android:background attribute blank. then the clicklistener will determine if they become X or O.

currently i have something like this in my xml layout:

Code
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/board"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:background="@drawable/tictactoeboard" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/x"
android:scaleType="center"
android:id="@+id/x1_image"
android:layout_alignTop="@+id/board"
android:layout_alignLeft="@+id/board"
android:layout_alignStart="@+id/board" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/x"
android:scaleType="center"
android:layout_alignTop="@+id/board"
android:layout_centerHorizontal="true"
android:id="@+id/x2_image" />

....
....


This post was edited by iGotThatFiyah on Apr 29 2015 08:48pm
Member
Posts: 2,059
Joined: Aug 16 2013
Gold: 0.80
Apr 29 2015 09:08pm
i guess i need an image for each box that matches the board b/c onclick doesn't respond to an image with no "android:background" (basically no image)...

cropping 9 images of the board in photoshop to match the board is the only solution.

This post was edited by iGotThatFiyah on Apr 29 2015 09:09pm
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll