Category Archives: Java

Path.combine in Java

In Dotnet and in particular in C# you can to Path.Combine

public static string Combine(
string path1,
string path2
);

This is a simple way to do something similar in Java:

public final class Path {
private Path() { }
public static String combine(final String... paths){
if(paths == null || paths.length == 0) {
return null;
}

File file = new File(paths[0]);
final int len = paths.length;
for (int i = 1; i < len; i++) {
file = new File(file, paths[i]);
}

return file.getPath();
}
}

Digg thisBuffer this pageShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrTweet about this on Twitter

Alpha numeric input filter

In Android you should know that it’s possible to specify input filters in EditText.
You should also know that it’s possible to create custom input filters. In this post I wil show how to create an Alpha numeric input filter.

First create a class that implements InputFilter

In this class you will have to implement the filter method.

In this method you will have to see if chars in source argument are valid. Every time a char is not valid you will have to return “” and ever time a char is valid you will have to return null.

this is a sample of what should be done:

public class AlphaNumericFilter implements InputFilter {
private Context _context;

public AlphaNumericFilter(Context context) {
this._context = context;
}

@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {

for (int i = start; i < end; i++) {
if (!Character.isLetterOrDigit(source.charAt(i))) {
return “”;
}
}
return null;
}
}

To use it it’s simple.

For a EditText just do this:
this._editText = (EditText) findViewById(R.id.edit_text);
this._editText.setFilters(new InputFilter[]{ new AlphaNumericFilter(MyActivity.this)});

For a EditTextPreference just do this:
this._editText = (EditTextPreference) prefs.findPreference(“edit_text”);
this._editText.getEditText().setFilters(new InputFilter[]{new AlphaNumericFilter(MyPreferenceActivity.this) });;

Digg thisBuffer this pageShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrTweet about this on Twitter