annoying toast keeps showing how to stop it - android

Every time I pressed enter the toast keeps popping out even if it should not come out...
take a lot at my code:
etAddMove.setOnKeyListener(new OnKeyListener() {
#SuppressLint("NewApi")
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_ENTER && event.getRepeatCount() == 0){
String ssmoveName = etAddMove.getText().toString();
int x = ssmoveName.length() - 1;
if (ssmoveName.isEmpty() || Character.isWhitespace(ssmoveName.charAt(0)) || Character.isWhitespace(ssmoveName.charAt(x))) {
Toast.makeText(ListMovingNames.this,
"Please enter a valid name! Avoid giving a blank name or white space at the beginning or end of the name",
Toast.LENGTH_LONG).show();
}else{
try {
SQLHandler check = new SQLHandler(ListMovingNames.this);
check.open();
String scheck = check.checkMove(ssmoveName);
check.close();
if (!scheck.equals(ssmoveName)) {
Toast.makeText(ListMovingNames.this, "Move name already exist please give a different name", Toast.LENGTH_LONG).show();
} else{
SQLHandler entry = new SQLHandler(ListMovingNames.this);
entry.open();
entry.createMove(ssmoveName);
entry.setTodo(ssmoveName);
entry.close();
Intent i = new Intent(getApplicationContext(), StartMoving.class);
i.putExtra("moveName", ssmoveName);
startActivity(i);
}
} catch (Exception e) {
// TODO Auto-generated catch block
SQLHandler entry = new SQLHandler(ListMovingNames.this);
entry.open();
entry.createMove(ssmoveName);
entry.setTodo(ssmoveName);
entry.close();
Intent i = new Intent(getApplicationContext(), StartMoving.class);
i.putExtra("moveName", ssmoveName);
startActivity(i);
}
}
return true;
}
return onKeyDown(keyCode, event);
}
});
how can you make this guy stop if I don't want it to show...
EDIT
I think I know why this is happening, It must be because each time i press enter keyup and keydown is initialize thats why the first initialization is keydown which calls the condition and returns false and when keydown is called the condition is called again which will return true, making the toast display. thats what i think so far...

Try with following if not work then come to know.
Toast toast;
toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();
if(Yourcondition)
{
toast.show();
}
else
{
//nothing to display
}

Finally solve this shit. As I was suspecting the reason why toast keeps showing is that everytime the enter key is press, the keydown and keyup is called that's why the process is called twice making the toast appear because the condition to show the toast is meet when the process is called the second time.
So to avoid that KeyEvent.ACTION_DOWN must be present.
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ( (event.getAction() == KeyEvent.ACTION_DOWN ) && (keyCode== KeyEvent.KEYCODE_ENTER) )
{
// do something here
}
return true;
}

Use this:
Toast toast;
toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT);
if(Yourcondition)
{
toast.show();
}
else
{
//nothing to display
}
In the last answer, you are using .show method, when you are creating the toast. It will show the even, if the statement is true or not.
So, remove .show from
toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();
this statement.
This might help.

Related

Display toast for empty fields when user submits data

