Start and stop recording with button click - java

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

Related

show alertdialog while i press the screen

i have a list with surveys and all those surveys contains a picture. when you finish a survey the app returns to the overview where i can see all completed and all uncompleted surveys and all rejected surveys.
What i want:
Now if i click long on the screen and the survey isnt rejected and is completed i want to get a preview of the picture as long as i press the screen and disappear if i release the screen.
The problems are:
How can i cancel or dismiss a AlertDialog without adding a positive
Button?
Is it possible to get the MotionEvent without an onTouch Listener and
just with longClickListener?
Here is my code with an onTouch and a longClick-listener and i want to eliminate one of them:
row.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
row.setOnLongClickListener(l -> {
// V3.0 only execute long click if the survey hasn't been declined
if (getContext() instanceof SurveyListPatientActivity && !erhebung.rejected() && !erhebung.isCompleted()) {
((SurveyListPatientActivity) getContext()).showRejectSurvey(erhebung);
}else if(getContext() instanceof SurveyListPatientActivity && !erhebung.rejected() && erhebung.isCompleted()){
AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
ImageView imageView = new ImageView(getContext());
byte[] bytes = Base64.decode(erhebung.getPic(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
imageView.setImageBitmap(bitmap);
imageView.setPadding(10,10,10,10);
adb.setView(imageView);
adb.create().show();
if (event.getAction() == MotionEvent.ACTION_UP){
// cancel AlertDialog
}
}
return true;
});
return false;
}
});
Thank you very much!
EDIT:
Whit the help of #brianoqr i changed my code into this.
Everything works fine but the Dialog dosent disappear.
row.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
ImageView imageView = new ImageView(getContext());
byte[] bytes = Base64.decode(erhebung.getPic(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
imageView.setImageBitmap(bitmap);
imageView.setPadding(10,10,10,10);
adb.setView(imageView);
dialog = adb.create();
dialog.show();
isLongPressed = true;
return true;
}
});
row.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
System.out.println("1");
if (event.getAction() == MotionEvent.ACTION_UP) {
System.out.println("2");
if (isLongPressed) {
dialog.cancel();
System.out.println("canceld");
isLongPressed = false;
}
}
return false;
}
});
I probably would do it differently, but to answer the question how to cancel a dialog with out a button:
AlertDialog dialog = adb.create();
dialog.show()
if (event.getAction() == MotionEvent.ACTION_UP){
dialog.dismiss()
}
The code you have written will not work as you want, the onlongpress is the leading action, you can structure your code something like in this ticket: Android - Detect End of Long Press
And your dialog will then need to be in a different scope to allow for automatic dismissal
EDIT
row.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
if(!isLongPressed){
isLongPressed = true;
AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
ImageView imageView = new ImageView(getContext());
byte[] bytes = Base64.decode(erhebung.getPic(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
imageView.setImageBitmap(bitmap);
imageView.setPadding(10,10,10,10);
adb.setView(imageView);
dialog = adb.create();
dialog.show();
}
return true;
}
});
row.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
System.out.println("1");
if (event.getAction() == MotionEvent.ACTION_UP) {
System.out.println("2");
if (isLongPressed) {
dialog.cancel();
System.out.println("canceld");
isLongPressed = false;
}
}
return false;
}
});

setPressed is not working

I have this in my code and the .setPressed doesn't work:
Button btndesligado = (Button) findViewById(R.id.button12);
btndesligado.setOnClickListener(new OnClickListener() { //Botão para pôr silêncio!
#Override
public void onClick(View v) {
btndesligado.setPressed(true);
som = false;
vibrar = false;
}
});
What is wrong? It doesn't give me any error, it just doesn't work when I open it and click it. It was suposed to be pressed after I press once.
Android is changing the setPressed both before and after your onClickEvent
so change your code this code
btndesligado.setOnClickListener(new OnClickListener() { //Botão para pôr silêncio!
#Override
public void onClick(View v) {
btndesligado.setPressed(true);
som = false;
vibrar = false;
}
});
to this
btndesligado.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// show interest in events resulting from ACTION_DOWN
if (event.getAction() == MotionEvent.ACTION_DOWN) return true;
// don't handle event unless its ACTION_UP so "doSomething()" only runs once.
if (event.getAction() != MotionEvent.ACTION_UP) return false;
btndesligado.setPressed(true);
som = false;
vibrar = false;
return true;
}
});
It seems nothing wrong with your codes, It's working as you did, It sets the View's internal state to "pressed", or false to reverts the View's internal state from a previously set "pressed" state. And you've put that inside onClick of button means it acts only after you press the button,
If you want it pressed or revert then do like this,
Button btndesligado = (Button) findViewById(R.id.button12);
btndesligado.setPressed(true);
btndesligado.setOnClickListener(new OnClickListener() { //Botão para pôr silêncio!
#Override
public void onClick(View v) {
som = false;
vibrar = false;
}
});
Try this way.
btndesligado.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btndesligado.setPressed(true);
som = false;
vibrar = false;
}
});

