If Condition not working in my command - android

Where Is The problem can anybody help me my if statement is not working as i want it to have checked my edittext is visible or invisible in android.
Now i have to check the condition is.,
If my edittext is visible means how can i insert the data.
If my edittext is gone means how can i insert on another data.
This is my code for if i have to check the checkbox means the edittext is invisible otherwise the edittext is visible .:
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.selling1);
b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DecimalFormat tw = new DecimalFormat("0.00");
EditText a= (EditText)findViewById(R.id.y);
EditText b= (EditText)findViewById(R.id.a);
Float x=Float.parseFloat(a.getText().toString());
Float y=Float.parseFloat(b.getText().toString());
if((a.getText().toString().equals(""))){
Toast t= Toast.makeText(getApplicationContext(), "10000 is there", Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER, 02, 10);
t.show();
}else{
Float z=(x*y)/(100+y);
Float p=x-z;
EditText c= (EditText)findViewById(R.id.c);
c.setText(tw.format(z));
EditText d= (EditText)findViewById(R.id.e);
d.setText(tw.format(p));
}

print a.getText().toString() and see what it is returning, if it is empty try this
if("".equals(a.getText().toString())){
Toast t= Toast.makeText(getApplicationContext(), "10000 is there", Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER, 02, 10);
t.show();
}

If you want to check visibility of EditText the don't compare its text. Follow this instead:
if (a.getVisibility() == View.VISIBLE){
//Perform your action of inserting
}
Hope you understand why to choose this. Say, user entered something in EditText. Then you made that EditText invisible. At this time its text is not cleared. So you still get that text.
Hope this helps.

Related

Checking empty of EditText and send Toast - / avoid saving empty data to SQLite

Sorry taking your time, am asked a question in a very wrong way.
So what I doing now, a small notepad program where the title and content saved of the note to the SQLite database.
This part working as should, but I don't have any input check and the app saving the note with empty title and content.
there is my current code for this :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dba = new DatabaseHandler(MainActivity.this);
title = (EditText) findViewById(R.id.titleEditText);
content = (EditText) findViewById(R.id.wishEditText);
saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
saveToDB();
}
});
}
private void saveToDB() {
MyNote wish = new MyNote();
wish.setTitle(title.getText().toString().trim());
wish.setContent(content.getText().toString().trim());
dba.addWishes(wish);
dba.close();
//clear
title.setText("");
content.setText("");
Intent i = new Intent(MainActivity.this, DisplayNotesActivity.class);
startActivity(i);
How can I implement a basic input checking and avoid the saving of empty notes?
my first idea was to check the emptiness of the input and drop a toast message, tried several solutions from not, but not worked me.
many thanks
C
You have to be sure you're using the same EditText variable which in this case I think is title.
EditText title = (EditText) findViewById(R.id.titleEditText);
if(title.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Input Text Is Empty.. Please Enter Some Text", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, title.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
});
U need to specyfi your question.
But from what i undestood u don't know how check editText is empty and don't know why can't write into text box.
First easy method is just check length of string if it bigger than 0 that's meen it's not empty
EditText title = (EditText) findViewById(R.id.IDEDITTEXT);
if(String.valueOf(title.getText()).length()>0){
//do something
}else{
//do something
}
And about second (i thing question) is check your xml file (layout) does your editText is enabled and not is textViev.
Btw your code is hard to read like your question:)
EditText title = (EditText) findViewById(R.id.titleEditText);
if(title.getText().toString().length()<=0) {
Toast.makeText(MainActivity.this, "Input Text Is Empty.. Please Enter Some Text", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, title.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
});
try this code...

EditText crashing simple calculator

