How to Disable Button after first Click in android - java

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
}
}
});

Related

I Dont know how can I get bookmark button in fire base

iam already did add bookmark in the firebase but i dont know how can i get it
reference = FirebaseDatabase.getInstance().getReference("pagenumber");
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pagenumber = pdfView.getCurrentPage();
reference.setValue(pagenumber);
if (true) {
Toast.makeText(getApplicationContext(), "تم حفظ العلامة", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "لم يتم حفظ العلامة", Toast.LENGTH_SHORT).show();
}
}
});
load.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}

How to restrict increment button if stock is 10?

I am new to Android. I'm trying to restrict increments if the value of stock is 10. The count value is set in textcount which is a TextView. plus and minus are button for increment and decrement.
stockshow is textview and ITEM_QTY i added in getter setter method
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addtocart.setVisibility(View.VISIBLE);
count++;
textcount.setText(String.valueOf(count));
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count--;
textcount.setText(String.valueOf(count));
}
});
stockshow.setText(country.getITEM_QTY());
You'll want to make use of the setEnabled() method of View to ensure that the plus button can no longer be clicked should the stock count be set to 10. Likewise, you'll also want to disable the minus button once the stock count reaches 0. You could create a method like this:
private void checkBounds() {
plus.setEnabled(count < 10);
minus.setEnabled(count > 0);
}
And then use it in your listeners like so:
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addtocart.setVisibility(View.VISIBLE);
count++;
textcount.setText(String.valueOf(count));
checkBounds();
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count--;
textcount.setText(String.valueOf(count));
checkBounds();
}
});
You have to add an if condition in your increment method
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(count<10){
addtocart.setVisibility(View.VISIBLE);
count++;
textcount.setText(String.valueOf(count));
}
//You can add an else if you want to do something else{}
}
});

Change music with a variable in Android Studio

I want to change the song with a variable but can not do that.
Button btnSpeaker = (Button)findViewById(R.id.btnSpeaker);
btnSpeaker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer file = MediaPlayer.create(MainActivity.this , R.raw.file1);
file.start();
}
});
final MediaPlayer mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.file1);
Button btnSpeaker = (Button)findViewById(R.id.btnSpeaker);
final int[] resources={R.raw.file1,R.raw.file2};
btnSpeaker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int resource;
//if u meant getting resource variable from the file name
resource=getApplicationContext().getResources().getIdentifier("file2","raw",getPackageName());
//or
resource=resources[index];
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(getApplicationContext().getResources().openRawResourceFd(resource).getFileDescriptor());
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
});
Button btnSpeaker = (Button)findViewById(R.id.btnSpeaker);
btnSpeaker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mp;
mp=MediaPlayer.create(getApplicationContext(), getResources().getIdentifier(word.toLowerCase(),"raw",getPackageName()));
mp.start();
}
});

How to make wait MediaPlayer for other hit requests?

There is a play button(sound review), when I hit it plays, but when I hit several times, it plays multiply.
I wanted to stop this with other topic's codes, didn't help, any idea?
img_scream1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(mediaControl.isPlaying()==true){
mediaControl.pause();
}
else
{
mediaControl = MediaPlayer.create(java_sounds_scary.this, R.raw.hooray);
mediaControl.start();
}
}
});
Change your code to something like this:
boolean isPaused = true;
img_scream1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(isPaused){
mediaControl = MediaPlayer.create(java_sounds_scary.this, R.raw.hooray);
mediaControl.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
}
});
mediaControl.start();
isPaused = false;
}
else
{
mediaControl.pause();
isPaused = true;
}
}
});
Don't create the mediaControl instance every time(Which makes the song to load from begining). Instead, you can initialise mediaControl to null and create an instance of it when it is null
mediaControl =null;
img_scream1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View view){
if (mediaControl == null) {
mediaControl = MediaPlayer.create(java_sounds_scary.this, R.raw.hooray);
}
if (mediaControl.isPlaying() == true) {
mediaControl.pause();
} else {
mediaControl.start();
}
}
});

using onLongClickListener to change values quickly

I'm trying to create a set of buttons that will change a value up or down by 1 with a single tap, and change the value quickly by holding the button. I can't figure out how to get the value to change quickly. Here's what I have:
btPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mTempo=mTempo+1;
setTempo(mTempo + 1);
tvTempo.setText(Integer.toString(mTempo));
}
});
btPlus.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public void onLongClick(View view) {
mTempo = mTempo + 1;
setTempo(mTempo + 1);
tvTempo.setText(Integer.toString(mTempo));
}
});
btMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mTempo=mTempo-1;
setTempo(mTempo - 1);
tvTempo.setText(Integer.toString(mTempo));
}
});
btMinus.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public void onLongClick(View view) {
mTempo = mTempo - 1;
setTempo(mTempo - 1);
tvTempo.setText(Integer.toString(mTempo));
}
});
return rootView;
}
Thanks for your help.
Holding the button is something else than long clicking it. You need an onTouchListener
In the onPressed method check if the MotionEvent is UP or DOWN and then start/stop counting.
EDIT: Try Counting up in a Thread, something like that:
Final Thread t = new Thread(new Runnable(){
#Override public void run(){
while(!t.isInterrupted(){
setTempo(mTempo++);
}
}
switch (event){
case ACTION_DOWN:
t.start();
break;
case ACTION_UP:
t.interrupt();
break
}

Categories

Resources