Customize android search widget for scoped search - android

I want to implement search filter in my android application. I have gone through the examples on how to integrate search filter and able to understand how to integrate it in application. But my requirement is to provide scoped search based a filter while doing search. I have tried to search similar implementations but was not able to find any examples. Please check section Scoped Search in this UI Pattern collection, especially Dropbox example for iphone.
As mentioned before I was unable to find similar example in android but by looking at Dictionary.com 's application (snapshot shown below) I came to know that its possible in android also (of course by adding some more efforts in case its not possible with Search Widget itself). Can any one please provide any directions how I can implement similar scoped search in my application ?
Thanks for spending time on this.

I would do the following:
first i create a searchType layout for the alertdialog with the choose: (images, video, etc..)
then i create the activity for the search and implement the widget(like on android guide).
in the activity create a variable:
private String searchType = "";
then
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.YOUR_MENU, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat
.getActionView(menuItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
//HERE INSERT THE CODE ABOUT THE ALERT DIALOG FOR THE CHOOSE
//THEN INSERT THE aALERT DIALOG RESPONSE INTO THE searchType VARIABLE
}
}

I have created a custom search widget that does not make use of the built in search functionality. Its simple to implement and can provide information to the current activity.
It also uses an Autocomplete textview so you can use Autocomplete, you could alternatively just replace this with a normal EditText.
public class CustomViewSearch extends View {
private CustomAutoCompleteView searchEditText;
private boolean viewShown = false;
private ActionBar actionBar;
private InputMethodManager inputMethodManager;
private OnEditorActionSearchListener onEditorActionSearchListener;
private List<String> dataItems;
private ArrayAdapter<String> arrayAdapter;
public interface OnEditorActionSearchListener {
void onEditorActionSearch(String searchText);
void onTextChangedListener(String text);
List<String> getNewItemsForSuggestions(String text);
}
public void setOnEditorActionSearchListener(OnEditorActionSearchListener l) {
onEditorActionSearchListener = l;
}
// IMPORTANT: Provide your activity as the context
public CustomViewSearch(final Context context, List<String> items) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View searchView = inflater.inflate(R.layout.custom_view_search_widget, null);
actionBar = ((ActionBarActivity) context).getSupportActionBar();
inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(searchView);
actionBar.setDisplayShowCustomEnabled(true);
dataItems = items;
searchEditText = (CustomAutoCompleteView) searchView.findViewById(R.id.edit_text_search);
searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
onEditorActionSearchListener.onEditorActionSearch(searchEditText.getText().toString());
return true;
}
return false;
}
});
searchEditText.requestFocus();
searchEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
onEditorActionSearchListener.onTextChangedListener(s.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
dataItems = onEditorActionSearchListener.getNewItemsForSuggestions(s.toString());
// update the adapater
arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, dataItems);
arrayAdapter.notifyDataSetChanged();
searchEditText.setAdapter(arrayAdapter);
}
});
ImageButton closeImageButton = (ImageButton) searchView.findViewById(R.id.image_button_search_close);
closeImageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (searchEditText.getText().length() > 0) {
searchEditText.setText("");
} else {
hideKeyboard();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowCustomEnabled(false);
viewShown = false;
}
}
});
viewShown = true;
showKeyboard();
}
public String actionClick() {
if (!viewShown) {
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
searchEditText.requestFocus();
showKeyboard();
viewShown = true;
return null;
} else {
return getSearchText();
}
}
public void showKeyboard() {
inputMethodManager.showSoftInput(searchEditText, InputMethodManager.SHOW_IMPLICIT);
}
public void hideKeyboard() {
inputMethodManager.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
}
public String getSearchText() {
return searchEditText.getText().toString();
} }
The corresponding xml layout for the search :
<?xml version="1.0" encoding="utf-8"?>
<za.co.android.CustomAutoCompleteView android:id="#+id/edit_text_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/empty_layout"
android:paddingBottom="8dp"
android:textColor="#android:color/white"
android:singleLine="true"
android:imeOptions="actionSearch"
android:textCursorDrawable="#null"
android:paddingRight="36dp"
android:layout_alignParentLeft="true"
android:inputType="text"
android:hint="#string/search"
android:textColorHint="#color/palette_primary_light_grey"/>
<ImageButton
android:id="#+id/image_button_search_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/ic_menu_close_clear_cancel"
android:layout_toLeftOf="#+id/empty_layout"
android:layout_centerVertical="true"
android:layout_marginRight="4dp"/>
<FrameLayout
android:id="#+id/empty_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentRight="true">
</FrameLayout>
The custom autocomplete widget code:
public class CustomAutoCompleteView extends AutoCompleteTextView {
public CustomAutoCompleteView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomAutoCompleteView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public CustomAutoCompleteView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
// this is how to disable AutoCompleteTextView filter
#Override
protected void performFiltering(final CharSequence text, final int keyCode) {
String filterText = "";
super.performFiltering(filterText, keyCode);
}
/*
* after a selection we have to capture the new value and append to the existing text
*/
#Override
protected void replaceText(final CharSequence text) {
super.replaceText(text);
}}
Usage of the CustomViewSearch widget:
Add a search icon to the menu.xml for the Activity. Then in the onOptionsItemSelected method - when the menu item is clicked, call the below function:
public void triggerSearch() {
if (customViewSearch == null) {
customViewSearch = new CustomViewSearch(this, null);
customViewSearch.setOnEditorActionSearchListener(new CustomViewSearch.OnEditorActionSearchListener() {
#Override
public void onEditorActionSearch(String searchText) {
// DO SOME STUFF
((AppItemListFragment_) getSupportFragmentManager()
.findFragmentById(R.id.appitem_list)).searchTriggered(searchText);
}
#Override
public void onTextChangedListener(String text) {
((AppItemListFragment_) getSupportFragmentManager()
.findFragmentById(R.id.appitem_list)).searchTriggered(text);
}
#Override
public List<String> getNewItemsForSuggestions(String text) {
return getNewAutoCompleteStrings(text);
}
});
} else {
String searchText = customViewSearch.actionClick();
if (searchText != null) {
// DO SOME STUFF
((AppItemListFragment_) getSupportFragmentManager()
.findFragmentById(R.id.appitem_list)).searchTriggered(searchText);
}
}}
I hope this helps - for me it was much easier to implement a custom search than use the built in one and provides a scoped search for any screen you are on.

