Making custom search suggestions in ActionBarSherlock search bar - android

This is how I do my basic search using the ActionBar Search widget. This is obviously the easy way where the suggestions are provided in a listView in the layout. But I want the suggestions inside the search box itself. Though it was possible to do it in a normal search box, how do I do the same using Actionbar Search box.
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
menu.add(0, 1, 1,"Search").setIcon(R.drawable.ic_search_inverse).setActionView(R.layout.collapsible_edittext).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
switch (item.getItemId()) {
case 1:
search = (AutoCompleteTextView) item.getActionView();
search.addTextChangedListener(filterTextWatcher);
search.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
return true;
}
return false;
}
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// your search logic here
doGeoSearch(String.valueOf(s));
}
};
public void doGeoSearch(String query){
Geocoder geocoder;
ArrayList<Address> addresses;
ArrayList<String> address = new ArrayList<String>() ;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = (ArrayList<Address>) geocoder.getFromLocationName(query, 6);
Log.d("Address",String.valueOf(addresses));
for(int i = 0;i<addresses.size();i++)
{
String addr = new String();
addr.concat(addresses.get(i).getAddressLine(0));
addr.concat(addresses.get(i).getAddressLine(1));
addr = addresses.get(i).getAddressLine(0) + addresses.get(i).getLocality() + addresses.get(i).getAdminArea();
//addr.concat(addresses.get(i).getAddressLine(2));
Log.d("addr",addr);
address.add(addr);
}
SearchAddressAdapater addressList = new SearchAddressAdapater(getApplicationContext(),R.layout.search_list,addresses, LocationActivity.this);
//addressView.setAdapter(addressList);
//ListView addressListView = new ListView();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

You can add a search widget to your ActionBar Sherlock, the search dialog has this functionality and it is very simple to implement as it is a simple expandable action item.
This tutorial will show you how to do everything you need with the search widget including search suggestions

Even though the SearchView was implemented and works, the search suggestions work on newer devices, but don't work on older devices like Gingerbread. Here's an issue:
https://github.com/JakeWharton/ActionBarSherlock/issues/659

Related

android, block special character in searchview

I'm a newbie on programing specially with android.
I use a SearchView in my app but I don't know how block special character.
I have seen topics about EditText but nothing about SearchView and I need help.
I use SearchView in App bar with this xml for menu :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/search"
android:icon="#drawable/ic_search_white_24dp"
android:title="#string/search_title"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
</menu>
and this Activity code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_option_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
So I waiting your answers, thanks.
You can detect when a char is entered:
//In onCreate
EditText et;//initialize
et.addTextChangedListener(tw);
outside onCreate:
private TextWatcher tw = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//If the text contains a "bad" char, (char that isn't equal to A-Z/a-z or 0-9, remove it from the edittext
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
};
(the edittext should be declared as a class variable)
Suggestion as to detection:
//s is the char sequence
String string = s.toString();
boolean found = false;
StringBuilder sb = new StringBuilder();
for(int i = 0; i < s.length(); i++){
String ch = s.charAt(i) + "";//Convert to check with regex
if(!Pattern.compile("[A-Z|a-z|0-9]").matcher(ch).find()){
found = true;
}else{
sb.append(ch);
}
}
if(found){
editText.setText(sb.toString());//only apply the text if a special char is found
}
My interpretation of special char was non-numeric and non-A-Z. This can be modified to match any char you count as special. If there is a few chars you want to disallow, it is better to check for those chars instead of checking if it contains all the others.
I've had this issue with '>' being converted to '|>'. This is how I fixed it with Kotlin:
override fun onQueryTextChange(newText: String?): Boolean {
Log.w(TAG, "new text $newText")
if (newText != null && newText.contains("|>")) {
val text = newText?.replace("|>", ">")
search.setQuery(text, false)
} else {
// do something
}
return false
}

EditText not adding space after word swiped in