I'm trying to build basic calculator, but when I put a number at first edittext and hit add button, it gets crashed.
It is fine when I add two number in both editTexts. There was no problem in that. But the problem happened when I put only one number and hit add.
It is throwing NumberFormatException: Invalid int: "".
here is my basic code.
public class MainActivity extends AppCompatActivity {
EditText num1;
EditText num2;
Button add,sub,multi,div;
TextView MyResults;
String Number1 ;//;
String Number2 ;//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (EditText)findViewById(R.id.num1);
num2 = (EditText)findViewById(R.id.num2);
add = (Button)findViewById(R.id.Add);
sub = (Button)findViewById(R.id.Sub);
multi = (Button)findViewById(R.id.Multiple);
div = (Button)findViewById(R.id.Divide);
MyResults = (TextView)findViewById(R.id.results);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Number1 =num1.getText().toString();
Number2 =num2.getText().toString();
int mAdd1 = Integer.valueOf(Number1);
int mAdd2 = Integer.valueOf(Number2);
int myAdd = mAdd1+mAdd2;
MyResults.setText(String.valueOf(myAdd));
Toast.makeText(getApplicationContext(), "Added: "+ myAdd, Toast.LENGTH_LONG).show();
}
});
}//end OnCreate.
When you leave an EditText empty
int mAdd2 = Integer.valueOf(Number2);
Will crash because a empty String is not a valid Integer (hence the NumberFormatException).
You could put it in a try/catch like this:
try {
Number1 =num1.getText().toString();
Number2 =num2.getText().toString();
int mAdd1 = Integer.valueOf(Number1);
int mAdd2 = Integer.valueOf(Number2);
MyResults.setText(String.valueOf(myAdd));
Toast.makeText(getApplicationContext(), "Added: "+ myAdd, Toast.LENGTH_LONG).show();
catch (NumberFormatException e) {
//Give a toast saying the user did not enter two correct values
}
Or alternatively, check the value beforehand and check if nothing was entered:
number1 =num1.getText().toString();
number2 =num2.getText().toString();
if (number1.equals("") || number2.equals("")) {
//Show error or set numbers to a different value
}
However, for this to work you must assume the user did not enter anything that isn't a valid Integer like a letter, for example by setting the EditText's input type to numbers.
android:inputType="number"
You should do validation checking for a blank edit text and not do the calculation in this case.
Instead you might take advantage of EditText's setError(CharSequence) method where you can show a message to the user why the input is not valid.

Android:Got NumberFormatExxception while getting data from EditText field

java.lang.RuntimeException: Unable to start activity ComponentInfo { com.project/com.project.simple} : java.lang.NumberFormatException
EditText et1,et2,et3;
Button b1, b2;
Float two,three,four;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.can);
et1 = (EditText) findViewById(R.id.editText1);
two = Float.valueOf(et1.getText().toString());
et2 = (EditText) findViewById(R.id.editText2);
three = Float.valueOf(et2.getText().toString());
et3 = (EditText) findViewById(R.id.editText3);
four = Float.valueOf(et3.getText().toString());
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
you are in onCreate() where the fields are specified. the user could not have time to enter any valid data. you need to move the fetching of the data somewhere else...like your onClick() perhaps.
move your code from onCerate to an other method
(for example the onClick method of the Button )
and you should
try this:
try{
two = Float.parseFloat(et1.getText().toString());
}catch(NumberFormatException e){
two = 0;
Toast toast = Toast.makeText(this, 'Invalid number format on `two` field', 500);
toast.show();
}
For each text fields what you want to read
Comment:
float format is 2.3 and don't use 2,3
It seems that the input is not valid. Please check twice that you don't try to parse a letters to a number. If you have a float please check that you use the right locale for parsing. E.g. in germany is pi 3,1415... and not 3.1415...
If you cannot preparse the values you could put the parsing trys in try catch blocks like this:
float value;
try {
value=Float.parseFloat(someString);
} catch(NumberFormatException e) {
// input was no valid float
}

Dynamically added EditTexts and forcing the user to enter data on focus change

I have dynamically added 3 EditTexts on a TableLayout. Then on focus change through tab on emulator in Eclipse, I check if they are blank, if blank I need to set the focus to the EditText else it should move to the next.
The issue is that, focus moves to the next EditText and cursor allows entry into next field. Also both EditTexts get highlighted. Can someone help me with the proper way to change focus?
I though this would do the trick but it doesn't work. Again both the EditTexts are getting focussed as both are blank, causing focus listeners on both to be activated.
if(arrEdit.size()>0){
for(EditText etLot: arrEdit){
etLot.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
String msg = null;
EditText et = null;
if(!hasFocus){
for(int i=0;i< arrEdit.size();i++){
msg= ((TextView)arrTxtVw.get(i)).getText().toString();
System.out.println("Focus Changed on "+msg);
if((((EditText)v).length()>0)==false && ((EditText)v).getId()== arrEdit.get(i).getId()){
System.out.println("size is zero for "+msg);
(arrEdit.get(i)).requestFocus();
et=(arrEdit.get(i));
break;
}
}
TextView text = (TextView) lyout.findViewById(R.id.text);
text.setTextSize(15);
text.setText(msg+"cannot be blank");
Toast toast = new Toast(ctxref);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(1500);
toast.setView(lyout);
toast.show();
final EditText ettemp = et;
et.post(new Runnable() {
public void run() {
ettemp.requestFocus();
}
});
}
}
});
}
}

how to take input from user in android

i have a EditText in android in which i want the user to enter the text and checks for the condition "BYE"
Code sample:
EditText text = (EditText)findViewById(R.id.EditText01);
String abc= text .getText().toString();
while( !(abc).equals("bye")){
abc = text.getText().toString();//user should enter the text from keyboard and the while loop should go and chech the condition.but not able to enter the text
//do some operation with abc
}
How can i make user to enter the text??The UI should wait for the text to be entered(something like we have InputStreamReader in java applications).
Very Simple:-
EditText text = (EditText)findViewById(R.id.EditText01);
String str = text.getText().toString();
now in str u will get string which is entered in EditText
I don't think you need a loop to do this. From your comment it looks like you also have an "Enter" button or something that you click to do the checking. Just set an onclicklistener and onclick you can make the edittext invisible (or un-editable), check is the edittext is equal to "BYE" and then do your actions might look something like this:
final EditText ET = (EditText) findViewById(R.id.EnterText);
Button B1 = (Button) findViewById(R.id.EnterButton);
B1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ET.setVisibility(View.INVISIBLE);
if(ET.getText().toString() == "BYE")
{
//do something if it is "BYE"
} else {
Context context = getApplicationContext();
CharSequence text = "Please enter BYE";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
ET.setVisibility(View.VISIBLE);
} });
Instead of running this check in an infinite loop, only run it on every onKeyUp of the EditText. You know, anyway, that the condition will only ever be fulfilled if the user actually enters something.

Categories

Resources