Android custom double tap location

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

Android. How to catch the moment when keyboard gets hidden?

I'm trying to catch the event of removing keyboard from the screen and i'm using the following code:
searchTextField.setOnEditorActionListener(new OnEditorActionListener()
{
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
System.out.println("ACTION ID " + actionId);
if(actionId == EditorInfo.IME_ACTION_DONE)
{
System.out.println("ACTION DONE!!!!!!!!!!");
return true;
}
return false;
}
searchTextField.setOnFocusChangeListener( new View.OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
System.out.println("HAS FOCUS");
else
System.out.println("FOCUS LOST");
}
});
But unfortunately it doesn't work. onEditorAction just never called, no matter if i start editing or finish. Regarding onFocusChange method it's called just for the very first time when the keyboard goes up. When the keyboard goes down or when it goes up for the second time it's not called. Can anyone explain what am i doing wrong?
I used GlobalLayoutListener on the activity rootView for checking that whether keyboard is hidden or visible:
It works as follows:
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
... do something here
}
}
});

Android: Prompt user to save changes when Back button is pressed

I have an activity that contains several user editable items (an EditText field, RatingBar, etc). I'd like to prompt the user if the back/home button is pressed and changes have been made that have not yet been saved. After reading through the android documentation, it seems like this piece of code should go in the onPause method. I've tried putting an AlertDialog in the onPause however the dialog gets shown and then immediately tears down because nothing is there to block the pause from completing.
This is what I've come up with so far:
#Override
protected void onPause() {
super.onPause();
AlertDialog ad = new AlertDialog.Builder(this).setMessage(
R.string.rating_exit_message).setTitle(
R.string.rating_exit_title).setCancelable(false)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// User selects OK, save changes to db
}
}).setNeutralButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// User selects Cancel, discard all changes
}
}).show();
}
Am I on the right track or is there another way to accomplish what I'm trying to do here? Any help would be great!
You're not quite on the right track; what you should be doing is overriding onKeyDown() and listening for the back key, then overriding the default behavior:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
return true;
}
return super.onKeyDown(keyCode, event);
}
If you're only supporting Android 2.0 and higher, they've added an onBackPressed() you can use instead:
#Override
public void onBackPressed() {
// do something on back.
return;
}
This answer is essentially ripped from this blog post. Read it if you need long presses, compatibility support, support for virtual hard keys, or raw solutions like onPreIme() etc.
What do you think about this approach ..
private void exit(){
this.finish();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setTitle("Message");
alertbox.setMessage("Quit ??? ");
alertbox.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
exit();
}
});
alertbox.setNeutralButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
alertbox.show();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
Here is some sample code:
#Override
public void onBackPressed() {
if (isDirty) {
new AlertDialog.Builder(this)
.setTitle("You have made some changes.")
.setMessage("Would you like to save before exiting?")
//.setNegativeButton(android.R.string.no, null)
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
MyActivity.super.onBackPressed();
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
if (Save())
MyActivity.super.onBackPressed();
}
}).create().show();
}
else {
MyActivity.super.onBackPressed();
}
}
I am doing the same thing that you do. I have one activity with a customer information (EditText for name, last name, email, ..., you know, EditText everywhere) and I used AsyncTask for that, and it works wonderfully.
#Override
public void onBackPressed()
{
// start async task for save changes.
new GuardandoContactoHilo().execute()
}
public void VolverAtras()
{
super.onBackPressed();
}
public class GuardandoContactoHilo extends AsyncTask< Void, Void, Void>
{
private ProgressDialog saving;
#Override
protected void onPreExecute()
{
super.onPreExecute();
saving = new ProgressDialog(cont);
saving.setProgressStyle(ProgressDialog.STYLE_SPINNER);
saving.setMessage("Saving customer ...");
saving.show();
}
#Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
saving.dismiss();
VolverAtras(); // go back !!
}
#Override
synchronized protected Void doInBackground(Void... arg0)
{
// do what you want to do before come back ! for example, save data ;)
return null;
}
}

Categories

Resources