I have an android phone using googles keyboard. On any EditText field in any other app if I use the swipe method to enter text in, it adds a space after each word. However, I have written my own app and when I use the swipe method to enter text on my EditText field it does NOT add a space sothewordsbleedtogether. This is very annoying.
I have an AlertDialog with a linear view added. On that linear view there is a text EditText. Here is my code to create the EditText and add it to the view:
final EditText titleBox = new EditText(this);
titleBox.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_CORRECT |
InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES |
InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE);
titleBox.setHint("Title");
layout.addView(titleBox);
Any ideas why its not adding spaces in between my words?
This was marked as a possible duplicate, but that question was about not allowing the first character to be a space....Im asking about allowing a space after words that are entered via a keyboard swipe.
Update
Here is the entire method of similar page, its having the same issue, its slightly less complex then the initial page I was troubleshooting. This one doesn't even have a LinearLayout associated:
private void addBudget(final Budget budget) {
EditText taskEditText = new EditText(this);
taskEditText.setId(R.id.add_budget_text);
taskEditText.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
String dialogTitle = "Add budget";
final AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle(dialogTitle)
.setMessage("What do you want to call this budget?")
.setView(taskEditText)
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// final String task = ;
SQLiteDatabase db = mHelper.getWritableDatabase();
Budget lBudget = new Budget();
if (budget != null) {
lBudget = budget;
}
EditText taskEditText = (EditText) findViewById(R.id.add_budget_text);
lBudget.title = String.valueOf(taskEditText.getText());
// Init custom budget object //new Budget(){{ title=task; id = budgetID;}}
int retId = mHelper.saveBudget(db, lBudget);
db.close();
int retRow = updateUI(retId);
mTaskListView.smoothScrollToPosition(retRow);
}
})
.setNegativeButton("Cancel", null)
.create();
// Handle done on soft keyboard
taskEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
int result = actionId & EditorInfo.IME_MASK_ACTION;
if (result == EditorInfo.IME_ACTION_DONE) {
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
return true;
}
return false;
}
});
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
}
I didnt know if you got solved, i just had the same problem today and found a way to solve it.
I saw a "extrange" performance of the swipe, sometimes it showed the "blankspace" and sometimes not.
The way i found to check if it was shown and if it didnt, add it, was this:
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) {
checkCancel();
int compare = count-before;
if(compare>1){
String text = editText.getText().toString();
String lastChar = (text.substring(text.length()-1,text.length()));
if(!lastChar.equals(" ")){
String plus = text+" ";
editText.setText(plus);
editText.setSelection(editText.getText().length());
}
}
}
#Override
public void afterTextChanged(Editable s) {
}
} );
You can see, onTextChanged can use the variables "before" and "count" and if the compare (difference between last word and current one) is more than 1, it's a word entered by Swipe. Then you can check if the "blankspace" is shown, and if not, just add it and perfom anything you want with it.
Hope it helps!
Could you try this? Add the filter into the editText. I used it for enter code on my app.
EditText et = (EditText) findViewById(R.id.edit_text);
et.setFilters(new InputFilter[] {
new InputFilter() {
#Override
public CharSequence filter(CharSequence charSequence, int start, int end, Spanned spanned, int start, int end {
if(charSequence.equals("")){
return charSequence;
}
if(charSequence.toString().matches("[a-zA-Z ]+")){
return charSequence;
}
return "";
}
}
});
So I uninstalled the google keyboard and reinstalled and I changed the build to release. One of those two things fixed it.

Activate ActionBar Search Once the Activity Started

I am creating a custom Android ActionBar search following this tutorial.
The handleMenuSearch(); is activated on onOptionsItemSelected.
protected void handleMenuSearch(){
ActionBar action = getSupportActionBar(); //get the actionbar
if(isSearchOpened){ //test if the search is open
action.setDisplayShowCustomEnabled(false); //disable a custom view inside the actionbar
action.setDisplayShowTitleEnabled(true); //show the title in the action bar
//hides the keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edtSeach.getWindowToken(), 0);
//add the search icon in the action bar
mSearchAction.setIcon(getResources().getDrawable(R.drawable.search));
isSearchOpened = false;
} else { //open the search entry
action.setDisplayShowCustomEnabled(true); //enable it to display a
// custom view in the action bar.
action.setCustomView(R.layout.search_bar);//add the custom view
action.setDisplayShowTitleEnabled(false); //hide the title
edtSeach = (EditText)action.getCustomView().findViewById(R.id.edtSearch); //the text editor
edtSeach.requestFocus();
edtSeach.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
loadData(edtSeach.getText().toString());
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
//open the keyboard focused in the edtSearch
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edtSeach, InputMethodManager.SHOW_IMPLICIT);
//add the close icon
mSearchAction.setIcon(getResources().getDrawable(R.drawable.ximage));
isSearchOpened = true;
}
}
What I want to achieve is to activate the searching once the activity is started.
I tried to put handleMenuSearch(); under onCreate but this gives me NullPointerException
The reason for this I guess is because I cannot call mSearchAction from onCreate.
public boolean onPrepareOptionsMenu(Menu menu) {
mSearchAction = menu.findItem(R.id.search);
return super.onPrepareOptionsMenu(menu);
}
Problem solved by using Handler.
protected void onResume() {
super.onResume();
new Handler().postDelayed(new Runnable() {
public void run() {
handleMenuSearch();
}
}, 1000);
}

Using LinkMovementMethod and Native copy/paste in Textview - Android

