I have an editText and a button. On click of the button, if the editText is empty it should set an error message to the editText. I am not getting how to test this scenario using Robolectric. This is my code `
// to check if the fields are empty
#Override
public boolean validate() {
if(edtEmail.getText().toString().trim().equalsIgnoreCase("")){
showErrorMessage(true);
return false;
}
if(edtPassword.getText().toString().trim().equalsIgnoreCase(null)){
showErrorMessage(false);
return false;
}
return true;
}
// to set error message to edittext
#Override
public void showErrorMessage(boolean email) {
if(email){
edtEmail.setError("Enter Correct Email Id");
} else {
edtPassword.setError("Enter Correct Password");
}
}
// on click of button
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(validate()) {
mPresenter.login(edtEmail.getText().toString().trim(), edtPassword.getText().toString().trim());
}
}
});
`
You can try this
assertThat("Enter Correct Email Id",edtEmail.getError().toString());
Related
How to Abone no ENTER ----> Sicil no ENTER ----> TC Kimlik no ENTER BORÇ_BUL ?
If you are triyng to use ENTER key as TAB key. The code below isn't very elegant, but it works. You can refactor, set the order from an array, etc...
Imagine a Login form (user and password fields and signin button):
public class LoginView extends VerticalLayout {
private TextField username;
private PasswordField passwordFld;
private AbstractTextField current;
public LoginView(
buildFields();
}
private void buildFields(){
HorizontalLayout fields = new HorizontalLayout();
username = new TextField("User");
passwordFld = new PasswordField("Pwd");
Button signin = new Button("Sign");
fields.addComponents(username, passwordFld,signin);
username.addFocusListener(new FocusListener() {
public void focus(FocusEvent event) {
current = username;
}
});
passwordFld.addFocusListener(new FocusListener() {
public void focus(FocusEvent event) {
current = passwordFld;
}
});
ShortcutListener enterkey = new ShortcutListener ("Enter", KeyCode.ENTER, null){
public void handleAction(Object sender, Object target) {
if (current.equals(username)){
passwordFld.focus();
}else if (current.equals(passwordFld)){
sign();
}else{
sign();
}
}
};
fields.addShortcutListener(enterkey );
signin.addClickListener(getClickListener());
addComponent(fields);
}
private ClickListener getClickListener() {
return new ClickListener() {
public void buttonClick(final ClickEvent event) {
sign();
}
};
}
private void sign(){
//Do the sign in or fail
}
}
following are the methods i use to get the key event, which are working fine on emulator but doesn't seem to work on device.
following are the methods i use to get the key event, which are working fine on emulator but doesn't seem to work on device.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_SPACE) {
Log.e("onkeydown", "onkeydown");
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
// TODO Auto-generated method stub
if (event.getKeyCode() == KeyEvent.KEYCODE_SPACE) {
Log.e("dispatch", "dispatch");
}
return super.dispatchKeyEvent(event);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getKeyCode() == KeyEvent.KEYCODE_SPACE) {
Log.e("onkeyup", "onkeyup");
}
return super.onKeyUp(keyCode, event);
}
Try this..
editText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int key, KeyEvent event) {
if (event.getAction()==KeyEvent.ACTION_DOWN && key == KeyEvent.KEYCODE_ENTER) {
//write code when key press
return true;
}
return false;
}
});
I want to come to home screen when clicking on the back button from my application in blackberry. I have searched for this in google and stackoverflow, But I didn't get any solution. Anybody help for this.
In My Starting Screen i wrote like this for back button.
protected boolean keyDown(int keycode, int time)
{
if (Keypad.key(keycode) == Keypad.KEY_ESCAPE)
{
close();
return true;
}
else
{
return super.keyDown(keycode, time);
}
}
In my HomeScreen i wrote like this.
protected boolean keyDown(int keycode, int time)
{
if (Keypad.key(keycode) == Keypad.KEY_ESCAPE)
{
UiApplication.getUiApplication().pushScreen(new StartingScreen());
return true;
}
else
{
return super.keyDown(keycode, time);
}
}
From my application HomeScreen I am able to come to starting screen of the my application. after that when i click on back button from starting screen i need to go to blackberry home screen that means i need to exit the app and come out of that. I wrote close(); to come to blackberry home screen. but its not working. it is again coming to my application homescreen.
Override keyDown method in your subclass of MainScreen.
protected boolean keyDown(int keycode, int time) {
int key = Keypad.key(keycode);
if(key==Characters.ESCAPE){
// do something here
return true;
}
return super.keyDown(keycode, time);
}
To come to home screen
while(!(UiApplication.getUiApplication().getActiveScreen() instanceof HomeScreen)){
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
}
I got the Solution... i have written lik this.
public boolean onClose()
{
int choose=Dialog.ask(Dialog.D_YES_NO, "Are you sure Want to Exit?");
if(choose==Dialog.YES)
{
System.exit(0);
}
return true;
}
Try this -
public class yourclass extends MainScreen{
public yourclass(){
}
}
public boolean onClose() {
Application.getApplication().invokeLater(new Runnable() {
public void run() {
//close this screen and push your home screen
}
});
return true;
}
i am new in blackberry developer. i am use a pillsetbutton and pillfieldbutton
but when i am click pillfieldbutton no any action is performed.I am using setchangeListener() method.but no any Action is performed.i am going Through this process.
public DemoPill() {
PillButtonSet objButtonSet=new PillButtonSet();
final PillButtonField objButtonField1=new PillButtonField("NSE");
final PillButtonField objButtonField2=new PillButtonField("BSE");
objButtonSet.add(objButtonField1);
objButtonSet.add(objButtonField2);
this.add(objButtonSet);
bjButtonSet.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
System.out.println("Hi ");
if(field==objButtonField1)
{
System.out.println("This Is NSE Button");
}
else if(field==objButtonField2)
{
System.out.println("This Is BSE Button");
}
}
});
}
}
You are printing it on console. So without debugging the code you will never know if your click is consumed. So just use an event thread to see the output on your screen. I have provided you the sample just check it. It will show the output on your screen. You can also use Dialog.inform(String message ) But its always good to do it on event thread.
public DemoPill() {
PillButtonSet objButtonSet=new PillButtonSet();
final PillButtonField objButtonField1=new PillButtonField("NSE");
final PillButtonField objButtonField2=new PillButtonField("BSE");
objButtonSet.add(objButtonField1);
objButtonSet.add(objButtonField2);
this.add(objButtonSet);
bjButtonSet.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// System.out.println("Hi ");
if(field==objButtonField1)
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("objButtonField1 button clicked")
}
});
}
else if(field==objButtonField2)
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("objButtonField2 button clicked")
}
});
}
}
});
}
}
May be this will help cheers. :)
You only can view the output of
System.out.println("ANYDATA");
in debug mode not in run.
Try to debug it not to run it.
I create a custom menu in new class extends popupScreen. I went when the user clock on the menu button the menu display. I use this code
protected boolean keyDown(int keycode, int time) {
if(Keypad.KEY_MENU == Keypad.key(keycode))
{
UiApplication.getUiApplication().popScreen(getScreen());
Menu popup = new Menu();
UiApplication.getUiApplication().pushScreen(popup);
return true;
}
else
return super.keyDown(keycode, time);
}
but I obtain error on this line UiApplication.getUiApplication().pushScreen(popup); causes by pushScreen. How can I change this code or there is another method to display this menu.
try this
protected boolean keyDown(int keycode, int time) {
if(Keypad.KEY_MENU == Keypad.key(keycode))
{
synchronized (UiApplication.getEventLock())
{
UiApplication.getUiApplication().popScreen(getScreen());
Menu popup = new Menu();
UiApplication.getUiApplication().pushScreen(popup);
}
return true;
}
else
return super.keyDown(keycode, time);
}
In Blackberry, creating, displaying and hiding of Menu is handled by the system; you will be provided with callbacks at appropriate time. The callbacks you will need to code are:
public boolean onMenu( int instance )
{
return true; // Menu is created
}
public void makeMenu( Menu menu, int instance )
{
MenuItem _desired_menu_item = new MenuItem()
{
public void run()
{
}
}
menu.add( _desired_menu_item );
super.makeMenu( menu, instance)
}
That's it! It would work. Please check Blackberry "UI and Navigation guide" for more details