Related

OnClick for Floating Action Button

I'm new to working with floating action button and trying to get a few of the basic things working today. Currently I am stuck on getting the onClick functionality to work. I pulled most of the code from googles FAB basic example, and in there it has an onChecked method which sends a string to a logger to show you have clicked it.
#Override
public void onCheckedChanged(FloatingActionButton fabView, boolean isChecked) {
// When a FAB is toggled, log the action.
switch (fabView.getId()){
case R.id.fab_1:
break;
default:
break;
}
}
I was thinking I'd be able to replace the functionality in there but that had no affect. So I tried to create the onClickListener like you would with any other button but that also had no affect. I am not sure how to continue since neither option worked. my goal is just to produce a dialog when the floating action button is clicked, but for now I am just trying to use a placeholder alert dialog.
This is the FloatingActionButtonFragment class:
public class FloatingActionButtonFragment extends Fragment implements FloatingActionButton.OnCheckedChangeListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fab_layout, container, false);
// Make this {#link Fragment} listen for changes in both FABs.
FloatingActionButton fab1 = (FloatingActionButton) rootView.findViewById(R.id.fab_1);
fab1.setOnCheckedChangeListener(this);
fab1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Are you sure?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
AlertDialog dialog = builder.create();
dialog.show();
}
});
return rootView;
}
#Override
public void onCheckedChanged(FloatingActionButton fabView, boolean isChecked) {
// When a FAB is toggled, log the action.
switch (fabView.getId()){
case R.id.fab_1:
break;
default:
break;
}
}
}
And here is the FloatingActionButton class:
public class FloatingActionButton extends FrameLayout implements Checkable {
/**
* Interface definition for a callback to be invoked when the checked state
* of a compound button changes.
*/
public static interface OnCheckedChangeListener {
/**
* Called when the checked state of a FAB has changed.
*
* #param fabView The FAB view whose state has changed.
* #param isChecked The new checked state of buttonView.
*/
void onCheckedChanged(FloatingActionButton fabView, boolean isChecked);
}
/**
* An array of states.
*/
private static final int[] CHECKED_STATE_SET = {
android.R.attr.state_checked
};
private static final String TAG = "FloatingActionButton";
// A boolean that tells if the FAB is checked or not.
private boolean mChecked;
// A listener to communicate that the FAB has changed it's state
private OnCheckedChangeListener mOnCheckedChangeListener;
public FloatingActionButton(Context context) {
this(context, null, 0, 0);
}
public FloatingActionButton(Context context, AttributeSet attrs) {
this(context, attrs, 0, 0);
}
public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr);
setClickable(true);
// Set the outline provider for this view. The provider is given the outline which it can
// then modify as needed. In this case we set the outline to be an oval fitting the height
// and width.
setOutlineProvider(new ViewOutlineProvider() {
#Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, getWidth(), getHeight());
}
});
// Finally, enable clipping to the outline, using the provider we set above
setClipToOutline(true);
}
/**
* Sets the checked/unchecked state of the FAB.
* #param checked
*/
public void setChecked(boolean checked) {
// If trying to set the current state, ignore.
if (checked == mChecked) {
return;
}
mChecked = checked;
// Now refresh the drawable state (so the icon changes)
refreshDrawableState();
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this, checked);
}
}
/**
* Register a callback to be invoked when the checked state of this button
* changes.
*
* #param listener the callback to call on checked state change
*/
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
mOnCheckedChangeListener = listener;
}
#Override
public boolean isChecked() {
return mChecked;
}
#Override
public void toggle() {
setChecked(!mChecked);
}
/**
* Override performClick() so that we can toggle the checked state when the view is clicked
*/
#Override
public boolean performClick() {
toggle();
return super.performClick();
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// As we have changed size, we should invalidate the outline so that is the the
// correct size
invalidateOutline();
}
#Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}
}
There isn't much to either class at this point, they are mostly husks, but I just want to get this basic functionality down before I continue, and being the noob I am, I don't know why this wouldn't work.
If you are not already heading for deadline, you must change the floating action button to the one provided by google in design library
just follow http://android-developers.blogspot.in/2015/05/android-design-support-library.html
Add to the XML Layout:
<android.support.design.widget.FloatingActionButton
android:id="#+id/myFAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/your_icon"
app:elevation="4dp"
... />
Add to the code behind:
FloatingActionButton myFab = (FloatingActionButton) myView.findViewById(R.id.myFAB);
myFab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
doMyThing();
}
});
For more details follow : FloatingActionButton example with Support Library
Actually now with android support library it was very easy to add FAB and to customize it with click listeners
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// FAB Action goes here
}
});
Reference : http://androidgifts.com/android-material-design-floating-action-button-tutorial/
To use dialog/Alertdialog with the floating action button you're using, try changing your onClick(View v) from this
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
to
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());

