I'm currently creating an app for SmartGlasses. It has an SDK that has some basic swipe Gesture and a custom Gesture Detector. I want to integrate this with my current code.
picture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (touches < 4) {
v.setBackgroundResource(picList.get(touches));
touches++;
} else {
touches = 0;
loop = 1;
v.setBackgroundResource(picList.get(touches));
}
}
});
I'm using this to go in a loop of images.
The SDK, one of its functions, is SlideRight(). And also a onTouchEvent().
#Override
public boolean onTouchEvent(MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) {
return true;
}else {
return false;
}
}
---------
#Override
public boolean onSlideRight(boolean b) {
return false;
}
How can I call this function and get it to do at least a Log.d when swiping right?
Related
I am using this code but its not working i am still able to click more than 2 times.
accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
accept.setEnabled(false);
if (dialog1 != null && dialog1.isShowing()) {
dialog1.dismiss();
}
handler.removeCallbacks(runnable);
}
});
its not showing any error but not working as desired.
boolean run = true;
accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(run) {
run = false;
//your code
}
}
});
I created 2 objects of class PlaceAutocompleteFragment and set OnPlaceSelectedListener on both.
placePickup = (PlaceAutocompleteFragment)getFragmentManager().findFragmentById(R.id.place_source);
placePickup.setOnPlaceSelectedListener(this);
placeDrop = (PlaceAutocompleteFragment)getFragmentManager().findFragmentById(R.id.place_target);
placeDrop.setOnPlaceSelectedListener(this);
Now, I want to find a way to identify which object has invoked onPlaceSelected() method.
#Override
public void onPlaceSelected(Place place) {
latLng = place.getLatLng();
if (invoking object is placePickup){
do this;
}
if (invoking object is placeDrop){
do this;
}
}
you can maintain a boolean variable
when user clicks on pickUp make that variable true.
boolean isPickUp=false;
onCLlick of pickUp where you load PlaceAutoComplete fragment make it true;
and onCLick of drop make it false;
if (isPickUp){
do pickUp related work;
}
else{
do drop related work here;
}
Use Inline call like below.
placePickup = (PlaceAutocompleteFragment)getFragmentManager().findFragmentById(R.id.place_source);
placePickup.setOnPlaceSelectedListener(new PlaceSelectionListener() {
#Override
public void onPlaceSelected(Place place) {
Log.e(TAG,"Pick Up");
}
#Override
public void onError(Status status) {
}
});
placeDrop = (PlaceAutocompleteFragment)getFragmentManager().findFragmentById(R.id.place_target);
placeDrop.setOnPlaceSelectedListener(new PlaceSelectionListener() {
#Override
public void onPlaceSelected(Place place) {
Log.e(TAG,"Drop");
}
#Override
public void onError(Status status) {
}
});
I am trying to make a Helicopter-like game (as you can probably tell, I'm using "Rocket Man" instead of a helicopter). I'm new to Android programming and to Stack Overflow, so don't pick apart my code too much (please). Anyway, right now I'm just trying to get the rocketman to move up while touch is held down, and move down while touch is released.
It isn't working out very well.
I'm using a Moto XT886 for testing, (in case that's relevent). When I run the application, it starts up and loads the MainActivity perfectly well. When I touch the screen the first time (boolean start == true), the text views are hidden, as they are supposed to. However, on the second, third, fourth and all the rest of the times I touch the screen (Start == false), nothing happens. There are no errors in Log Cat, the code doesn't seem to recognize the touch.
I'm pretty sure that it has something to do with those while loops, but I'm using this application mainly to learn Android Java and I don't know enough (yet) to recognize that I'm doing something wrong. Are you able to put a while loop with the conditions as the MotionEvent of an onTouchListner? Or is it something else?
Either way, I appreciate the help, this is an awesome website (this is the first question I couldn't find already asked on Stack Overflow, and for all I know, mine could've already been answered)!
Here's the code to my MainActivity, if you need the .xml code as well, (though I'm pretty sure that's unnecessary) just say the word.
public class MainActivity extends ActionBarActivity {
boolean Start;
RelativeLayout rLayout;
TextView gameTitle;
TextView clickStart;
TextView highScore;
Handler handler;
ImageView rocketMan;
float Y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
rLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (Start == true) {
gameTitle.setVisibility(View.GONE);
clickStart.setVisibility(View.GONE);
highScore.setVisibility(View.GONE);
Start = false;
}
if (Start = false) {
while (event.getAction() == MotionEvent.ACTION_DOWN) {
Y = -7;
new Thread(new Runnable() {
#Override
public void run() {
while (true) {
try {
Thread.sleep(100);
handler.post(new Runnable() {
#Override
public void run() {
HeliMove();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
while (event.getAction() != MotionEvent.ACTION_DOWN) {
Y = 7;
new Thread(new Runnable() {
#Override
public void run() {
while (true) {
try {
Thread.sleep(100);
handler.post(new Runnable() {
#Override
public void run() {
HeliMove();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
return true;
}
});
}
private void initialize() {
rLayout = (RelativeLayout) findViewById(R.id.mainlayout);
Start = true;
gameTitle = (TextView) findViewById(R.id.rocketmanTitle);
clickStart = (TextView) findViewById(R.id.tapToStart);
highScore = (TextView) findViewById(R.id.highScore);
handler = new Handler();
rocketMan = (ImageView) findViewById(R.id.rocketMan);
}
private void HeliMove() {
rocketMan.setY(rocketMan.getY() + Y);
}
}
Thanks a million!
I'm making a simple android app for recording sound. I have a startRecording() and stopRecording() methods. Now I implemented a ToggleButton named "Touch to record" so as you can already imagine, when the button is checked, you have to hold the record button to record sound and when the button is on "off" you have to click to start and then click to stop.
This is the current code:
touchToRecord.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked)
{
recBtn.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn_pressed);
chTimer.start();
chTimer.setTextColor(Color.GREEN);
startRecording();
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn);
chTimer.stop();
stopRecording();
nameAlert();
}
return true;
}
});
}
else
{
//onClickListener
}
}
});
Now I'm not sure how to make the onClickListener. If I try to do it like this:
recBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
It won't work because it underlines setOnClickListener and says:
The method setOnClickListener (View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){})
Also, one more thing after I get this working; how do I check if the method is already running with if statement? I want to do something like this:
if (startRecording == isRunning)
{
stopRecording();
}
You could try android toggle button. Please see more information on:
http://developer.android.com/guide/topics/ui/controls/togglebutton.html
Solved the problem!
I avoided using onClickListener by setting onClick in XML and then just creating the method. Here's the code:
public void recordBtnClick(View v){
final ToggleButton touchToRecord = (ToggleButton)findViewById(R.id.tBtn1);
final ImageButton recBtn = (ImageButton) findViewById(com.whizzappseasyvoicenotepad.R.id.recButton);
if (touchToRecord.isChecked() == false)
{
if (recorder == null)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn_pressed);
chTimer.start();
chTimer.setTextColor(Color.GREEN);
startRecording();
}
else if (recorder != null)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn);
chTimer.stop();
stopRecording();
nameAlert();
}
}
else
{
//DO NOTHING
}
}
I'm currently creating a custom double tap using the onClickListener with the following code:
newImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
counterTap++;
Timer t = new Timer("Double tap counter");
t.schedule(new TimerTask() {
#Override
public void run() {
counterTap = 0;
}
}, 0, 300);
if(counterTap >= 2) {
newImage.setVisibility(FrameLayout.GONE);
counterTap = 0;
}
}
});
The problem I'm facing is as follows:
Whenever I tap the ImageView the event does fire. However, the second time I tap the ImageView, the above code only executes when clicking on the exact same position on the ImageView as before.
Rather use a onTouchListener. In the touch listener in the onTouch method you can return false the first time you tapped which means the event was not handled, the second time you return true and the touch listener will handle the event as finished. See my example below, you can use a similar method
new OnTouchListener() {
boolean doubleTap = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
if (!doubleTap) {
doubleTap = true;
return false;
} else {
return true;
}
}
}
};
this might solve your issue.
EDIT : This is a better opotion
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onDown(MotionEvent e) {
return true;
}
// double tap event
#Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}
}
This question might also help you get to the best answer