I'm working on a small project where you need to guess the picture behind the tiles. Currently everything is working, but I have no idea how I can check if someone clicked a tile.
I know I can do it with a button, but I want to be able to remove a tile when someone actually presses that tile. Is there a way to check if someone pressed somewhere on the screen or something?
You need to make your tile clickable, and then add a method on click event.
Add the following to your tiles in xml.
android:clickable="true"
android:onClick="TileClicked"
Then create a method in your activity
public void TileClicked(View v)
{
int clickedID = v.getId();
// Do something to the clicked tile .. e.g.
v.setVisiblity(View.INVISIBLE);
// or filter specific tiles
if(clickedID = R.id.myTile1)
{
// do something when tile 1 clicked
}
}
Alternatively, you can add the onclick listener in code and call the method there.
Related
Hi I'm implementing click listeners in the following way but after some time the methods and variables inside the listener's closure get the wrong values or something. Let me explain the implementation of the listener a little better a for loop creates the listener for a set of image views then later in the program the for loop is called a second time and it resets the listener methods and variables to different values. Everything works great for about 30 minutes but then for some reason, the listener's methods and variables start having the wrong values. Has anybody ever heard of this behavior or can tell me where I've gone wrong with the code? Keep in mind that the listener I'm about to paste here is just a small piece of a 1014 line class. I'm hoping somebody can spot How I'm implementing the listener wrongly and can give me some advice on how to "reset" the listener so that it's variables and values stay over time. Hopefully you can read the code without putting it in an editor but feel free to copy it for readability's sake Here is the code for the image view listener with comments.
//image views are held in an array
//set an image view in its imageview container
imgArr0[slotId1].invalidate()
imgArr0[slotId1].setImageDrawable(null)
//drw is not defined in this example
imgArr0[slotId1].setImageDrawable(drw)
/*if video or image id is set to image then set a listener for the image
*/
/*slotId1 is not defined in this example but it is simply a counter to iterate over the ImageView array
*/
if (videoOrImageId0[slotId1] == "image") {
//null any listeners that might be attached to the image view
imgArr0[slotId1].setOnClickListener(null)
//set or reset the listener
imgArr0[slotId1].setOnClickListener() {
`enter code here`//if the current config is portrait then set a new image image
if (currentConfig0 == "portrait0") {
act0.lrgImage0.invalidate()
act0.lrgImage0.setImageDrawable(null)
/*drw is not defined in this example but works fine in the production script
*/
act0.lrgImage0.setImageDrawable(drw)
}
--calmchess
ccc tv application with problem.
(https://i.stack.imgur.com/PjdbN.jpg)![enter image description here](https://i.stack.imgur.com/FaMnc.
I was able to partially solve this question by destroying all the image views and their associated click listeners then rebuilding those... However I don't consider this issue completely solved so if anybody can provide a better solution I'd love to hear it because rebuilding the images every few minutes has to be using a lot of unnecessary hardware resources.
--calmchess
I am facing following problem:
I got 4 Buttons over the screen, which can be dragged and move back to their original position when dropping the button.
To do so I want to read out the specific position of the button (on the specific device) by using the getX() and getY() function and writing the results into an array.
buttons[0][0] = button.getX();
buttons[0][1] = button.getY();
As I cannot use these functions in the onCreate or in the onStart (the position is always (0,0) cause the view is not instantiated yet I am thinking about how to read out the original position without any extra coding.
Thanks a lot
In your onCreate—though onResume may make more sense—get a reference to a view that is an ancestor of all the buttons (or just the root of the activity, which may be easiest) and then do this:
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
rootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// Read values here.
buttons[0][0] = button.getX();
buttons[0][1] = button.getY();
}
});
not sure what you mean by "extra coding" but you could use an OnTouchListener (or modify the one you're already using, if that's the case) to capture the position as soon as they touch the button, but before they drag it anywhere (I think that would be ACTION_DOWN).
This has the advantage of only being set when you need it to, skipping it when they don't actually drag the buttons, and not calling it for every layout pass, etc.
I'm working on a tile memory game.
A user can press any one of the tiles on a 9x9 board.
However, I want to limit the user to being only to press one at a time.
I've tried researching using a TableLayout, setting beforeAscendants=true, and having that try to manage where a user's click goes, but that proves tedious and saving all those coordinates seems inefficient to the possibility of an easier solution existing.
Right now, I call .setOnClickListener on each of those tiles and setting them to the same onClickListener, but there's no way to limit how many tiles can be pressed at once. I've tried A) setting a synchronized() section around the code inside the anonymous onClickListener code, B) having a boolean to set to true if one tile is already in the onClickListener code but it's simply ignored and C) banging my head against the wall, didn't help
Thanks!
This is the most straightforward and simplest way I can come out with. You should optimize the logic too, it's pretty lengthy here.
onClick(View v) {
switch (v.getId()) {
case R.id.tile1:
// Tile 1 is clicked
// Disable other tiles (try setClickable(false))
break;
...
default:
break;
}
}
Basically I have a chart with four different series, that by default shows all four of them at the same time. I want to allow the user to hide and/or show them again as he pleases by marking checkboxes.
I found a similar question here and followed both the advices given, but it didn't work. Here is what one of my checkboxes looks like now:
checkBox1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
multiRenderer.addSeriesRenderer(distanceRenderer);
// distanceRenderer.setColor(Color.parseColor("#990000")); //(making transparent method)
}
else{
//distanceRenderer.setColor(Color.TRANSPARENT); //(making transparent method)
multiRenderer.removeSeriesRenderer(distanceRenderer);
}
}
});
The idea is to when I uncheck the checkbox, I will hide that one renderer, when I check it again, it will be added again.
In the suggestions of the thread I linked to, they said to remove all renderers and then reenter the ones that aren't supposed to be hidden. I also tried this, but to no avail. The third option was simply to change the color to transparent whenever I wanted it to disappear, this also didn't work.
I debugged it, and it is getting in the conditions correctly, but nothing happens. Is there something I need to call that I've missed?
Thanks!
Edit: I don't know if this somehow helps, but I also tried to call the XYMultipleSeriesDataset's function repaint(). This caused an IllegalStateException, soooo.... Still no good...
Try this for hide purpose :
r.setColor(Color.TRANSPARENT);
mChartView.repaint();
where r is XYSeriesRenderer .
i work on an app and i have this problem.
Example:
i have 4 buttons in the main page:
APPLE,
STRAWBERRY,
CHERRY,
FAVORITES
if i click on APPLE STRAWBERRY OR CHERRY, it will open another activity with 3 more buttons
APPLE HISTORY,
MADE APPLE JUICE,
HOW TO GROW APPLE
Each of this buttons when clicked, open another activity with some data and text
I would like when someone made a long press on APPLE, STRAWBERRY or CHERRY to open a windows that ask if i want to save the selected item to FAVORITES, so it will copy the button inside the activity FAVORITES with the option to delete this choice in the future.
Can someone help me?
Thanks
You can set a listener for a long click to handle it
yourButton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
Inside this listener you can open a Dialog, where the user can choose if he wants to add it to Favorites or not. Furthermore save the users choice in SharedPreferences with a boolean or something which indicates that he made his choice. In your Favorite Activity read the SharedPreferences file and handle accordingly.
This could be done by adding Views programatically to have a dynamic setup. Or if the repertory is limited, like your example, set the Visibility of some Views / Buttons in your XML to gone and change it to Visible by reference to the users input.
You could save all your favorites items in a csv file, in a preference, in a xml file.. There are millions of way to achieve it.
I would personally use a sharedPreference to let the user edit it easily.
Is it possible to add an array or object to SharedPreferences on Android