How to make EditText read and copy only but not editable

I want to make my edit box read only but not editable.
User should able to copy from my Edit box but it should not be editable ny user.
please let me know how to do this.
The command text.setTextIsSelectable(true) requires API 11. For those using lower API's use the following XML:
android:inputType="none"
android:textIsSelectable="true"
This will make your editText selectable but not editable.
The easiest way to do this is to add this code:
textInput.setInputType(InputType.TYPE_NULL);
textInput.setTextIsSelectable(true);
textInput.setKeyListener(null);
Create a TextView as has been indicated by the other answer, instead of an EditText. Then override the Activity's context menu in your Activity class as below:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(0, v.getId(), 0, "Copy");
//cast the received View to TextView so that you can get its text
TextView yourTextView = (TextView) v;
//place your TextView's text in the clipboard
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(yourTextView.getText());
}
Then simply call registerForContextMenu(yourTextView); in onCreate().
You can overwrite the key listener, so you can do anything except editing
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
return true;
}
});
Use android:editable="false" property for the EditText in your layout view file.
i'm using this class
import android.content.Context;
import android.text.InputFilter;
import android.text.Spanned;
import android.util.AttributeSet;
import android.widget.EditText;
/*
*
* To make EditText read and copy only but not editable
* using
* sendReadOnlyCallback(callback);
*
*/
public class MyEditText extends EditText {
private InputFilter[] originalFilters = null;
private boolean internalChange = false;
private InputFilter[] myInputFilters = null;
private static ReadonlyCallback sDummyCallback = new ReadonlyCallback() {
#Override
public boolean isReadOnly() {
return false;
}
};
private ReadonlyCallback callback = sDummyCallback;
public MyEditText(Context context) {
super(context);
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public interface ReadonlyCallback {
public boolean isReadOnly();
}
public void setReadonlyCallback(ReadonlyCallback cb) {
if (cb == null)
callback = sDummyCallback;
else
callback = cb;
}
public void setFilters(InputFilter[] filters) {
// duplicated from TexView
originalFilters = new InputFilter[filters.length];
System.arraycopy(filters, 0, originalFilters, 0, filters.length);
// funny No. 1 : have to re instantiate `callback`
// otherwise got `NullPointerExcection` when called from `filter`
callback = sDummyCallback;
myInputFilters = new InputFilter[] { new InputFilter() {
// funny No. 2:
// have to make refs to `originalfilters`
// otherwise got `NullPointerExcection` when called from `filter`
InputFilter[] flts = originalFilters;
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (!internalChange && callback.isReadOnly())
return dest.subSequence(dstart, dend);
int filtercount = flts.length;
if (filtercount == 0)
return null;
// duplicated from InputFilter.AllCaps
for (int i = 0; i < filtercount; i++) {
CharSequence repl = flts[i].filter(source, start, end, dest, start, end);
if (repl != null) {
source = repl;
start = 0;
end = repl.length();
}
if (i == filtercount)
return repl;
}
return null;
}
} };
super.setFilters(myInputFilters);
}
#Override
public InputFilter[] getFilters() {
if (myInputFilters == null)
return super.getFilters();
return originalFilters;
}
#Override
public synchronized void setText(CharSequence text, BufferType type) {
internalChange = true;
super.setText(text, type);
internalChange = false;
}
}
Why not this?
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setEnabled(false);

