I'm working on an app to write NFC tags. The layout has an EditText to enter the URL to write and a Button to save in NFC tag.
My question is the following:
How can I add a fixed String to the text entered in the EditText? For example:
Instead of
Text entered: 45
Text written: 45
Should be:
Text entered: 45
Text written: http://myweb.com?id=45
URL (The user never sees this, but is set internally in the app and will be saved in the NFC tag): http://myweb.com?id=
I'm from php side, but I don't know how to do this in android. In php it should be like this:
$Formula = '$URL'.'$TextEntered';
I hope you can help me.
Try this:
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
your_formular="http://myweb.com?id=" + s.toString();
}
});
Related
I am trying to build a simple converter code(convert feet to mtr) in Android. I am able to do this but only when user clicks some button. Now I want to modify it such that it starts to convert as and when the user gives input(Something like google converter). Is there any way to do this in Android?
Thanks in advance.
Add the listener to your edittext:
yourEditText.addTextChangedListener(addTextWatcher);
Add the TextWatcher interface:
private TextWatcher addTextWatcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence sequence, int start, int before, int count) {
// here is where you could grab the contents of the edittext
// input each time a character is entered, and pass the value
// off to your unit conversion code. Careful to check for
// numerals/decimals only, or to set the proper inputType in
// your xml.
}
#Override
public void beforeTextChanged(CharSequence sequence, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable sequence) {
}
};
For AndroidStudio, I am creating a simple conversion app that allows you to convert kilometres to miles and vice versa. I am able to convert it by clicking a button, but how would you do it in real time? For example, as I am typing the number, it converts it right away in a different textbox.
This is the code for my onClick method that I created for my button:
public void onClick(View vw){
EditText value = (EditText)findViewById(R.id.editText);
double total = Double.parseDouble(value.getText().toString()) * 0.621371192;
TextView obj = (TextView)findViewById(R.id.milesDisplay);
obj.setText(Double.toString(total));
obj.setVisibility(View.VISIBLE);
}
You can do this if you're using an EditText:
mEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Every time you hit a number, capture the number and convert it
}
#Override
public void afterTextChanged(Editable s) {
}
});
inside onTextChanged a listener is trigged every time you hit the keyboard. Force it to only accept numbers.
I'm trying to work with a MultiAutocompleteTextView in such way that whenever I type a character in the textview, an event will be fired OR after 2-3 elements have been typed, fire the event again.
The reason I work with multiautocomplete is because I also need the autocompletion feature .
Is there such an event that can be triggered after every character or 2-3 characters typed? Thanks!
Yes you can use add a TextWatcher and TextChangedListener to your edit Text like this:
myEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//check count or count in edit text and do something
}
#Override
public void afterTextChanged(Editable arg0) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});
http://developer.android.com/reference/android/text/TextWatcher.html
In my activity there is an error in this line (Home.this,android.R.layout.activity_home, searchResults)); which says "activity_home cannot be resolved" but if i give as single_list_item_1 as mentioned in one of the tutorial, error disappears, for this error i had checked in res for capital letters, then cleaned the project but still not able to get rid of this.
searchBox.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
//get the text in the EditText
String searchString=searchBox.getText().toString();
int textLength=searchString.length();
//clear the initial data set
searchResults.clear();
for(int i=0;i<songsList.size();i++)
{
String playerName=songsList.get(i).get("title").toString();
if(textLength<=playerName.length()){
//compare the String in EditText with Names in the ArrayList
if(searchString.equalsIgnoreCase(playerName.substring(0,textLength)))
Toast.makeText(getApplicationContext(),playerName,1).show();
searchResults.add(songsList.get(i));
}
list.setAdapter(new ArrayAdapter<HashMap<String, String>>
(Home.this,android.R.layout.single_list_item, searchResults));
}
adapter.notifyDataSetChanged();
}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
public void afterTextChanged(Editable s) {
}
});
android.R.layout.activity_home
Something which starts with android is a predfined android resource.
if you want to use your own layout like activity_home.xml
you must use it this way
R.layout.activity_home
which means your application resource.
you have to learn the difference between android.R.layout.activity_home and R.layout.activity_home. the first one will try to load layout from android's predefined layout collection, while the other one is located in your projects layout folder
I am creating a comma seperated file and don't want to give the user a way to confuse the app.
Following what RoToRa said, you can delimitate the file using tabs instead.
If you do want to disallow commas, you can add a TextWatcher to modify the string before it is posted to the GUI:
EditText text;
private void foo()
{
text.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void afterTextChanged(Editable s)
{
// modify string here
}
});
}
Just check the addTextChangedListener(TextWatcher watcher) method - add the listener, which will check the editText field when it changes.