I'm trying to perform long click on searchView menu item in xml menu in android java.
I want to find a way in java code to perform long click on this menu item(the icon not the searchbar) the menu item use app:actionViewClass="android.support.v7.widget.SearchView"
I tried many solutions but none of those working.
Can you lend me an hand please?
EDIT:
Here the code that i try
searchItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
final int itemId = searchItem.getItemId();
View actionView = searchItem.getActionView();
/** define view on long click listener */
final View.OnLongClickListener toolbarItemLongClicked = new View.OnLongClickListener() {
int counter;
#Override
public boolean onLongClick(View view) {
Log.d("longclick", "Long click performed");
return true;
}
};
Related
I have a Button and when I click it it plays a Sound. How to use longpress to turn sound ( on and off ), so basically first tap should play a sound, second tap should stop it.
MainActivity
You can use onLongClickListener:
Button button;
button = findViewById(R.id.<your_button_id>);
button.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
//your code goes here
return false;
}
});
You have to add onLongClickListener to your button and implement the method onLongClick in your main activity.
for example:
public class MainActivity implements View.OnLongClickListener
after you implement the onLongClickListener you override the function onLongCLick
#Override
public boolean onLongClick(View view) {
return false;
}
And finally you need to set onLongClickListener to your button
btn.setOnLongClickListener(this);
In order to make the sound on and of just hold a global boolean variable which is called
private boolean isPlaying;
When it is long pressed once you set it to true, and when it is called again set it to false.
and stop your sound.
In my ActionBar, I have a MenuItem that has attribute showAsAction="always" as seen in the image below. Based on the connection a user has to our servers, I will be changing the text as well as color of the item.
Currently, I am able to change the text of the item very easily in onPrepareOptionsMenu(...):
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.action_connection);
if(mIsConnected) {
item.setTitle(R.string.action_connected);
} else {
item.setTitle(R.string.action_not_connected);
}
return super.onPrepareOptionsMenu(menu);
}
This works great and if possible, I would like to change the color of the text here as well. I've seen many posts about how to change the text of ALL the overflow items or the title to the ActionBar itself but nothing about changing an individual action item PROGRAMMATICALLY. The current color is set in xml, I want to change it dynamically.
Well, each MenuItem View is actually a subclass of TextView, so this will make changing the text color easier.
A simple method you can use to locate a MenuItem View is View.findViewsWithText.
A basic implementation, considering you just have that one MenuItem you're interested in changing, might look something like this:
private final ArrayList<View> mMenuItems = Lists.newArrayList();
private boolean mIsConnected;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Add a your MenuItem
menu.add("Connected").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
// Adjust the text color based on the connection
final TextView connected = !mMenuItems.isEmpty() ? (TextView) mMenuItems.get(0) : null;
if (connected != null) {
connected.setTextColor(mIsConnected ? Color.GREEN : Color.RED);
} else {
// Find the "Connected" MenuItem View
final View decor = getWindow().getDecorView();
decor.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
mIsConnected = true;
// Remove the previously installed OnGlobalLayoutListener
decor.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// Traverse the decor hierarchy to locate the MenuItem
decor.findViewsWithText(mMenuItems, "Connected",
View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
// Invalidate the options menu to display the new text color
invalidateOptionsMenu();
}
});
}
return true;
}
Results
Right now I am working on an Android application that requires allowing the user to cut copy and paste onto an editText fields. But when I copy a formatted string from other places (i.e. a string that is underlined) and paste it on to the editText field, it shows it as a formatted version. How do I remove this?
I have tried to add a textwatcher by adding addTextChangedListener, and in the after text change I just do edittext.setText(s.toString()+"") but this creates an infinite loop. :(
Please help! Thanks in advance.
Edit----
I have made some progress by setting setCustomSelectionActionModeCallback
editDestination_.setCustomSelectionActionModeCallback(new Callback() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {return true;}
#Override
public void onDestroyActionMode(ActionMode mode) {}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.removeItem(android.R.id.paste);
menu.removeItem(android.R.id.selectAll);
menu.add(0, CUSTOM_PASTE, 0, "Paste").setIcon(R.drawable.paste_ic);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case CUSTOM_PASTE:
edittext.setText(readFromClipboard(aContext_));
// Finish and close the ActionMode
mode.finish();
return true;
default:
break;
}
return false;
}
});
This is working pretty well until I realize there are two types of cut/copy/and paste on my phone. One is when the edit text is empty and I long click on the field. This bring up a popup menu. The other one is when there is text in the field, and when I long click, this bring up a cut/copy/and paste bar below my action bar. My code from above is only affecting the bar-below-action bar one. :(
PROBLEM :
The thing you are doing is creating an infinite loop because everytime you call setText(), again afterTextChanged() gets called (because you are changing the text inside the EditText.
SOLUTION :
Suppose the EditText is editTextToClearStyle
EditText editTextToClearStyle = (EditText)findViewById(R.id.youredittextname);
editTextToClearStyle.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
editTextToClearStyle.setTextAppearance(getApplicationContext(),R.style.normalText);
}
I have solved it.
I set a onLongClickListener to catch it before the popup-cut/copy/paste menu shows up.
CharSequence actions[] = new CharSequence[] {"paste"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setItems(actions, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
edittext.setText(readFromClipboard(aContext_));
}
});
builder.show();
Then if there is a string already present in the EditText, I return false to allow normal android system below-action-bar-cut/copy/paste function to work.
I am working on an app, in which I am want to use a pop-up menu to control some actions and settings. However, when I launch the app in my emulator, the items appear blank, although when I click on them, the action is fired and it works. Here is a screen of the emulator:
I am following the guide, so my code does not differ much from the dev guides, but here is my code:
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long ID)
{
// TODO Auto-generated method stub
courseId = ID;
fields = sqldbase.query(DBHelper.courseTable, new String[] {
DBHelper.courseID, DBHelper.courseName,
DBHelper.courseProf, DBHelper.averageGrade },
DBHelper.courseID + " = " + ID, null, null, null, null);
PopupMenu popup = new PopupMenu(getBaseContext(), view);
popup.getMenuInflater().inflate(R.menu.courses_popup_menu,
popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
Log.i(TAG,"OnMenuItemClick Fired"); return false;
}
});
return false;
}
});
Could this be related to a problem with themes? I have been trying to use a Holo.light.NoTitleBar theme, so in my xml in eclipse, the word Passbook does not show, and everything is white, instead of black. However, after running in the emulator, the theme is ignored, and this shows up.
Thanks.
fixed it. apparently, popup menus use the attribute android:title instead of android:text.
I found a workaround to actually enable the ActionBar home button on the nested PreferenceScreen... however it doesn't call OnOptionsItemSelected in my PreferenceActivity. Anyone know a way to actually use the home button on a nested PreferenceScreen?
Modification of post 35 here:
http://code.google.com/p/android/issues/detail?id=4611
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)
{
super.onPreferenceTreeClick(preferenceScreen, preference);
if (preference!=null)
if (preference instanceof PreferenceScreen)
if (((PreferenceScreen)preference).getDialog()!=null)
((PreferenceScreen)preference).getDialog().getActionBar().setHomeButtonEnabled(true);
return false;
}
I had this problem recently and this is how I solved it. Firstly to access the PreferenceScreen I use the exact same method you mentioned above.
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
super.onPreferenceTreeClick(preferenceScreen, preference);
// If the user has clicked on a preference screen, set up the action bar
if (preference instanceof PreferenceScreen) {
initializeActionBar((PreferenceScreen) preference);
}
return false;
}
From here I looked into what a PreferenceScreen is, and I was saddened to find out it is just wrapper of a Dialog. Moving forward, I then set the actionbar display options and attempt find the home button area. This unfortunately wasn't too easy to get, but with the help of the hierarchy viewer I managed to gain access by finding the home icon and then its parent views. Once we have access to the containing LinearLayout, we can attach an onClickListener where we dismiss the PreferenceScreen's dialog, which calls PreferenceScreen's onDismissListener and returns us to the previous screen.
/** Sets up the action bar for an {#link PreferenceScreen} */
public static void initializeActionBar(PreferenceScreen preferenceScreen) {
final Dialog dialog = preferenceScreen.getDialog();
if (dialog != null) {
// Inialize the action bar
dialog.getActionBar().setDisplayHomeAsUpEnabled(true);
// Apply custom home button area click listener to close the PreferenceScreen because PreferenceScreens are dialogs which swallow
// events instead of passing to the activity
// Related Issue: https://code.google.com/p/android/issues/detail?id=4611
View homeBtn = dialog.findViewById(android.R.id.home);
if (homeBtn != null) {
OnClickListener dismissDialogClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
};
// Prepare yourselves for some hacky programming
ViewParent homeBtnContainer = homeBtn.getParent();
// The home button is an ImageView inside a FrameLayout
if (homeBtnContainer instanceof FrameLayout) {
ViewGroup containerParent = (ViewGroup) homeBtnContainer.getParent();
if (containerParent instanceof LinearLayout) {
// This view also contains the title text, set the whole view as clickable
((LinearLayout) containerParent).setOnClickListener(dismissDialogClickListener);
} else {
// Just set it on the home button
((FrameLayout) homeBtnContainer).setOnClickListener(dismissDialogClickListener);
}
} else {
// The 'If all else fails' default case
homeBtn.setOnClickListener(dismissDialogClickListener);
}
}
}
}