ClassCastExcptionAndroid

MainActivity
public class MainActivity extends Activity {
// Declare our Views, so we can access them later
private CheckUsernameEditText etUsername;
private EditText etPassword;
private EditText etPassword2;
private Button btnRegister;
private Button btnCancel;
private TextView lblUserStatus;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set Activity Layout
setContentView(R.layout.activity_main);
// Get the EditText and Button References
etUsername = (CheckUsernameEditText) findViewById(R.id.username);
etPassword = (EditText) findViewById(R.id.password);
etPassword2 = (EditText) findViewById(R.id.password2);
btnRegister = (Button) findViewById(R.id.register_button);
btnCancel = (Button) findViewById(R.id.cancel_button);
lblUserStatus = (TextView) findViewById(R.id.userstatus);
// Set our new Listener to the Username EditText view
etUsername.setOnUsernameAvailableListener(new OnUsernameAvailableListener() {
#Override
public void onAvailableChecked(String username,
boolean available) {
// Handle the event here
if (!available) {
etUsername.setTextColor(Color.RED);
lblUserStatus
.setText(username
+ " is already taken. Please choose another login name.");
} else {
etUsername.setTextColor(Color.GREEN);
lblUserStatus.setText(username + " is available.");
}
}
});
// Set Click Listener
btnRegister.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// create Account
}
});
btnCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Close the application
finish();
}
});
}
The corresponding XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
*
*
<EditText
android:id="#+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
*
*
</LinearLayout>
CheckUsernameEditText
public class CheckUsernameEditText extends EditText implements OnKeyListener {
OnUsernameAvailableListener onUsernameAvailableListener = null;
final private static String[] registeredUsers = new String[] {
// This is just a fixed List for tutorial purposes
// in a real application you'd check this server sided or inside the
// database
"tseng", "admin", "root", "joedoe", "john" };
public CheckUsernameEditText(Context context) {
super(context);
// Set KeyListener to ourself
this.setOnKeyListener(this);
}
public CheckUsernameEditText(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// Set KeyListener to ourself
this.setOnKeyListener(this);
}
public CheckUsernameEditText(Context context, AttributeSet attrs) {
super(context, attrs);
// Set KeyListener to ourself
this.setOnKeyListener(this);
}
// Allows the user to set an Listener and react to the event
public void setOnUsernameAvailableListener(
OnUsernameAvailableListener listener) {
onUsernameAvailableListener = listener;
}
// This function is called after the check was complete
private void OnUserChecked(String username, boolean available) {
// Check if the Listener was set, otherwise we'll get an Exception when
// we try to call it
if (onUsernameAvailableListener != null) {
// Only trigger the event, when we have a username
if (!TextUtils.isEmpty(username)) {
onUsernameAvailableListener.onAvailableChecked(username,
available);
}
}
}
#Override
public boolean onKey(View v, int keycode, KeyEvent keyevent) {
// We only want to handle ACTION_UP events, when user releases a key
if (keyevent.getAction() == KeyEvent.ACTION_DOWN)
return false;
boolean available = true;
// Whenever a user press a key, check if the username is available
String username = getText().toString().toLowerCase();
if (!TextUtils.isEmpty(username)) {
// Only perform check, if we have anything inside the EditText box
for (int i = 0; i < registeredUsers.length; i++) {
if (registeredUsers[i].equals(username)) {
available = false;
// Finish the loop, as the name is already taken
break;
}
}
// Trigger the Event and notify the user of our widget
OnUserChecked(username, available);
return false;
}
return false;
}
// Define our custom Listener interface
public interface OnUsernameAvailableListener {
public abstract void onAvailableChecked(String username,
boolean available);
}
}
The problem is that I take a classclastexception. Because I declare on the xml the username as edittext and in the main code i declare it as CheckUsernameEditText. How I can solve this problem?Why the casting isn't working, especially now that the CheckUsernameEditText extends the EditText class?
All CheckUsernameEditText objects are EditText objects,
but not all EditText objects are CheckUsernameEditText objects.
You should use your custom class in the XML:
<your.package.name.CheckUsernameEditText
android:id="#+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
I believe if you have a custom View (in your case CheckUsernameEditText) you have to declare it as such in the XML... Remember that as #Sam points out you can't cast downward into a derived class, you can only cast upward into a parent class, so you can always cast your CheckUsernameEditText View up to an EditText (or just View) but you can't go the other way.

