d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Android Java Drag And Drop Without The Dragging?
Prev123Next
Add Reply New Topic New Poll
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 30 2015 06:50am
Quote (iGotThatFiyah @ Apr 29 2015 11: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)...


i think because you're setting height and width to wrap and not giving it an image, its height and width are probably 0. hence you can't actually click it. try changing the opacity and setting a height/width but it might not be able to handle a click if it's invisible.

This post was edited by carteblanche on Apr 30 2015 06:50am
Member
Posts: 2,059
Joined: Aug 16 2013
Gold: 0.80
Apr 30 2015 05:24pm
so i'm stuck here

in my selection screen, whenever the user clicks on the default "X" (top icon), the icons swap and vice versa.



i'm using a boolean flag to keep track of the previous icon that was clicked..

when the user is on the "board" screen, the icon that was last clicked/selected should appear first (player 1 always goes first) ...i've tried accessing the public variable by creating a new object but it only retrieves the global variable that was initialized, not the modified value so the boolean flag would always be true.



This post was edited by iGotThatFiyah on Apr 30 2015 05:28pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 30 2015 07:31pm
Quote
i'm using a boolean flag to keep track of the previous icon that was clicked..

when the user is on the "board" screen, the icon that was last clicked/selected should appear first (player 1 always goes first) ...i've tried accessing the public variable by creating a new object but it only retrieves the global variable that was initialized, not the modified value so the boolean flag would always be true.


i have no idea what you're talking about. if you can't access a variable, you're storing it in the wrong place. why are you even using this flag? i'm confused. i think Views have some property you can use to store data. i dont remember the name off hand. data, metadata, tag, or something like that.

/edit:
http://developer.android.com/reference/android/view/View.html
Quote
Tags

Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.


This post was edited by carteblanche on Apr 30 2015 07:36pm
Member
Posts: 2,059
Joined: Aug 16 2013
Gold: 0.80
Apr 30 2015 08:52pm
this is my code for the first Activity posted above...maybe it's a really bad design. i'm using the flag so the program knows which icon to swap next... the icons are swapped every time you press the top one. idk it's just a neat feature to have when the user wants to choose X over O, or vice versa and he can switch back by simply reclicking the top icon if he suddenly decides to stay with his initial selection. unless this is not the proper way to implement such functionality.

Code
private boolean isX = true;
View v;
View v1;
View back_view;
View start_view;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_versus);

v = findViewById(R.id.x_versus);
v.setOnClickListener(this);

v1 = findViewById(R.id.o_versus);
v1.setOnClickListener(this);

back_view = findViewById(R.id.backBtn);
back_view.setOnClickListener(this);

start_view = findViewById(R.id.startBtn);
start_view.setOnClickListener(this);

}

public void onClick(View view) {
// if player 1 button is clicked
if (view.getId() == R.id.x_versus) {
// if player 1's icon is X (by default it is X)
if (isX) {
// change X to O when clicked
view.setBackgroundResource(R.drawable.o_button);
// change player 2 icon to X
v1.setBackgroundResource(R.drawable.x);
// player 1 button is no longer X
isX = false;
// if player one button is O
} else {
// change O to X
view.setBackgroundResource(R.drawable.x_button);
// change player 2 icon to O
v1.setBackgroundResource(R.drawable.o);
// player 1 button is X
isX = true;
}
}
else if (view.getId() == R.id.backBtn) {
Intent intent = new Intent(this, PlayActivity.class);
this.startActivity(intent);
}
else if (view.getId() == R.id.startBtn) {
Intent intent = new Intent(this, StartActivity.class);
this.startActivity(intent);
}
}


the swap:



This post was edited by iGotThatFiyah on Apr 30 2015 09:00pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 30 2015 09:03pm
i'm still not clear what your problem is. you shouldn't have any problems accessing the flag. so where are you trying to access the flag from? if you're trying to access it from another activity, you should pass it to the activity via your intent.
Member
Posts: 2,059
Joined: Aug 16 2013
Gold: 0.80
Apr 30 2015 09:09pm
Quote (carteblanche @ Apr 30 2015 08:03pm)
i'm still not clear what your problem is. you shouldn't have any problems accessing the flag. so where are you trying to access the flag from? if you're trying to access it from another activity, you should pass it to the activity via your intent.


yes i'm trying to access the flag from another activity but i tried doing that, it would always return true. i haven't looked into Tags yet. maybe that would be an easier solution.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 30 2015 09:13pm
Quote (iGotThatFiyah @ Apr 30 2015 11:09pm)
yes i'm trying to access the flag from another activity but i tried doing that, it would always return true. i haven't looked into Tags yet. maybe that would be an easier solution.


show me the code where you're passing it via the intent and the code in your new activity where you read from it.

i dont see how a tag will help since the tag is on a view that isn't in your new activity.

This post was edited by carteblanche on Apr 30 2015 09:16pm
Member
Posts: 2,059
Joined: Aug 16 2013
Gold: 0.80
Apr 30 2015 09:20pm
Quote (carteblanche @ Apr 30 2015 08:13pm)
show me the code where you're passing it via the intent and the code in your new activity where you read from it.

i dont see how a tag will help since the tag is on a view that isn't in your new activity.


the above code is from the "VersusActivity" Class.

then I had the following in "StartActivity":

Code
VersusActivity va = new VersusActivity();
boolean isX = va.isX;


i'm not sure what you mean by passing the boolean via Intent. I've only used Intent for starting a new Activity. So is the boolean not necessary? I need to know the selected Icon by Player 1 so when the game starts, it knows which icon to use first, X or O..

This post was edited by iGotThatFiyah on Apr 30 2015 09:22pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 30 2015 09:24pm
Quote (iGotThatFiyah @ Apr 30 2015 11:20pm)
the above code is from the "VersusActivity" Class.

then I had the following in "StartActivity":

Code
VersusActivity va = new VersusActivity();
boolean isX = va.isX;


that's your problem. obviously if you create a new activity it's not going to be the same one you just used.

Quote
i'm not sure what you mean by passing the boolean via Intent. I've only used Intent for starting a new Activity.


that's exactly what you want to use. you want to pass the data when starting the new activity. read up on intents.

http://developer.android.com/guide/components/intents-filters.html
Member
Posts: 2,059
Joined: Aug 16 2013
Gold: 0.80
May 1 2015 12:45pm
is there another way to allow the user to click a box only once besides what i'm doing in the following code? or is my method good enough?
box1_clicked is a boolean initialized to false..basically a flag to indicate the box has been clicked.

Code
public void onClick(View view) {

if (view == box1_view && !box1_clicked) {
box1_clicked = true;
....


This post was edited by iGotThatFiyah on May 1 2015 12:45pm
Go Back To Programming & Development Topic List
Prev123Next
Add Reply New Topic New Poll