I have an activity in which there are three EditTexts: First Name, Middle Name, Last Name, and a Submit button. If the user submits with a field blank, a different toast appears for different field. Here's is an example when button will be clicked!:
I'll assume you can figure out how to use a button listener yourself. Inside that button listener, you can use this code:
// create EditText references
EditText etFirstName = (EditText)findViewById(R.id.firstName);
EditText etMiddleName = (EditText)findViewById(R.id.middleName);
EditText etLastName = (EditText)findViewById(R.id.lastName);
// boolean variables that each represent whether or not the each field is blank
// if the EditText length is 0, it's true (blank); otherwise, it's false (filled)
boolean firstNameBlank = etFirstName.length() == 0;
boolean middleNameBlank = etMiddleName.length() == 0;
boolean lastNameBlank = etLastName.length() == 0;
if (firstNameBlank && middleNameBlank && lastNameBlank) {
// toast all three blank
}
else if (firstNameBlank && middleNameBlank) {
// toast just first and middle are blank
}
else if (firstNameBlank && lastNameBlank) {
// toast just first and last are blank
}
else if (middleNameBlank && lastNameBlank) {
// toast just middle and last are blank
}
else if (firstNameBlank) {
// toast just first is blank
}
else if (middleNameBlank) {
// toast just middle is blank
}
else if (lastNameBlank) {
// toast just last is blank
}
else {
// none are blank
}
Try this way
EditText e = (EditText)findViewById(R.id.EDITTEXT));
if(e.getText().length == 0){
//Show Toast
}else{
//continue your code
}
implement onClick() event of your submit button as below:
#Override
public void onClick(View view) {
super.onClick(view);
switch (view.getId()) {
case R.id.btnSubmit:
if(edtFirstName.getText().toString().length() < 1){
Toast.makeText(this, "please fill all boxes.",Toast.LENGTH_LONG).show();
}else if(edtSecondName.getText().toString().length() < 1){
Toast.makeText(this, "please fill middle name and last name.",Toast.LENGTH_LONG).show();
} else if(edtLastName.getText().toString().length() < 1){
Toast.makeText(this, "please fill last name.",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "please wait...processing.",Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}

Handle back button, in MonoGame on Android

i have a problem with handling back button in my game on android, developed on MonoGame with MonoDroid. I can't catch back button click event.
I tried:
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
LoadingScreen.Load(ScreenManager, true, null, new BackgroundScreen(),
new LevelSelectScreen());
}
And on main Activity
public override bool OnKeyUp(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back)
{
...
return true;
}
return base.OnKeyDown(keyCode, e);
}
public override void OnBackPressed()
{
...
Toast.MakeText(this, keyCode + " key! + " + keyCode.ToString(), ToastLength.Short).Show();
}
public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back)
{
...
return true;
}
return base.OnKeyDown(keyCode, e);
}
Nothing helps, on WP first variant works perfectly
YEAH!
I found solution:
When you call
GamePad.GetState(PlayerIndex.One)
in Android GamePadState cleared
public static GamePadState GetState(PlayerIndex playerIndex)
{
var instance = GamePad.Instance;
var state = new GamePadState(new GamePadThumbSticks(), new GamePadTriggers(), new GamePadButtons((Buttons)instance._buttons), new GamePadDPad());
instance.Reset(); // <- CLEAR BUTTONS
return state;
}
So, do not call GetState in Android several times.

how to get edittext values to be compared with database values in dialog box

I have a dialog box which asks the user for login credentials. I want to compare that password with the database value earlier stored for authentication. M not able to do that.
can anybody pls help.
public void onClick(View v) {
// TODO Auto-generated method stu
Toast.makeText(context, "Button Clicked", Toast.LENGTH_SHORT).show();
String existPin=exist_pin.getText().toString();
String confirmPin=new String();
prefEditor.putString("ExistPin", existPin);
prefEditor.commit();
WayDataBase way=new WayDataBase(LoginActivity.this);
String storedPin=way.getPin(confirmPin);
Toast.makeText(context, "Data from database", Toast.LENGTH_SHORT).show();
if(existPin.equals(storedPin))
{
d2.dismiss();
Intent i=new Intent(getApplicationContext(),ApplicantPage1.class);
startActivity(i);
}
else
{
Toast.makeText(context, "Invalid credentials", Toast.LENGTH_SHORT).show();
}
}
Better to compare within database, simply pass entered pin in parameter within getPin function and check and return Boolean instead String something like below:
public boolean isValidPin(String pin) {// within database.
boolean type = false;
Cursor c = myDataBase.rawQuery(
"select * from table where user_pin='" + pin+ "'", null);
if (c.getCount() > 0) {
c.moveToFirst();
type = true;
}
c.close();
return type;
}
Fetch pin from box and pass to function and later put condition and check boolean.
String pin=exist_pin.getText().toString();
boolean flag=isValidPin(String pin);
if(flag)
{
d2.dismiss();
Intent i=new Intent(getApplicationContext(),ApplicantPage1.class);
startActivity(i);
}
else
{
Toast.makeText(context, "Invalid credentials", Toast.LENGTH_SHORT).show();
}