Using native look and feel of the searchView for actionBarSherlock on old Android versions

I've noticed that there is no working solution that shows how to use filter on a listView items using the actionbar, that works on older Android versions (like 2.3.x).
The only example I've found is in the file "LoaderCursorSupport.java" of the fragments example. However, it only works when the searchView can be created, meaning starting from Android 3.x, as shown in the code:
View searchView=SearchViewCompat.newSearchView(getActivity());
if(searchView!=null)
...
The above bug (or missing feature, whichever way you look at it) still exist even on version 4.2 of actionBarSherlock.
So I've made my own solution, which works great (and I wish the official library could add my fix to it too), but I don't know where to get the "x" button within the editText view that is responsible for clearing the text.
Can anyone please tell me how to get the native look and feel and put it correctly in the code?
Here's a screenshot of what i'm talking about:
For those wish to use this feature, here is my code snippet :
#Override
public boolean onCreateOptionsMenu(final com.actionbarsherlock.view.Menu menu)
{
getSupportMenuInflater().inflate(R.menu.activity_main,menu);
_searchMenuItem=menu.findItem(R.id.menu_item_action_search);
View searchView=SearchViewCompat.newSearchView(this);
if(searchView!=null)
SearchViewCompat.setOnQueryTextListener(searchView,new OnQueryTextListenerCompat()
{
#Override
public boolean onQueryTextChange(final String newText)
{
_listAdapter.getFilter().filter(newText);
return true;
}
#Override
public boolean onQueryTextSubmit(final String query)
{
return super.onQueryTextSubmit(query);
}
});
else
{
searchView=new EditText(this);
searchView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
((EditText)searchView).setHint(R.string.search);
((EditText)searchView).addTextChangedListener(new TextWatcher()
{
String curretTextToFilter =null;
#Override
public void onTextChanged(final CharSequence newText,final int start,final int before,final int count)
{
if(newText==curretTextToFilter)
return;
curretTextToFilter=newText.toString();
_listAdapter.getFilter().filter(curretTextToFilter==null||curretTextToFilter.length()==0 ? null : curretTextToFilter);
}
#Override
public void beforeTextChanged(final CharSequence s,final int start,final int count,final int after)
{}
#Override
public void afterTextChanged(final Editable s)
{}
});
}
final View finalSearchView=searchView;
_searchMenuItem.setOnActionExpandListener(new OnActionExpandListener()
{
#Override
public boolean onMenuItemActionExpand(final MenuItem item)
{
if(finalSearchView instanceof EditText)
{
final InputMethodManager m=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
finalSearchView.requestFocus();
if(m!=null)
m.toggleSoftInput(0,InputMethodManager.SHOW_IMPLICIT);
}
return true;
}
#Override
public boolean onMenuItemActionCollapse(final MenuItem item)
{
if(finalSearchView instanceof EditText)
((EditText)finalSearchView).setText(null);
else _listAdapter.getFilter().filter(null);
return true;
}
});
_searchMenuItem.setActionView(searchView);
//
return true;
}
#Override
public boolean onKeyUp(final int keyCode,final KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_SEARCH)
{
_searchMenuItem.expandActionView();
return true;
}
return super.onKeyUp(keyCode,event);
}
Except for the "X" button, I think the rest can be done using a library with a theme, called HoloEverywhere.

android paste event

