I have one edittext and listview having some values. While typing text in edittext the listview contents will change according to the values typed inside edittext.
Now the problem is, the string is in case sensitive. ie, If the original text is Apparel, then if we type apparel or appa the original text is not displaying.
I want to make the string search case insensitive.
My code is,
private List<SearchList> searchTerms(List<SearchList> search_list, String s) {
List<SearchList> matches = new ArrayList<SearchList>();
for (SearchList search_lists : search_list) {
if (search_lists.search_term.contains(s)) {
matches.add(search_lists);
}
}
return matches;
}
Is there any way to achieve this. I have tried a lot.
string contains(); function is case sensitive.
And from your question, I've noticed that your list contains items with Capital letters as well in it.
So apply toLowerCase() to both side would cut it.
if (search_lists.search_term.toLowerCase().contains(s.toLowerCase()))
{
matches.add(search_lists);
}
Hope this helps.
Try to lower() a string:
private List<SearchList> searchTerms(List<SearchList> search_list, String s) {
List<SearchList> matches = new ArrayList<SearchList>();
for (SearchList search_lists : search_list) {
if (search_lists.search_term.contains(s.toLowerCase())) {
matches.add(search_lists);
}
}
return matches;
}
Just do as like and compare, Hope it will work :)
EditText editText= (EditText) findViewById(R.id.editText);
String editTextString=editText.getText().toString();
editTextString.equalsIgnoreCase(yourCompare);
Related
I have created a getter a setter for my building name and wish change the string to replace any characters that does not match with the regex expression. However in doing so, every time I enter an invalid character the edit text cursor/selection position changes to the beginning of the text. Hoe do I avoid this from happening?
private String buildingname="";
#Bindable
public String getBuildingname() {
return this.buildingname;
}
public void setBuildingname(String buildingname) {
if(!this.buildingname.equals(buildingname)) {
this.buildingname = buildingname.replaceAll(alphanumericregex,"");
this.pcr.notifyChange(this, com.tomtom.sangrahit.BR.buildingname);
}
}
You can use setSelection of editText AFTER you are setting new text
int position = myEditText.getSelectionStart();
myEditText.setText(myNewText);
myEditText.setSelection(position);
im writing a simple app where you can input data to database through app. I have to make conditions for example: you can't put digits in Name field. I know I have to do this in onClick, but I dont really know how. Can you help me?
public void onClick(View v)
{
switch (v.getId()){
case R.id.insertButton:
dm.insert(editName.getText().toString(),
editAge.getText().toString(),
editSurname.getText().toString(),
editSex.getText().toString(),
editPesel.getText().toString());
break;
case R.id.selectAllButton:
showData(dm.selectAll());
break;
case R.id.searchButton:
showData(dm.searchName(editSearch.getText().toString()));
break;
case R.id.deleteButton:
dm.delete(editDelete.getText().toString());
break;
}
}
You can cast the input to String in a try/catch, or set the input type to integer (use android:inputType="number" in your xml)
I know I have to do this in onClick.
onClick is perhaps not the ideal place as the user has already input the data and is therefore after the fact, as such.
Frequently it would be better to not allow unacceptable(sic) input when the user tries to enter such input.
There are alternatives, perhaps the simplest is to restrict the characters that can be input via the XML definition of the EditText e.g. to restrict to letters only then you could use :-
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
To allows spaces you could use (space added between lower and upper case) :-
android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
However, really you should use a String Resource so you could easily cope with multiple languages (locales).
Therefore you could create a String resource (res/values/strings.xml) such as :-
<string name="alphabet">"abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"</string>
and then use :-
android:digits="#string/alphabet"
Programatically (alternatively) you could use an InputFilter :-
mAlphaBet = getResources().getString(R.string.alphabet);
// Define the Input Filter
InputFilter inputfilter = new InputFilter() {
public CharSequence filter(CharSequence s,
int start,
int end,
Spanned dest,
int deststart,
int destend) {
for (int i=0; i < end; i++) {
// If not in the alphabet then suppress input
if(!mAlphaBet.contains(String.valueOf(s.charAt(i)))) {
return "";
}
}
// Indicate input is OK
return null;
}
};
// Apply filters (just the 1) to the EditText
editName.setFilters(new InputFilter[]{inputfilter});
mAplhaBet is retrieved from the resources as above, again catering for locales if defined.
I build EditText dynamically. Among other things, I set 2 properties: hint(.setHint) and inputType(.setInputType). My problem: when I invoke setInputType, setHint has no effect: blank edittexts remain blank with no hint. Once I comment out setInputType, I see all hints. I need both input type and hint. What to do? My code:
private EditText buildTextBox(Property property)
{
EditText control = new EditText(this);
control.setInputType(getInputTypeByPropertyInputType(property.getType()));// android.text.InputType.
control.setHint(property.getDisplayName());
return control;
}
private int getInputTypeByPropertyInputType(String type)
{
if (type.equals("integer"))
{
return android.text.InputType.TYPE_CLASS_NUMBER;
}
else
{
return android.text.InputType.TYPE_CLASS_TEXT;
}
}
#Eugene
Ensure you call control.SetHint() just before you call the control.setGravity() and control.setInputType(); and it works for me verrry much!
column1 = new EditText(this);
column1.setId(i);
column1.setHint("Value");
column1.setInputType(InputType.TYPE_CLASS_NUMBER);
column1.setGravity(Gravity.RIGHT);
I agree with Eugene.
Remove the gravity(just don't use CENTER) and the hint texts will come back as normal.
Nice find!
How to allow only a valid floating point number into a text field floating point like these only
2.353
-2.354
4444.45
Implement a focus listener on the field. When the focus changes from the textfield to any other part of your form simply use a regexp to check the validity of the input.
Something like :
^(-)?\d*(\.\d*)?$
Should do the trick.
Then use the pattern matching of Android to see if the input matches the regexp :
Pattern p = Pattern.compile("^(-)?\d*(\.\d*)?$");
Matcher m = p.matcher(inputString);
if (m.find()) {
////Found
}
else {
//Not found
}
But be aware of local settings...In France for example, the dot(.) used to separate the decimals is in fact a comma(,)
Use OnFocusChangeListener to achieve this.
//value pool
final String[] check = new String[]{"2.353","-2.354","4444.45"};
yourEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
String s = ((EditText)v).getText().toString().trim();
for(String tmp : check){
if(!TextUtils.isEmpty(s) && s.equals(tmp)){
//it ok
break;
}
else{
//do something
}
}
}
});
For edittext use android:inputType="number"
Convert the resulting string into an integer (e.g., Integer.parseInt(myEditText.getText().toString())`).
android:inputType="numberDecimal|numberSigned"
I want to get the user input for the EditText view and display it on the screen through TextView when the Button is clicked. I also, want to know what modifications can be done on the string.xml file to do this.
I didn't get the second question, maybe you can elaborate...but for your first query.
String content = edtEditText.getText().toString(); //gets you the contents of edit text
tvTextView.setText(content); //displays it in a textview..
I'm just beginner to help you for getting edittext value to textview. Try out this code -
EditText edit = (EditText)findViewById(R.id.editext1);
TextView tview = (TextView)findViewById(R.id.textview1);
String result = edit.getText().toString();
tview.setText(result);
This will get the text which is in EditText Hope this helps you.
EditText ein=(EditText)findViewById(R.id.edittext1);
TextView t=new TextView(this);
t.setText("Your Text is="+ein.getText());
setContentView(t);
bb.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
String s1=tt.getText().toString();
tv.setText(s1);
}
}
);
First get the text from edit text view
edittext.getText().toString()
and Store the obtained text in a string, say value.
value = edittext.getText().toString()
Then set value as the text for textview.
textview.setText(value)
yesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
eiteText=(EditText)findViewById(R.id.nameET);
String result=eiteText.getText().toString();
Log.d("TAG",result);
}
});
Easiest way to get text from the user:
EditText Variable1 = findViewById(R.id.enter_name);
String Variable2 = Variable1.getText().toString();
in "String.xml" you can notice any String or value you want to use, here are two examples:
<string name="app_name">My Calculator App
</string>
<color name="color_menu_home">#ffcccccc</color>
Used for the layout.xml: android:text="#string/app_name"
The advantage: you can use them as often you want, you only need to link them in your Layout-xml, and you can change the String-Content easily in the strings.xml, without searching in your source-code for the right position.
Important for changing language, you only need to replace the strings.xml - file
Use the following code when clicked on the button :
String value = edittext.getText().toString().trim(); //get text from editText
textView.setText(value); //setText in a textview
Hope to be useful to you.
Try this->
EditText text = (EditText) findViewById(R.id.text_input);
Editable name = text.getText();
Editable is the return data type of getText() method it will handle both
string and integer values
First get the value from edit text in a String variable
String value = edttxt.getText().toString();
Then set that value to textView
txtview.setText(value);
Where edttxt refers to edit text field in XML file
and txtview refers to textfield in XML file to show the value