Disabling touchlistener working in one context but not in another context

i have an application which has this layout. now when the user touches some button and releases his finger from that button, the key is entered into the textbox. mean while it also pronounces that letter to the user. saying "B is entered". so it is during ACTION_UP. during this event handling i need to prevent touch events even if by accident the user touches them.
i am disabling the touchlistener during touch events(in UP state), but it is not happening.The touch events are getting queued and i am not getting how to disable the touch event while the current one is being handled.but i could able to accomplish it during the hardkeys event handling. i could not understand this weird behavior.
here is my code
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
String s = null;
s = ((Button) v).getText().toString().trim();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
b_prev=(Button)v;
//while(tts.isSpeaking());
tts.speak(s, TextToSpeech.QUEUE_FLUSH, null);
break;
case MotionEvent.ACTION_UP:
fun(false); //disabling the touch event for all buttons
for(int i=0;i<mybtn.length;i++)
{
if(isPointInsideView(event.getRawX(),event.getRawY(),mybtn[i]))
{
s = (mybtn[i]).getText().toString().trim();
btn=mybtn[i];
break;
//et.setText("up android" + s, TextView.BufferType.EDITABLE);
}
}
if(btn.equals(mybtn[27])) //clear button pressed
{
//do something
}
else if(btn.equals(mybtn[26])) //space button
{
//do soemthing
}
else
{
//do soemthing
}
fun(true); //enabling touch event for all buttons
break;
case MotionEvent.ACTION_MOVE:
for(int i=0;i<mybtn.length;i++)
{
if(isPointInsideView(event.getRawX(),event.getRawY(),mybtn[i]))
{
s = (mybtn[i]).getText().toString().trim();
btn=mybtn[i];
break;
//et.setText("down android" + s, TextView.BufferType.EDITABLE);
}
}
break;
}
return true;
}
void fun(boolean flag)
{
int i;
for(i=0;i<mybtn.length;i++)
{
if(flag==true)
mybtn[i].setOnTouchListener(this);
else
mybtn[i].setOnTouchListener(null);
mybtn[i].setEnabled(flag);
}
}
Even though i am disabling the touch events using fun(), they are getting queued and executing after the completion of current touch event. so please tell me what i need to do to fix this problem.
i have made an observation that i am using two hardkeys in confirmation events.during hard key events the disabling/enabling of layout buttons is working fine. But it is not working same in the above scenario. what could be the problem with it? i am attaching that code as well. look at it.why this is happening i am not getting.
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH
&& event.getRepeatCount() == 0)
{
String s = et.getText().toString().trim();
fun(false); //disable touch events
if(pass==1 && quit==1)
{
name= et.getText().toString().trim();
number=get_Number(name);
if(number!=null)
{
pass++; // go for confirmation
in = new Intent(ContactActivity.this, Calling.class);
tts.speak("contact found", TextToSpeech.QUEUE_FLUSH, null);
while(tts.isSpeaking());
tts.speak("Do u want to continue", TextToSpeech.QUEUE_FLUSH, null);
while(tts.isSpeaking());
}
else
{
tts.speak( name +"does not exist", TextToSpeech.QUEUE_FLUSH, null);
while(tts.isSpeaking());
tts.speak("try again", TextToSpeech.QUEUE_FLUSH, null);
while(tts.isSpeaking());
fun(true); //enable touch events
}
}
else if(pass==2)
{
b.putString("number", number);
in.putExtras(b);
tts.speak( "calling"+name, TextToSpeech.QUEUE_FLUSH, null);
while(tts.isSpeaking());
tts.shutdown();
startActivity(in);
//int pid = android.os.Process.myPid();
//android.os.Process.killProcess(pid);
finish();
//Intent
}
else if (quit==2) {
while(tts.isSpeaking());
tts.speak("Quitting application",TextToSpeech.QUEUE_FLUSH, null);
while(tts.isSpeaking());
tts.shutdown();
finish();
}
}//end of if
else if (keyCode == KeyEvent.KEYCODE_MENU
&& event.getRepeatCount() == 0)
{
fun(false);//disable touch events
if(pass==2)
{
pass=1;
//while(tts.isSpeaking());
tts.speak("Going back",TextToSpeech.QUEUE_FLUSH, null);
while(tts.isSpeaking());
fun(true);
//mhandler.postDelayed(mUpdateTimeTask, 1000);
}
else if(quit==2)
{
quit=1;
//while(tts.isSpeaking());
tts.speak("Going back",TextToSpeech.QUEUE_FLUSH, null);
while(tts.isSpeaking());
fun(true);//enable touch events
}
else if(quit==1)
{
quit++;
//mhandler.removeCallbacks(mUpdateTimeTask);
//while(tts.isSpeaking());
tts.speak("Do u want to quit",TextToSpeech.QUEUE_FLUSH, null);
while(tts.isSpeaking());
//Toast.makeText(TimerActivity.this,"Do u want to quit",Toast.LENGTH_LONG).show();
}
}
return true;
}
Try the workaround mentioned here Android button enabling/disabling not working
onClick(View v)
{
if(condition)
{
return;
}
else
{
//perform onClick stuff
}
}