Is there a way to catch the paste event in my application? I must do something when I click long on an editText and select Paste from context menu. Thanks
Create menu.xml with position 'paste'
Register contextMenu to your EditText
EditText et=(EditText)findViewById(R.id.et);
registerForContextMenu(et);
Create contextMenu
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
menu.setHeaderTitle("title");
}
Create method menu onClick
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
switch (item.getItemId()) {
case R.id.paste:
break;
}
return true;
}
You should implement a TextWatcher listener on the control that receives the paste action.
The TextWatcher class provides methods to handle the OnChange, BeforeChange and AfterChange of any Editable. For instance:
private void pasteEventHandler() {
((EditText)findViewById(R.id.txtOutput))
.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Log.d(TAG, "Text changed, refreshing view.");
refreshView();
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
}
Below is code where you can override actionbar copy/Paste etc.
public class MainActivity extends Activity {
EditText editText;
private ClipboardManager myClipboard;
private ClipData myClip;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
editText = (EditText) findViewById(R.id.editText3);
myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
editText = (EditText) findViewById(R.id.editText3);
editText.setCustomSelectionActionModeCallback(new Callback() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.copy:
int min = 0;
int max = editText.getText().length();
if (editText.isFocused()) {
final int selStart = editText.getSelectionStart();
final int selEnd = editText.getSelectionEnd();
min = Math.max(0, Math.min(selStart, selEnd));
max = Math.max(0, Math.max(selStart, selEnd));
}
// Perform your definition lookup with the selected text
final CharSequence selectedText = editText.getText()
.subSequence(min, max);
String text = selectedText.toString();
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);
Toast.makeText(getApplicationContext(), "Text Copied",
Toast.LENGTH_SHORT).show();
// Finish and close the ActionMode
mode.finish();
return true;
case android.R.id.cut:
// add your custom code to get cut functionality according
// to your requirement
return true;
case android.R.id.paste:
// add your custom code to get paste functionality according
// to your requirement
return true;
default:
break;
}
return false;
}
});
}
}
You can set Listener Class:
public interface GoEditTextListener {
void onUpdate();
}
Сreate self class for EditText:
public class GoEditText extends EditText
{
ArrayList<GoEditTextListener> listeners;
public GoEditText(Context context)
{
super(context);
listeners = new ArrayList<>();
}
public GoEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
listeners = new ArrayList<>();
}
public GoEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
listeners = new ArrayList<>();
}
public void addListener(GoEditTextListener listener) {
try {
listeners.add(listener);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
/**
* Here you can catch paste, copy and cut events
*/
#Override
public boolean onTextContextMenuItem(int id) {
boolean consumed = super.onTextContextMenuItem(id);
switch (id){
case android.R.id.cut:
onTextCut();
break;
case android.R.id.paste:
onTextPaste();
break;
case android.R.id.copy:
onTextCopy();
}
return consumed;
}
public void onTextCut(){
}
public void onTextCopy(){
}
/**
* adding listener for Paste for example
*/
public void onTextPaste(){
for (GoEditTextListener listener : listeners) {
listener.onUpdate();
}
}
}
xml:
<com.yourname.project.GoEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/editText1"/>
And in your activity:
private GoEditText editText1;
editText1 = (GoEditText) findViewById(R.id.editText1);
editText1.addListener(new GoEditTextListener() {
#Override
public void onUpdate() {
//here do what you want when text Pasted
}
});
You could use this code that I've create recently
setOnReceiveContentListener(editText, arrayOf("text/*"),
OnReceiveContentListener { view, payload ->
if(view == editText && payload.source == ContentInfoCompat.SOURCE_CLIPBOARD && payload.clip.description.hasMimeType("text/*")) {
var t=""
for (i in 0 until payload.clip.itemCount) {
t += payload.clip.getItemAt(i).text
}
//do what ever you want to do with the text and set it to editText
editText.setText(t)
return#OnReceiveContentListener null
} else
return#OnReceiveContentListener payload
}
)
return the payload as it i, if not text and coming from clipboard to avoid any unintended intercepting
Got callback of paste event in onActionItemClicked(mode:ActionMode, item:MenuItem)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
editText.customInsertionActionModeCallback = object : ActionMode.Callback {
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
Log.e("ActionMode", "::ItemClicked")
if(item?.groupId == android.R.id.paste){
Log.e("ActionMode", "::Paste::ItemClicked")
}
return true
}
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
Log.e("ActionMode", "::Created")
return true
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
Log.e("ActionMode", "::Prepared")
return true
}
override fun onDestroyActionMode(mode: ActionMode?) {
Log.e("ActionMode", "::Destroyed")
}
}
}

Categories

Resources