I have a TextView with html text and I need native copy/paste and clickable links.
I have used the next code, but when I use
setMovementMethod(LinkMovementMethod.getInstance());
the native copy/paste stops to work.
If I change setMovementMethod(LinkMovementMethod.getInstance())
to ArrowKeyMovementMethod, the copy/paste works but the click links stops to work.
I donĀ“t posted all code but setMovementMethod is used in updateDetail method.
Does someome can help me?
Regards, Luiz
My code is:
textDetail = (TextView) view.findViewById(R.id.text_detail);
textDetail.setTextIsSelectable(true);
textDetail.setCustomSelectionActionModeCallback(new ActionMode.Callback()
{
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
return false;
}
});
protected void makeLinkClickable(SpannableStringBuilder strBuilder,
final URLSpan span) {
int start = strBuilder.getSpanStart(span);
int end = strBuilder.getSpanEnd(span);
int flags = strBuilder.getSpanFlags(span);
ClickableSpan clickable = new ClickableSpan() {
#Override
public void onClick(View view) {
}
////////
else {
///////////////
}
}
};
strBuilder.setSpan(clickable, start, end, flags);
strBuilder.removeSpan(span);
}
protected void setTextViewHTML(TextView text, String html) {
CharSequence sequence = Html.fromHtml(html);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
URLSpan[] urls = strBuilder.getSpans(0, sequence.length(),
URLSpan.class);
for (URLSpan span : urls) {
makeLinkClickable(strBuilder, span);
}
text.setText(strBuilder);
}
public void updateDetail(String msg) {
setTextViewHTML(textDetail, msg);
textDetail.setMovementMethod(LinkMovementMethod.getInstance());
}

Android lollipop can`t get keyboard input

I have user keyboard input working on all android versions except on Android Lollipop (5.0).
I have used this to open software keyboard:
public static void OpenKeyBoard(){
MainActivity._Instance.runOnUiThread(new Runnable() {
public void run() {
InputMethodManager imm = (InputMethodManager) MainActivity._Instance.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(MainActivity._Instance.getWindow().getDecorView(), 0);
}
});
}
and i get user input by standard event :
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
I have this code working for all pre Lollipop versions of Android. When I use it on Lollipop, the software keyboard appears, but when I try to click on any letter/number, the keyboard disappears and the method "onKeyDown" doesn`t receive any keycode.
Did anyone had this problem? Any opinion how to solve this?
Thank you.
Try updating your google SDK to the latest, I did and this fixed any issues I have with the keyboard dismissing.
I has this problem too. I managed to solve this by overriding the onLayout, onFocusChanged & onCheckIsTextEditor methods inside WebView. Here's the code that made it work (I generate a webview programmatically):
this.webView = new WebView(context)
{
boolean layoutChangedOnce = false;
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
if (!layoutChangedOnce)
{
super.onLayout(changed, l, t, r, b);
layoutChangedOnce = true;
}
}
#Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect)
{
super.onFocusChanged(true, direction, previouslyFocusedRect);
}
#Override
public boolean onCheckIsTextEditor()
{
return true;
}
};
this.webView.setFocusable(true);
this.webView.setFocusableInTouchMode(true);
this.webView.requestFocus(View.FOCUS_DOWN);
addView(this.webView);
this issue is not limited to just android applications but also occurs when any keyboard related event is accessed using javascript or jquery within a phone browser.
eg. I have my website which asks a user to enter zip code(numeric characters) on a certain page. Now the problem is when up tap on the input box, the numeric keyboard appears but it allows you to paste alpha-numeric characters in it as well.
Reason searched so far,
Try accessing this through your desktop and also a mobile device running android OS 5.0+ and try different possible keystrokes.
Note:
When you enter any alpha-characters using mobile device with above
mentioned configuration it shows up '229' as the keycode.
When you enter any alpha-characters using a desktop it shows up the
appropriate keycode.
This works for me I have a SurfaceView implements SurfaceHolder.Callback with a thread to do drawing
#Override
public boolean onTouchEvent(MotionEvent event)
{
synchronized (this)
{
mouse.useEvent(event);
}
return true;
}
#Override
public InputConnection onCreateInputConnection(EditorInfo editorinfo) {
BaseInputConnection bic = new BaseInputConnection(this, false);
editorinfo.actionLabel = null;
editorinfo.inputType = InputType.TYPE_NULL;
editorinfo.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN;
bic.finishComposingText();
return bic;
}
#Override
public boolean dispatchKeyEvent(KeyEvent event)
{
input.useEvent(event);
return super.dispatchKeyEvent(event);
}
I have solved this using AlertDialog editText programmatically. Eg:
public void KeyboardTextField(int id){
if (id == 0){
final String title = getStringResourceByName("profile_title");
final String createP = getStringResourceByName("profile_confirm");
final String cancel = getStringResourceByName("profile_cancel");
MainActivity._Instance.ActivityWillBeShown = true;
MainActivity._Instance.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity._Instance);
alert.setCancelable(false);
alert.setTitle(title);
// alert.setMessage(""); // message
// Set an EditText view to get user input
final EditText input = new EditText(MainActivity._Instance);
final int maxLength = 12;
input.setFilters(new InputFilter[] {new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
if (source != null && blockCharacterSet.contains(("" + source))) {
return "";
}
// TODO Auto-generated method stub
return null;
}
} , new InputFilter.LengthFilter(maxLength)});
//input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_NUMBER);
input.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); //turn off txt auto complete
input.setInputType(InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS); // type only text,numbers and some special char
alert.setView(input);
//input.setText("Player");
alert.setPositiveButton(createP, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
// Do something with value!
if (value.isEmpty()){
value = "Player";
}
nativeName(value);
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
}
});
alert.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
}
});
alert.show();
};
});
}
And Whenever you need keyboard you just call this function. It works on all Android versions (4.x, 5.x, 6.0)

Categories

Resources