I have a Button and when I click it it plays a Sound. How to use longpress to turn sound ( on and off ), so basically first tap should play a sound, second tap should stop it.
MainActivity
You can use onLongClickListener:
Button button;
button = findViewById(R.id.<your_button_id>);
button.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
//your code goes here
return false;
}
});
You have to add onLongClickListener to your button and implement the method onLongClick in your main activity.
for example:
public class MainActivity implements View.OnLongClickListener
after you implement the onLongClickListener you override the function onLongCLick
#Override
public boolean onLongClick(View view) {
return false;
}
And finally you need to set onLongClickListener to your button
btn.setOnLongClickListener(this);
In order to make the sound on and of just hold a global boolean variable which is called
private boolean isPlaying;
When it is long pressed once you set it to true, and when it is called again set it to false.
and stop your sound.
Related
I was thinking of creating a button that when tapped can disable the phone back button. I wanted to be able to enable it back again by pressing another button. However, the way that I found to disable the back button was with an override. Could somebody lend me a hand on how I could do that? Thanks!
What I tried was to put the override inside the button listener and onClick method, but it highland the override in red. I then tried putting the override in a different class and then calling the class when the the button is tapped.
I figured it out, but for anyone wondering the same thing, I'll post what I did. I implemented the following code inside MainActivity.class with the buttons:
private boolean backButtonEnabled = true;
#Override
public void onBackPressed() {
if (backButtonEnabled) {
super.onBackPressed();
}
}
public void disableBackButton() {
backButtonEnabled = false;
}
public void enableBackButton() {
backButtonEnabled = true;
}
My question may seem special but I need your help with how I create dialog boxes for each element of a recyclerview.
Indeed my fragment contains a recycle view with X elements, and I want that, when the user presses one of the elements for a long time, a dialog box opens and asks him "Are you sure you want to delete this element?" and two buttons "Yes" and "Cancel".
I have succeeded in doing so, but I especially want to know if my way of doing it is the right way, in order to avoid acquiring bad programming reflexes.
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
PlaceViewHolder Pholder =(PlaceViewHolder) holder;
Glide.with(mContext)
.load(mPhotoList.get(position))
.fitCenter()
.into(((PlaceViewHolder) holder).mPlace);
Pholder.mPlace.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
switchContext(mPhotoList.get(position));
}
});
Pholder.mPlace.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
new MaterialAlertDialogBuilder(mContext)
.setTitle(R.string.profile_photos_dialog_box_title)
.setMessage(R.string.profile_photos_dialog_box_message)
// Specifying a listener allows you to take an action before dismissing the dialog.
// The dialog is automatically dismissed when a dialog button is clicked.
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Continue with delete operation
}
})
// A null listener allows the button to dismiss the dialog and take no further action.
.setNegativeButton(android.R.string.no, null)
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
return true;
}
});
}
As you can see, every time I call onBindViewHolder I add an onClick listen and it is in this last one that I create the dialog box
Moreover, since my message is always the same, should I turn to a contextmenu, even if I find the format of this menu not very adapted to the choices and possible action?
I'm honestly not sure what the "best practice" may be, but I've done this before. How I've done it is, instead of creating the dialog and performing the action inside the longclick handler within onBindViewHolder, I create a custom listener interface so I can pass
relevant data back to the caller. Like this:
MyListListener
public interface MyListListener {
// Where "index" can be the index of the item, or the object represented by the list item at "index"
void onItemLongPressed(int index);
}
I'll pass that into the constructor of my custom RecyclerView.Adapter like this:
MyAdapter
public class MyAdapter extends ... {
private MyListListener listener;
public MyAdapter(MyListListener listener) {
this.listener = listener;
}
}
Then inside onBindViewHolder
MyAdapter / ViewHolder onBindViewHolder
Pholder.mPlace.setOnLongClickListener(new
View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
listener.onItemLongPressed(position);
}
});
And then my fragment or activity will implement the listener interface:
MyActivity
public class MyActivity extends ... implements MyListListener {
#Override
public void onItemLongPressed(int index) {
// Show the dialog and do things
}
}
I personally like this because it means my adapters are more isolated. They don't do much outside of showing a list of things. The logic behind those "things" ultimately lives elsewhere. It's easier to unit test this way as well, because you can mock the listener interface.
I'm trying to increase the number when I click long press button in android
like snapchat camera!
Use need to use OnLongClickListener and increment the number in it.
https://developer.android.com/reference/android/view/View.OnLongClickListener
You can use onLongClickListener for listening to long presses. Below is some code:
btnPlay.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
//increment the counter here
return true;
}
});
How can i detect a long press on the whole activity? since onLongClickListener is only for individual views.I want to run a method everytime the user longpress the screen
You can override your activity's dispatchTouchEvent() method. You also need a gesture detector in order to determine which motion events are 'long presses'. Put this into your activity:
final GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
// The code for when a long-press happens
}
});
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return super.dispatchTouchEvent(event);
}
Please note that I did not test the above code.
A long press is actually multiple registers of the key. So you could do a while loop on the input and as long as input is not NULL run the method you want. If I understood you correctly this should do the trick...
I am using the android developer tools in Eclipse, programming in Java, and I need to make an object move across the screen as long as a button is pressed. I've been doing research for hours, and I cannot find any methods to accomplish this. I've tried running threads, which often crash or seemingly don't execute. I've also tried an onClickListener which reads the button state and uses it to determine whether or not the button is still pressed. I'm currently using a while loop, but this just freezes the program. I believe that this is the best method, and I've tried to use Thread.sleep in order to limit the number of iterations per second, as I believe that this is the reason it is freezing. Am I on the right track or am I way off in left field? Here is a snippet of code:
rightButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
while(arg0.isPressed())
{
mover.updateCoordinates(1, 0);
}
}
});
Would you try this another method?
Firstly declare your button as class variable, declare a Handler and a Runnable:
private Button rightButton; // You will assign this in onCreate() method
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
#Override
public void run() {
if(rightButton.isPressed())
{
// If press state is pressed, move your item and recall the runnable in 100 milliseconds.
mover.updateCoordinates(1, 0);
mHandler.postDelayed(mRunnable, 100);
}
}
};
Then your button's onClickListener will looks like this:
rightButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0)
{
// Instead of performing a loop here, just call a runnable, do simple press state checking there.
mHandler.postDelayed(mRunnable, 100);
}
});
Create a loop that updates the views, waits, and calls itself after it finishes waiting. Within the loop, have an animation if statement with a boolean field that move on true and does not move on false. Have the onClick method toggle the boolean field's value.
Try:
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
// Start your animation here
} else if (e.getAction() == MotionEvent.ACTION_UP) {
// Stop your animation here
}
return false;
}
});
References:
MotionEvent
OnTouchListener
Alternatively, override onKeyUp and onKeyDown of the View class and check for the KEYCODE_ENTER KeyEvent