How to use onSearchRequested() to disable Search button globally? - android

I posted a similar question: Android - How to disable Search button, how to implement onSearchRequested()? But at that time I was not very sure what I was looking for. But this time I'll be very specific.
What I am NOT looking for is: How I can use the dialog builder to disable the search button. This has been answered.
What I AM looking for is: How can I use the onSearchRequested() method to permanently disable the search button. Please take a look here http://www.youtube.com/watch?v=9lekcH1JAf0&feature=youtu.be for further elaboration.
In regards to this method, google states the following: "You can override this function to force global search, e.g. in response to a dedicated search key, or to block search entirely (by simply returning false)". Here is the link: http://developer.android.com/reference/android/app/Activity.html#onSearchRequested%28%29

#Override
public boolean onSearchRequested() {
return true;//block search
}
if is not working have you tried this solution?:
https://stackoverflow.com/a/5461709/1552551

Related

Intercept link LinkMovementMethod with a Yes/No dialog

I have a standard LinkMovementMethod established in my TextView to push a web Activity of some sort when the user touches a link. However, I want to establish a "do you want to see the link" dialog rather than taking the user straight to the webpage. I've tried overriding the touch methods but it all gets a little convoluted. A little help?
You can accomplish it in two ways:
Create custom Spans: more complicated, but you can accomplish more customised text consisting of clickable parts (or bold, differently coloured etc). To know more, check out ClickableSpan and SpannableStringBuilder
Extend LinkMovementMethod to accept custom click listener
In my opinion second solution is better in basic cases like yours. Here is how you can do it:
Copy this java class: InternalLinkMovementMethod to your project
Add set the link movement method of your TextView to this custom one, providing a click listener:
OnLinkClickedListener clickListener = new OnLinkClickedListener() {
#Override
public boolean onLinkClicked(String linkText) {
// here you can handle your click, eg show the dialog
// `linkText` is the text being clicked (the link)
// return true if handled, false otherwise
}
}
yourTextView.setMovementMethod(new InternalLinkMovementMethod(clickListener));

master/detail template with action menu - right way to show both

Beginner here, targetting sdk v14 and v17 for my learning...no need for older support.
I am using the master/detail template and trying to get an action menu (for SEARCH) to show up both in phone and tablet view. Actually I can get it to work, but I have to duplicate up my code in both ItemDetailActivity.java and ItemListActivity.java
These are the methods that I have to have in both for SEARCH to work:
public boolean onQueryTextChange(String newText) {
public boolean onQueryTextSubmit (String query) {
public boolean onClose () {
I only want to search the "detail", not the "list".
So my question: is there a way to associate the action bar with only the list fragment? That way I can keep the search functions in 1 file.
Thanks!
I'll go ahead and answer what I (think) I know as I don't want to leave this question open.
From tracing in the debugger, it looks to me like the phone activity and the tablet activity are separate and if you want to hook up an actionmenu, you have to hook it up to both separately.

How to launch suggestions immediately in Android searchable before the user has even started typing

I'm doing some research on ContentProviders and Searchable configurations. I've set up a class that extends a Content Provider with a database that provides suggestions from a database as the user types. This uses the Search Manager paradigm (not a SearchView).
Up to this point, everything works great.
What I'd like to do and am having problems with is to display some suggestions before the user starts typing, after he launches the search. Setting the property 'android:searchSuggestThreshold="0" ' in the searchable.xml only works if the user actually taps into the search textbox after launching it - I would like to display suggestions immediately after the search has been launched (i.e. not wait for the user to do anything else).
Any ideas?
Edit: An example of what I'm talking about is the search functionality in the Google Play Store app - right when a user taps the Magnifying glass for search, a list of recent suggestions immediately pops up.
Well after spending many hours on this I have finally found a way to achieve what I needed. Here's the answer so perhaps fewer other developers suffer:
The SearchView for the search mode has a text changed listener, and only when the text changes does it fire off a request for suggestions. Obviously, for the initial state nothing has changed so it doesn't request suggestions through the query() function.
In order to "trick" the SearchView into thinking the text has changed, you can do the following:
#Override
public boolean onSearchRequested()
{
final SearchManager searchManager = (SearchManager) getSystemService( SEARCH_SERVICE );
searchManager.startSearch( " ", true, getComponentName(), null, false );
return super.onSearchRequested();
}

How to use InputConnectionWrapper?

I have an EditText. Now I want to get all changes made by the user to this EditText and work with them before manually inserting them into the EditText. I don't want the user to directly change the text in the EditText. This should only be done by my code (e.g. by using replace() or setText()).
I searched a bit and found an interesting class named InputConnectionWrapper. According to the javadoc it shall act as a proxy for a given InputConnection. So I subclassed it like this:
private class EditTextInputConnection extends InputConnectionWrapper {
public EditTextInputConnection(InputConnection target, boolean mutable) {
super(target, mutable);
}
#Override
public boolean commitText(CharSequence text, int newCursorPosition) {
// some code which takes the input and manipulates it and calls editText.getText().replace() afterwards
return true;
}
}
To initialize the wrapper I overwrote the following method in my EditText-subclass:
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection con = super.onCreateInputConnection(outAttrs);
EditTextInputConnection connectionWrapper = new EditTextInputConnection(con, true);
return connectionWrapper;
}
However, commitText() never gets called. The onCreateInputConnection() gets called and the constructor of EditTextInputConnection also, but never commitText(), altough it should be, when I enter some text into the field. At least, that's how I understand the usage of InputConnectionWrapper. Or am I wrong?
Edit: It seems, that commitText() is only called for special characters like "."," " etc. As I understand the Android sourcecode for all other characters InputConnectionWrapper.sendKeyEvent() should be called, but that's not the case... I'm absolutely stuck at this point. I already tried EditText.onKeyPreIme(), but this only works on hardware keyboards. So that's no alternative... I don't really understand, why Android handles soft keyboards that different from hardware keyboards.
EditText.onTextChanged() gets also fired on non-user input, so this is also not, what I'm looking for.
It turned out, that the above usage of the InputConnectionWrapper was totally correct. However, commitText() gets never called (except for special cases), as there are other methods, which are used during typing. These are mainly setComposingText() and sendKeyEvent(). However, it is also important to overwrite seldom used methods like deleteSurroundingText() or commitText() to make sure to catch every user input.
Blundell suggested on the chat that you use a TextWatcher. Check if this helps you out.
Use a TextWatcher, disconnect it when you're modifying your edittext and reconnect it when done. This way, you won't trigger infinite calls.

Android: How to enable my button back if EditText is not empty?

I have 2 EditTexts; 01 and 02. My button will be disabled once the activity is started and when these two EditText contain text, the button has to be enabled again. However my button is always disabled and can't enable it using button.setEnabled(true);.
Can anyone help me with this?
summit.setEnabled(false);
buttonEnable();
public void buttonEnable(){
if (feedback.length()>0 && email.length()>0){
summit.setEnabled(true);
}else{
summit.setEnabled(false);
}
}
You're correct about needing a TextWatcher. The afterTextChanged(Editable) method is the one you're interested in for something like this. Call your buttonEnable() method from it, and add the TextWatcher to any applicable text fields. (Looks like feedback and email from your sample.)
One easy way can also be to set onKeyListener to your editText(), then if there is something in editText(), set button enable if nothing disable it.

Categories

Resources