I have button for open other app by write name of package in edittext i want if user not write any text in edittext and click button for open app get error toast like you should name of package first .. how do like this
?
If I understand you correctly, you need to see if the user typed something into an EditText, and if they didn't, show a toast.
// Storing EditText value in package
String package = editText.getText().getString().trim();
// calculating how many character are there in string.
int checkEmpty = package.length();
if (checkEmpty == 0) {
Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
}
The better way to check empty in android you can write this code.
if(TextUtils.isEmpty(editText.getText().toString())){
Toast.makeText(getApplicationContext(), "This is empty.", Toast.LENGTH_LONG).show();
}
Related
I have made an application that whenever a user types a particular word it toast a massage now I want to add a feature that if the user has typed the particular word continuously many times in a particular time it will consider it once only and toast the massage ones only.
The code
if ( string.equals("help") ) {
Toast.makeText(getApplicationContext(), "we are here to help", Toast.LENGTH_SHORT).show();
}
Use a global String variable let's name it as lastText and check whether input text is the same as last text.
UPDATE for time tracking
private String lastText = ""; // Global for all class members
private long lastTextTime = 0; // Global
//...
// May be more code goes here
//...
if ( string.equals("help") && !string.equals(lastText) ) {
// Check whether 5 min has elapsed to show the toast message
if(System.currentTimeMillis - lastTextTime > (5*60*1000)) {
Toast.makeText(getApplicationContext(), "we are here to help",
Toast.LENGTH_SHORT).show();
// If the program reach here save this string as a last text for the next check
lastText = string;
lastTextTime = System.currentTimeMillis;
}
}
You can add already typed words to a list, and when the user types a word, you can iterate through this list and check if "word" is already was typed, and then if "false" - show toast.
Alternative solution - you can create a local SQLite database (using Room persistence library), it will allow you to save values even if user restart/exit app, but it will require more coding and fixing nuances
hi everyone I hello everyone I have an application
with three TextView in a fragment and I want at least one of the three TextView is never empty, that is, at least one of the three extview must always be written.
My code is:
TextView comment_text = getActivity().findViewById(R.id.addtext_comment);
TextView mymenu = getActivity().findViewById(R.id.mymenu);
TextView mymenu2 = getActivity().findViewById(R.id.mymenu2);
String comment = String.valueOf(comment_text.getText());
String mymenu_text= String.valueOf(mymenu.getText());
String mymenu2_text= String.valueOf(mymenu2.getText());
if (validate(mymenu_text) && validate(mymenu2_text) && comment.length()==0||
validate(mymenu_text) || validate(mymenu2_text)||comment.length()!=0) {
Log.d("main_activity_spinner", "it's ok");
}
public boolean validate(String text) {
if (text.contains("null")) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Attenction!");
builder.setMessage("please insert at leat comment or delat or status");
builder.show();
return false;
}else
return true;
}
Now my code, if I write in one of the three textView, it returns the alert "please insert at least comment or delay or status" and then Log("it's ok"),but I want that if I don't write in any of the TextView, I get back an alert and if I write in one of the three TextView (or if I write in two or in all three TextView), I return the Log "it's ok". How can i do?
To check that at least one of them has text, you just have to check whether all the strings are empty. If so, show your warning, and if not, "it's ok":
EditText comment_text = getActivity().findViewById(R.id.addtext_comment);
EditText mymenu = getActivity().findViewById(R.id.mymenu);
EditText mymenu2 = getActivity().findViewById(R.id.mymenu2);
String comment = comment_text.getText().toString();
String mymenu_text= mymenu.getText().toString();
String mymenu2_text= mymenu2.getText().toString();
// if all three strings are empty, show the alert, otherwise log "it's ok"
if( comment.isEmpty() && mymenu_text.isEmpty() && mymenu2_text.isEmpty() ) {
// all of them are empty
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Attenction!");
builder.setMessage("please insert at leat comment or delat or status");
builder.show();
}
else {
// at least one has text
Log.d("main_activity_spinner", "it's ok");
}
Note 1: I assume these are actually EditText views, not pure TextView instances, since TextView is not editable by a user at runtime.
Note 2: For this to work, you have to call it at the time of validation. If you put this in something like onCreate it will be meaningless since the user wouldn't have had time to enter anything anyway.
I am practicing to build a calc app in Android Studio using Java. here's how it looks like
Yes it is very simple
Now instead of using buttons to enter numbers, I used two EditText views for entering numbers. now I wrote a method to add two numbers like this:
btn_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int result;
first=Integer.valueOf(input1.getText().toString());
second=Integer.valueOf(input2.getText().toString());
result = first+second;
value.setText(""+result);
Toast.makeText(MainActivity.this, "Please fill the both space with numbers", Toast.LENGTH_LONG).show();
}
the code for adding numbers is fine but if I do not have an input in EditText views, the app crashes. I want to put the codes in an if else clause. if there are inputs in both spaces app does the operation but if not it makes a toast and askes for numbers from the user. but I have no idea how to code the if else conditions. anybody can help me?
You can test if the input1 or input2 is empty as your if else testing criteria. The following code assume you have correctly found and initialized input1 and input2 editText views.
#Override
public void onClick(View v) {
int result;
Editable firstInputText = input1.getText();
Editable secondInputText = input2.getText();
if(firstInputText != null && firstInputText.length>0 &&
secondInputText != null && secondInputText.length>0){
first=Integer.valueOf(firstInputText.toString());
second=Integer.valueOf(secondInputText.toString());
result = first+second;
value.setText(""+result);
} else{
Toast.makeText(MainActivity.this, "Please fill the both space with numbers", Toast.LENGTH_LONG).show();
}
}
Also for your calculator, you want to ensure the inputs are numeric strings. For your layout xml, you can specify it as follows.
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/my_test_editText"
android:inputType="number"
/>
This is a problem I've been trying to fix for 3 hours now.
How the app works:
I've got a list of numbers in a txt file (in assets). I'm reading that txt file line by line and I set the text of a TextView to the current line. Now I have to type the same number in an EditText. But if I compare the EditText and the current line, the app says they are not the same, although they are.
I've got a file in assets with different numbers, like this:
3
9
8
14
[finish]
The [finish] tag will be used later so I can finish the application, but is pretty unnecessary now.
I've defined the following strings and views:
public String curval;
public EditText etds;
public TextView curvalte;
A part of my onCreate() method:
etds = (EditText)findViewById(R.id.editText1);
buttonds = (Button)findViewById(R.id.button1);
curvalte = (TextView)findViewById(R.id.textView2);
try {
inds = this.getAssets().open(assetname);
} catch (IOException e) {
e.printStackTrace();
}
readerds = new BufferedReader(new InputStreamReader(inds));
Now I'm reading from the file and setting it as text of curvalte:
curval = readerds.readLine();
curvalte.setText(curval);
And it works just fine.
But then you have to type the same number that is now shown in curvalte in the EditText.
If I try to use equals() on the text in EditText with curval, it always says that EditText.getText().toString doesn't equal curval:
if(etds.getText().toString().equals(curval.toString()){
// The code here
}else{
// This is what I get
Toast.makeText(getBaseContext(), "Wrong answer!", Toast.LENGTH_LONG).show();
}
Even if I enter the correct answer, I get that toast saying it's wrong.
Removing the .toString() didn't fix it and I've got no idea what I could try.
Maybe it's because of the string that actually contains a number?
Try printing both of the values out and looking at them. You may not even be getting the right values from the file or perhaps your getting some weird formatting characters like "\n"
Try this with .trim(), sometimes that is the issue
if(etds.getText().toString().trim().equals(curval.toString().trim()){
// The code here
}else{
// This is what I get
Toast.makeText(getBaseContext(), "Wrong answer!", Toast.LENGTH_LONG).show();
}
Alright, I found the problem. It was the formatting of the assets file!
Cp1252 encoding fixed it.
Maybe you have spaces in the string or the difference between Uper and LowerCase. Try this version:
if(etds.getText().toString().trim().toLowerCase().equals(curval.toString().trim().toLowerCase())){
Log.i("Yes", "equal");
} else {
Log.i("NO", "not equal");
}
I have an Android App calling a PHP script, which returns 0 (FALSE) or 1 (TRUE) - unfortunately as a string. So in my Java Code I have the variable String result which is either "0" or "1". I know (thanks to you guys here) that this string can start with the BOM, so I remove it, if it does.
It is not really necessary but let's say I'd feel better if I had that result as an integer not as a string. The casting from string to int named code seems to work. At least I do not see anything happen.
But when I want to use the casted int like if (code == 1) or display it via Toast, my app crashes.
I can show with Toast that result == "1" and that result.length() == 1 so I do not see how I can possibly fail casting this string to int:
String result = postData("some stuff I send to PHP");
if (result.codePointAt(0) == 65279) // which is the BOM
{result = result.substring(1, result.length());}
int code = Integer.parseInt(result); // <- does not crash here...
// but here...
Toast.makeText(ListView_Confirmation.this, code, Toast.LENGTH_LONG).show();
I also tried using valueOf() and adding .toString() but it just keeps crashing. What am I missing here?
Use the following way to show toast
Toast.makeText(ListView_Confirmation.this, ""+code, Toast.LENGTH_LONG).show();
Toast.makeText requires a String(CharSequence) or an int but this int represents the resource id of the string resource to use (ex: R.string.app_name)
Try instead :
Toast.makeText(ListView_Confirmation.this, String.valueOf(code), Toast.LENGTH_LONG).show();
Use the below
Toast.makeText(ListView_Confirmation.this, String.valueOf(code), Toast.LENGTH_LONG).show();
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#valueOf(int)
public static Toast makeText (Context context, CharSequence text, int duration)
Make a standard toast that just contains a text view.
Parameters
context The context to use. Usually your Application or Activity object.
text The text to show. Can be formatted text.
duration How long to display the message. Either LENGTH_SHORT or LENGTH_LONG
http://developer.android.com/reference/android/widget/Toast.html
So use String valueOf(code) as the second parameter to makeText(params)
Returns the string representation of the integer (code) argument.
According to Toast library Toast.makeText(Context, int, int) uses that integer as Resource ID to Find a String in your resources.
Now since you don't have a resource with the same integer the function throws Resources.NotFoundException .
So to display your int value you have to change it back to text.
Toast.makeText(ListView_Confirmation.this, String.valueOf(code), Toast.LENGTH_LONG).show();
In android Api, the constructor of a toast is :
Toast.makeText(Context context, int resId, int duration)
"resId" is the reference of your String but not a String Object:
example: resId= R.string.helloworld