onTextChanged custom filter german umlauts - android

Im using a Filter for an ArrayAdapter. Now I would like to have following Feature:
For Example my Listview contains the word käuflich and the search Input is kauf, käuflich should be displayed as result. So ö,ä,ü should be replaced additionally by o,a,u.
Is this possible?
This is my simple filter
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Activity.this.adapter.getFilter().filter(cs);
}
Thanks

You can use the java.text.Normalizer to remove the accents. When iterating over list items in Filter.performFiltering(CharSequence), do the following:
for(int i = 0; i<Original_Names.size(); i++){
filterableString = Original_Names.get(i);
String s = Normalizer.normalize(filterableString, Form.NFD);
// Remove Accents
String withoutAccents =
s.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
if(withoutAccents.toLowerCase().contains(filterString)){
Filtered_Names.add(filterableString);
}
}
A word of caution here: Link

Related

How to create xls sheet and add dynamic data into it in Android?

This my code and I want to add data I got from an EditText each time dynamically.
//New Workbook
Workbook wb = new HSSFWorkbook();
Cell c = null;
//Cell style for header row
CellStyle cs = wb.createCellStyle();
cs.setFillForegroundColor(HSSFColor.LIME.index);
cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//New Sheet
Sheet sheet1 = null;
sheet1 = wb.createSheet("MYtest");
// Generate column headings
Row row1=sheet1.createRow(0);
c = row1.createCell(0);
c.setCellValue("ENTRY ONE");
c.setCellStyle(cs);
c = row1.createCell(1);
c.setCellValue("ENTRY TWO");
c.setCellStyle(cs);
c = row1.createCell(2);
c.setCellValue("ENTRY THREE");
c.setCellStyle(cs);
If you want to modify a cell value with the data that you entered in an EditText dynamically, you should use a `TextWatcher to set the text in the cell. See example below:
editText.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) {
}
#Override
public void afterTextChanged(Editable s) {
row1.getCell(1).setCellValue(s.toString());
}
});
Although, depending on how you visualise the XLS file, the change can't be considered dynamic since you have to export the document and then view it.
You have to use apache POI.
Download apache POI jar file or
add line into the gradle file.
compile 'org.apache.poi:poi:3.9'
And create, read, write excel file.
below is the sample tutorial for your reference click here

Input filter not filter one by one in android

I use InputFilter to check my String from editText.
final InputFilter filter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start,
int end, Spanned dest, int dstart, int dend) {
int num = 0;
try {
num = Integer.parseInt(source.toString());
if (!(num > 0 && num < 30)) {
return "";
}
} catch (Exception e) {
}
return null;
}
};
etStatDaysCount = (EditText) inflate2.findViewById(R.id.et_settings_days_to_statistics);
etStatDaysCount.setFilters(new InputFilter[]{filter});
But I have a problem. I want to check if user put number <0 or >30 I want to replace this text. This filter check number one by one. So if I put number 56, he check number 5 and after that 6 so this filter let user put number 56 in edit text. I want to check all string which I put in edittext. So if I put 56 he should check number 5 and after that check number 56. How can I do that?
It seems like you're answering your own question. If the problem is that the filter checks the numbers one by one then don't use it! Just grab the value from the EditText whenever you're ready to test it and make sure it meets your criteria.
etStatDaysCount = (EditText) inflate2.findViewById(R.id.et_settings_days_to_statistics);
//when you're ready to look at the value...
String input = etStatDaysCount.getText().toString()
if(input.trim().equals("") || input.matches("\\D") || Integer.valueOf(input) < 0 || Integer.valueOf(input) > 30)
//this is a value that you don't want
Use TextChangeListener. It is fired up whenever an input is made to editText.
etStatDaysCount = (EditText) inflate2.findViewById(R.id.et_settings_days_to_statistics);
etStatDaysCount.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3 ) {
// you can check every number/alphabet of input here
}
If you don't want to check each individual character in your input, you can always do like:
String yourInput = etStatDaysCount.getText().toString();
if(Integer.valueOf(yourInput) <0 && Integer.valueOf(yourInput) >30 )
// undesired value
Hope this helps

set spaces between letters in textview

is there a way to set a custom space (in pixels) between letters to an editText? I found only how to set spaces between the lines, but bot between letters on the same row
Using android:letterSpacing i was able to add spacing between characters in a textview
<android.support.v7.widget.AppCompatTextView
android:id="#+id/textViewValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:letterSpacing="0.35"
android:maxLines="1" />
Related methods:
setLetterSpacing(float)
I had to do this myself today so here are some updates about this problem :
From API 21 you can use XML attribute android:letterSpacing="2" or from code myEditText.setLetterSpacing(2);
Before API 21, use a TextWatcher with the following code
private static final String LETTER_SPACING = " ";
private EditText myEditText;
private String myPreviousText;
...
// Get the views
myEditText = (EditText) v.findViewById(R.id.edt_code);
myEditText.addTextChangedListener(this);
...
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Nothing here
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Nothing here
}
#Override
public void afterTextChanged(Editable s) {
String text = s.toString();
// Only update the EditText when the user modify it -> Otherwise it will be triggered when adding spaces
if (!text.equals(myPreviousText)) {
// Remove spaces
text = text.replace(" ", "");
// Add space between each character
StringBuilder newText = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
if (i == text.length() - 1) {
// Do not add a space after the last character -> Allow user to delete last character
newText.append(Character.toUpperCase(text.charAt(text.length() - 1)));
}
else {
newText.append(Character.toUpperCase(text.charAt(i)) + LETTER_SPACING);
}
}
myPreviousText = newText.toString();
// Update the text with spaces and place the cursor at the end
myEditText.setText(newText);
myEditText.setSelection(newText.length());
}
}
You could implament a custom TextWatcher, and add X spaces every time the user enteres 1.
i have used this, and works for most API levels if not all of them.
KerningViews
Provides a set of views which allows to adjust the spacing between the characters of that view, AKA, Kerning effect.
https://github.com/aritraroy/KerningViews

How can I get the ID by name in a spinner?

I want to get id of particular name that i selected in spinner.i did like this but it will print "_id" how can i retrieve id of particular name.
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String sel_name,numbers;
//String[] name=new String[]{arg0.getItemAtPosition(arg2).toString()};
sel_name=arg0.getItemAtPosition(arg2).toString();
String[] p=new String[]{People._ID};
Cursor cur=getContentResolver().query(People.CONTENT_URI, p,People.NAME+"= '"+sel_name+"'" ,null , null);
String r=cur.getString(cur.getColumnIndex(People._ID));
Toast.makeText(arg0.getContext(),r, Toast.LENGTH_LONG).show();
}
You need to add a cursor.moveToFirst(); call before you use the cursor.
If you are refering to sel_name, then this is what you should do to get the view's id:
sel_name = String.valueOf(arg0.getChildAt(arg2).getId());
add following after cursor
while(cur.moveToNext)
{
String r=cur.getString(cur.getColumnIndex(People._ID));
}
by id of perticular name if you mean a string den avoid to do so . alw
ays use id as int only ,because R file have int entries only , so will be easy to handle .
for this i guess in onItemSelected
arg1.getId() will work ........
or try setTag(id) in get view of customAdapter of spinner , then in onItemSelected int id = arg1.getTag();

Android: How to read a number as int from a String; basically to read Text of a ListViewItem?

This is my problem.
I have a ListView, each row is a CheckedTextView.
The list view items are "1", "2" and "3".
When a ListItem is clicked, I want to read the number and assign it to an int variable.
I did the following to read the Text of the clicked item:
onItemClick(AdapterView<?> parent, View v, int position, long id) {
int num = 0; //initialise to 0
CharSequence s = ((TextView)v).getText();
// s contains the number, how to get it into num?
}
Basically, I want the number read in s to be converted and given to num.
I know it maybe simple, but please help if you have an answer..
Regards,
Kiki
String aString = "78";
int aInt = Integer.parseInt(aString);

Categories

Resources