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
Related
I am working on an app in which users have to select a country code, i was successful in creating a spinner for the said purpose as shown in this link:
Creating a spinner for choosing country code
But i am getting problem in reading the value selected in the spinner.
{
String abc = onCountryPickerClick();//abc is always null
}
public String onCountryPickerClick (){
ccp.setOnCountryChangeListener(new CountryCodePicker.OnCountryChangeListener() {
#Override
public void onCountrySelected() {
selected_country_code = ccp.getSelectedCountryCodeWithPlus();
}
});
return selected_country_code;
}
When String abc = onCountryPickerClick(); is being invoked, the selected_country_code value will be assigned to abc.
When your CountryCodePicker.OnCountryChangeListener's onCountrySelected() method is being invoked, the ccp.getSelectedCountryCodeWithPlus();'s value gets assigned to selected_country_code. Since String is immutable, changing selected_country_code's value won't change the value of abc, nor the return selected_country_code; will be invoked.
One of possible solutions would be to change your CountryCodePicker.OnCountryChangeListener anonymous implementation to assign the selected country value to abc e.g.
#Override
public void onCountrySelected() {
selected_country_code = ccp.getSelectedCountryCodeWithPlus();
abc = selected_country_code
}
Callbacks are not synchronous. Unfortunately, you cannot simply do String abc = onCountryPickerClick(); because what you are returning is something that is not yet set. Let's go through your code:
ccp.setOnCountryChangeListener(
new CountryCodePicker.OnCountryChangeListener() {
#Override
public void onCountrySelected() {
selected_country_code = ccp.getSelectedCountryCodeWithPlus();
}
});
The code seems to say that when the country is selected in the spinner, you assign the value of selected_country_code. Assuming this is an action triggered by the user, when you call String abc = onCountryPickerClick();, how can you be sure the user has selected anything? This is the issue. You cannot be sure that the user has already selected the option and returning the value is not enough.
You can solve this in many ways. You can for example keep propagating the callback:
public void onCountryPickerClick(OnCountryChangeListener listener){
ccp.setOnCountryChangeListener(listener);
}
// Anywhere you call this
onCountryPickerClick(new CountryCodePicker.OnCountryChangeListener() {
#Override
public void onCountrySelected() {
// Here do whatever you want with the selected country
}
});
The above approach is not very different than what you have now. There are other options. You could use java observables i.e.:
class CountryCodeObservable extends Observable {
private String value;
public CountryCodeObservable(String value) {
this.value = value;
}
public void setCountryCode(String countryCode) {
value = countryCode;
setChanged();
notifyObservers(value);
}
}
public CountryCodeObservable onCountryPickerClick(){
CountryCodeObservable retValue = new CountryCodeObservable("");
ccp.setOnCountryChangeListener(
new CountryCodePicker.OnCountryChangeListener() {
#Override
public void onCountrySelected() {
retValue.setCountryCode(ccp.getSelectedCountryCodeWithPlus());
}
});
return retValue;
}
// Then when calling this method you can do something like:
CountryCodeObservable observable = onCountryPickerClick();
observable.addObserver((obj, arg) -> {
// arg is the value that changed. You'll probably need to cast it to
// a string
});
The above example lets you add more than one observable. It might be too much for your use case, I just thought it illustrates another approach and also the asynchronicity of this situation.
Again, there are even more ways to solve this, the key is that you can't simply return a string and hope it changes when the user selects anything.
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);
}
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;
}
okay so in my other android activity I have it display a button on the screen if a certain string is in the array. I will be using the split function on a stored string to turn my string back into an array and then assinging that array into my private favorites array. In this example I am trying to get it to display a button if the string "UltimateBP" is in the private array favorites.
if i assign it directly using:
favorites[1]="UltimateBP";
it works and the button shows up correctly.
however if I assign it using the method below. it will not show up.
It does the same thing when I use the TextUtils split() method.
public class SampleApplication extends Application{
private String mStringValue;
private int numOfFavorites=1;
private String[] favorites = new String[150];
#Override
public void onCreate() {
mStringValue = "SavageLook.com";
favorites[0] = "None";
String someWords = "UltimateBP|Orange|Yellow";
String aColors[] = someWords.split("\\|");
numOfFavorites++;
String X = aColors[0];
favorites[1]=X;
super.onCreate();
}
Instead of using:
String aColors[] = someWords.split("\\|");
You just need to show "|" like:
String aColors[] = someWords.split("|");
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--");
}