NullPointerException When Intent is used Android After Checking A boolean Condition?

I have a activity am using a boolean condition to check some thing .if the boolean condition Satisfy i need to Go to The next Page. But When The condition Satisfy the Device get crash With NullPointerException Am giving The Code Below
The Boolean Condition
boolean check()
{
boolean matches=false;
int falseFlag=0;
if(cc.length==picarray.length)
{
for (int i=0;i<cc.length;i++)
{
if(cc[i].equals(picarray[i]))
{
//---The Database Value Stored in Array is modified---
xmin=X[i]-25;
xmax=X[i]+25;
ymin=Y[i]-25;
ymax=Y[i]+25;
//---Check Whether The Selected Password Is Inside The Array Values---
if(xmin<realx[i]&&realx[i]<xmax)
{
System.out.println("TRUE");
}
else
{
falseFlag++;
System.out.println("FALSE");
}
if(ymin<realy[i]&&realy[i]<ymax)
{
System.out.println("TRUE");
}
else
{
falseFlag++;
System.out.println("FALSE");
}
}
else
{
falseFlag++;
}
}
}
else
{
falseFlag++;
}
if(falseFlag==0)
{
matches=true;
}
System.out.println("Authentication returns "+matches);
return matches;
}
in button click
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(check())
{
Intent sa=new Intent(Test.class,Test2.class);
startActivity(sa);
System.out.println("U R AUTHENTICATED");
}
else
{
System.out.println("INVALID USER");
Toast.makeText(getApplicationContext(), "INVALID USER", Toast.LENGTH_LONG).show();
}
}
});
try this,
Intent sa=new Intent(getApplicationContext(),Test2.class);
basically intent needs context and not a class...
i doub't this (Intent sa=new Intent(Test.class,Test2.class);) will compile
The first argument is a Context so when you create the intent, it should be:
Intent sa=new Intent(Test.this,Test2.class);
instead of
Intent sa=new Intent(Test.class,Test2.class);
This should also work:
Intent sa=new Intent(v.getContext(),Test2.class);
Intent sa=new Intent(Test.class,Test2.class);
The first parameter should be Test.this(Context), is it not throwing a Compile-time error ??

Categories

Resources