Unfortunately application has stopped in Android because of EditText - android

I have a little problem with checking length of EditText.
text = take.getText().toString();
if (text.matches("")) {
Toast.makeText(getApplicationContext(), "Enter first value", Toast.LENGTH_SHORT).show();
take is a EditText and text is a String.
When i run my app without Edittext value i got an Error:
Unfortunately "ApplicationName" has stopped.
In my Class i have
private EditText take;
private String text;
Please help me, i try to use method .equals("") or .length()==0 but stil the same.

you may have forgotten findViewById or you are calling it before setContentView of your activity!

Some time match and equal will not return proper. I prefer to use a function to check empty value.
public static String chechNullState(EditText checkTextBox) {
String makeModVal = null;
if (!TextUtils.isEmpty( checkTextBox.getText().toString() ))
{
makeModVal=checkTextBox.getText().toString();
return makeModVal;
}
else
{
return null;
}
}
Usage:
item =(EditText)findViewById(R.id.take);
String takeVal = chechNullState(item);
if ( !takeVal == null)
{
//do what ever you want;
}

Related

Regex for allowing only particular special characters

Below is the code which I was trying to integrate, if user tries to enter special character. What I want to do is if user tries to enter any special character, it should not allow them to enter any character in edittext.
For that I was thinking of some logic. But below code is what I went for which I dont think is an optimal solution to write it individually for all. Below is getter setter in model class.
public String getProfileName() {
if(profileName != null){
return profileName.replace("#","");
}
return profileName;
}
Would appreciate any help.
the below regex from #anubhava solved my issue.
public String getProfileName() {
if(profileName != null){
return profileName.replaceAll("[^\\w\\h-]+","");
}
return profileName;
}

Can I change a URL using EditText and Button?

Here's the code that I want to change
private void updateQuestion() {
mQuestionRef = new Firebase("https://fdbfdb-d7cbb.firebaseio.com/"+
mQuestionNumber +"/question");
}
So can I add an EditText and Button beside it and the button will change the link (https://fdbfdb-d7cbb.firebaseio.com/) according to the value of the EditText, is that possible?
Yes obviously it's possible. Build the url during run time like keep your base url on a variable, and concat other part of url from edittext on button click then make network call or whatever you want.
String baseUrl = "https://fdbfdb-d7cbb.firebaseio.com/";
private void onClick(){
updateQuestion(edittext.getText().toString()) //Check edittext empty or not before this line
}
//////OR//////
private void updateQuestion(String number)
{ mQuestionRef = new Firebase(baseUrl+ number +"/question");
}
private void updateQuestion()
{
mQuestionRef = new Firebase("https://fdbfdb-d7cbb.firebaseio.com/"+ edittext.text.toString() +"/question");
}
Yes ofcourse
Method
private void updateQuestion(String questionNo) { mQuestionRef = new Firebase("https://fdbfdb-d7cbb.firebaseio.com/"+ questionNo+"/question");
}
Method usage
First convert your edit text into string like
String question = edittext.text.toString()
UpdateQuestion(question)
Now call the function on your button click or any where you want

RxAndroid Update Field/Textview when String Value Changes

I am just getting started with RxJava/RxAndroid and I was wondering if I can use it to solve the following problem. Basically, given a Field, say a textview, and a value, a string, I am looking for a way to automatically update the textview whenever the value of the string changes. I am not sure exactly how I would implement this as an Observable. Let me demonstrate;
String str = "Test"; //the string value
TextView textView = (TextView) findViewById(R.id.textView); //the textview
Observable o = //looking for this part. Want to observe the String str
o.subscribe(new Observer<String>() { //subscribe here looking for string changes
#Override
public void onCompleted() {
System.out.println("Completed");
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext(String s) {
textView.setText(s); //update the textview here
}
});
//here is where the string changes, it could be hardcoded, user input, or
//anything else really, I just want the textview to be updated automatically
//without another setText
str = "Different String";
Is what I am looking for possible with RxAndroid/RxJava?
The easiest way to accomplish that would be to use any kind of of Subject, maybe either a BehaviorSubject or a PublishSubject. A Subject is both a Subscriber (so you can put values into it with onNext) and an Observable (so you can subscribe to it). Look here for an explanation of the differences: http://reactivex.io/documentation/subject.html
So, instead of
String str = "Test";
you would have
BehaviorSubject<String> stringSubject = BehaviorSubject.<String>create("Test");
You can then directly subscribe to stringObservable.
And instead of assigning a new value to your variable like this:
str = "Hello World!";
you would do
stringSubject.onNext("Hello World!");
Oh, and never leave onError empty - doing so will quietly swallow any exceptions that may have occured earlier, and you will sit and wonder why nothing is happening. At least write e.printStacktrace().

Button Text and Booleans (Simple Android App)

I'm build the Fun Facts app on the Android Development Track. I decided to take a exploratory detour and try to create a very basic introductory message to the user. I changed the factTextView text to "You can click the button below to see a new fact!" and changed the showFactButton text to "Try it out!"
From there, I changed the final line onClick object (is that an object?) to the following:
public void onClick(View view) {
String fact = mFactBook.getFact();
// Update the label with our dynamic fact
factLabel.setText(fact);
// Set button text to new fact prompt
showFactButton.setText("Show another fun fact.");
This seems to work fine. However, I feel like "updating" the button text to the same new string on every press isn't always the best practice, even if it is easy and readable. I tried to add a boolean that will check the text of the button, and update it only if it has not already been updated. This is what I've come up with so far:
View.OnClickListener listener = new View.OnClickListener() {
#Override
public String launchText = getResources().getString(R.string.start_text);
public String nextText = getResources().getString(R.string.next_text);
public String buttonText = (String) showFactButton.getText();
public boolean updateLaunchText() {
if (buttonText.equals(launchText)) {
buttonText.replaceAll(launchText, nextText);
return true;
}
else {
return true;
}
}
public void onClick(View view) {
String fact = mFactBook.getFact();
// Update the label with our dynamic fact
factLabel.setText(fact);
}
};
With the following added to strings.xml:
<string name="start_text">Try it out!</string>
<string name="next_text">Show another Fun Fact!</string>
No errors, but the button text stays on "Try it out!" I'm sure that all the extra objects are totally unnecessary compared to the first, working method for the scope of this app, but I'd still like to figure it out since I don't really have any idea what I'm doing with the boolean.
Questions: 1) What am I missing in the longer boolean approach? 2) What's the actual most efficient approach to accomplish this task?
Did you connect the listener to the button object?Without that connection no logic is applied to a button click.It goes like this:
buttonName.setOnClickListener(...)
You'd have to initialize the button object first though :)
Where r u call to method updateLaunchText() ?
you should change the objects to global object (not to create the into the listener):
private String launchText = getResources().getString(R.string.start_text);
private String nextText = getResources().getString(R.string.next_text);
private String buttonText = (String) showFactButton.getText();
and take the method updateLaunchText() out of the listener too.
and then into the onClick(View view) call to updateLaunchText() like this:
public void onClick(View view) {
updateLaunchText();
String fact = mFactBook.getFact();
// Update the label with our dynamic fact
factLabel.setText(fact);
}

Comparing two editTexts in android

I am learning android I tried following codeline but it's giving me error please give me suggestions, that how can I compare two edittext's text.
if((edt1.getText().toString() &&
edt4.getText().toString() &&
edt7.getText().toString)=="X")
Here's a solution that doesn't violate the DRY principle:
private static boolean allContain(final String value,
final EditText... editTexts)
{
for (EditText editText : editTexts) {
final String text = editText.getText().toString();
if (!text.equals(value)) {
return false;
}
}
return true;
}
You can use it as follows:
if (allContain("X", edt1, edt2, edt3, edt4)) {
// All EditTexts contain 'X'
}
Please try this:
if((edt1.getText().toString.equalsIgnoreCase("X")) &&
(edt4.getText().toString.equalsIgnoreCase("X")) &&
(edt7.getText().toString.equalsIgnoreCase("X")))
If you have to compare strings then you need to call the equals or equalsIgnoreCase function of String.
I have find the best solution..
if(Password.getText().toString().trim().matches(confirmPassword.getText().toString().trim()))
{
// then do your work
}
else
//otherwise show error message.
whereas
Password = (EditText)findViewById(R.id.pass);
confirmPassword = (EditText)findViewById(R.id.confirmpass);
are two editText.
if( (edt1.getText().toString()=="X")&&(edt4.getText().toString()=="X")&&(edt7.getText().toString()=="X") )
if you want to check edt1, edt4, edt7 have "X" value then try this..
if((edt1.getText().toString().equalsIgnoreCase("X")
&&edt4.getText().toString().equalsIgnoreCase("X") &&
edt7.getText().toString.equalsIgnoreCase("X"))
Make it simple:
if (!et1.toString().equals(et2.toString())) {
MsgBox(this,"--Your Message--");
}

Categories

Resources