Monday, May 13, 2013

[android help] How to make alert dialog appear only once when dragging the seek bar


Ok so i when i pull the seek bar the alert dialog freaks out and comes up multiple times on the avd. How do i fix it so that when there is no user input in the ETCash the alert Dialog shows up on the screen ONCE. This is keep on prompting the user invalid input until the user inputs something in the edittext that i created. Any suggestion!!! HAVE tried everything but with no avail !!



import java.text.DecimalFormat;

import junit.framework.Assert;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends Activity {



private SeekBar sbCash;
private Button btnten,btntwenty,btnthirty;
private TextView tvShwProg,tvfinal;
private EditText etCash;


@Override
public void onCreate(Bundle savedInstanceState)
{


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

etCash = (EditText)findViewById(R.id.etCash);
sbCash = (SeekBar)findViewById(R.id.sbCash);
tvShwProg = (TextView)findViewById(R.id.tvShwProg);
tvfinal = (TextView)findViewById(R.id.tvFinal);
btnten = (Button)findViewById(R.id.btnten);
btntwenty = (Button)findViewById(R.id.btntwenty);
btnthirty = (Button)findViewById(R.id.btnthirty);




etCash.addTextChangedListener(new TextWatcher(){

public void afterTextChanged(Editable arg0)
{


}
public void beforeTextChanged(CharSequence arg0, int arg1,int arg2, int arg3)
{

}
public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {

try{
guiChanged();
}
catch(Exception e)
{
e.printStackTrace();
AlertDialog alert =errormessage();
alert.show(); //<<< show AlertDialog here
}
}


});


SeekBar.OnSeekBarChangeListener sbListener = new SeekBar.OnSeekBarChangeListener()
{
public void onStopTrackingTouch(SeekBar seekBar) {

}

public void onStartTrackingTouch(SeekBar seekBar) {}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
try{
guiChanged();
}
catch(Exception e)
{
e.printStackTrace();
}
}


};
sbCash.setOnSeekBarChangeListener(sbListener);

dosomethig();
dosomethig2();
dosomething3();




}

public AlertDialog errormessage()
{
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Enter a Number!!!!")
.setNegativeButton("Fix", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.show();
}


public void guiChanged()
{
try{
DecimalFormat fmt = new DecimalFormat ("0.00");

int CashMoney = sbCash.getProgress();

tvShwProg.setText("Current Percentage " +CashMoney+ "%");

double amount = Double.parseDouble(etCash.getText().toString());

double moneydue = (amount * (CashMoney/100.0));

double totalamount = (moneydue + amount);

tvfinal.setText("Tip " + fmt.format(moneydue)+ " Total "+ fmt.format(totalamount));
}
catch(Exception e)
{
e.printStackTrace();

AlertDialog alert = errormessage();
alert.show();
if(alert.isShowing())
{
alert.cancel();
}
}

}


public void dosomethig()
{

btnten.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

sbCash.setProgress(10);


}
});
}
public void dosomethig2()
{

btntwenty.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

sbCash.setProgress(15);
}


});
}
public void dosomething3()
{
btnthirty.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

sbCash.setProgress(20);

}
});
}


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...