I have the following code:
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
}
return false;
}
My issues is following:
I have an search icon in the ActionBar. When I tap on the search icon an edittext is opened and cancel button shown. The search is working properly. Now, when I click the cancel button I want to hide the edittext and cancel button. How can I achieve this?
I solve my problem... using
item.collapseActionView();
Thanks for reply and hope it this will help others..
Here is my code where i have used it.....
private EditText search;
private Button search_cancle;
#Override
public boolean onOptionsItemSelected(final MenuItem item) {
int id = item.getItemId();
if(id==R.id.action_search){
View v = (View) item.getActionView();
search = ( EditText ) v.findViewById(R.id.txt_search);
search.requestFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
search_cancle=(Button)v.findViewById(R.id.search_cancel);
search_cancle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
item.collapseActionView(); // Here I have set it.....
search.setText("");
}
});
return true;
}
else
{
return super.onOptionsItemSelected(item);
}
}
Related
i have a collapseActionView search in actionbar..A back arrow appears in the place of home button in actionbar when we press on search button...on focusing edittext soft keyboard appears...i have a problem when we click on the back arrow without submitting the search value the soft keyboard doesnot hide itself...how to write functionality for the backarrow which appears in the actionbar when the search view is open?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
inflater.inflate(R.menu.main_activity_bar_l, menu);
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
inflater.inflate(R.menu.main_activity_bar, menu);
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
inflater.inflate(R.menu.main_activity_bar, menu);
} else {
inflater.inflate(R.menu.main_activity_bar, menu);
}
View v = (View) menu.findItem(R.id.my_action).getActionView();
/** Get the edit text from the action view */
txtSearch = (EditText) v.findViewById(R.id.myActionEditText);
/** Setting an action listener */
ImageView Search = (ImageView) v.findViewById(R.id.search);
Search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
search_value = txtSearch.getText().toString();
if (txtSearch.getText().toString().equals("")) {
Toast.makeText(getBaseContext(),
"Please enter search values", Toast.LENGTH_SHORT)
.show();
} else {
Intent intent = new Intent(MainActivity.this,
Display.class);
intent.putExtra("Action_search", txtSearch.getText()
.toString());
startActivity(intent);
txtSearch.setText("");
}
}
});
return super.onCreateOptionsMenu(menu);
}
i wrote functionality for home button but it doesnot work
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
//
//
//
case android.R.id.home:
Toast.makeText(MainActivity.this, "home", Toast.LENGTH_LONG).show();
// count the active fragment
// if(getSupportFragmentManager().getStackBackEntryCount() > 0) {
// // hide soft method as above
// InputMethodManager mImm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
// mImm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
// // do the pop backstack
// getSupportFragmentManager().popBackStack();
// } else {
// // some stuff like finish the activity
}
return super.onOptionsItemSelected(item);
}
Just write this code in onPause:
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
I am using a search field in the actionbar by reffering it to an xml in the layout folder called search_layout.xml.In the 2.3.3 version of android, there is an issue like the search icon is not shown in the keypad for the first time after I enter the text. Instead I have to click to the "enter" key, then on the textfield and then only the "search" icon appears. This does not happen in higher versions like 4.0.4 and 4.4.2 of android.
Here is the search_layout.xml:
And this xml is reffererd to in res>>menu>>menu.xml
<item
android:id="#+id/action_notification"
android:actionLayout="#layout/search_layout"
android:icon="#drawable/ic_searchicon"
android:orderInCategory="0"
android:showAsAction="always|collapseActionView"
android:title="Search"/>
This is the code snippet I am using for this in my activity called Activity_HomeScreen class.
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem itemListMap,itemRefresh;
editsearch = (EditText) menu.findItem(R.id.action_notification).getActionView();
editsearch.addTextChangedListener(textWatcher);
editsearch.setOnEditorActionListener(new OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_SEARCH)
{
editsearch.clearFocus();
InputMethodManager imm = (InputMethodManager)Activity_HomeScreen.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editsearch.getWindowToken(), 0);
captureViepagerFragments(mVpContainer.getCurrentItem());
}
return false;
}
});
MenuItem menuSearch = menu.findItem(R.id.action_notification);
menuSearch.setOnActionExpandListener(new OnActionExpandListener()
{
// Menu Action Collapse
#Override
public boolean onMenuItemActionCollapse(MenuItem item)
{
// Empty EditText to remove text filtering
editsearch.setText("");
editsearch.clearFocus();
InputMethodManager imm = (InputMethodManager)Activity_HomeScreen.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editsearch.getWindowToken(), 0);
captureViepagerFragments(mVpContainer.getCurrentItem());
return true;
}
// Menu Action Expand
#Override
public boolean onMenuItemActionExpand(MenuItem item)
{
// Focus on EditText
editsearch.requestFocus();
// Force the keyboard to show on EditText focus
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
Solved this issue by doing like this:
public boolean onMenuItemActionExpand(MenuItem item)
{
editsearch.clearFocus();
editsearch.post(new Runnable() {
#Override
public void run() {
editsearch.requestFocus();
final InputMethodManager imm = (InputMethodManager) Activity_Cat_Products.this
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editsearch, InputMethodManager.SHOW_IMPLICIT);
}
});
return true;
}
I use the GridView and set to MultiChoiceModeListener.
When I select the item from GridView , it will call the onCreateActionMode and the onActionItemClicked like the following code.
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
View v = LayoutInflater.from(getActivity()).inflate(R.layout.actionbar_layout, null);
mActionText = (TextView) v.findViewById(R.id.action_text);
mActionText.setText(formatString(fileListView.getCheckedItemCount()));
mode.setCustomView(v);
getActivity().getMenuInflater().inflate(R.menu.action_menu, menu);
return true;
}
And the menu will show how many item I have select like the following picture.
When I click the button , it will transmit the item which I have select to a new Fragment.
The following code is for button
download_button = (ImageButton) view.findViewById(R.id.download_button) ;
download_button.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = DownloadPage.newInstance(null, null, null, checkedItems) ;
MainActivity.addFragment(FileBrowserFragment.this, fragment);
menu.finish(); //can not call menu.finish();
}
But when it turn to the new fragment , the menu doesn't disappeared.
How to close the menu when I click the button and turn to the new fragment???
Think you are looking for finish(); on ActionMode see this example:
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
deleteSelectedItems();
mode.finish();
return true;
}
}
If you want to finish by button click, register listener to your button and then put the finish() method inside that.
EDIT
Try this:
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
download_button.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = DownloadPage.newInstance(null, null, null, checkedItems) ;
MainActivity.addFragment(FileBrowserFragment.this, fragment);
deleteSelectedItems();
mode.finish();
}
});
return true;
}
I am working on Sherlock ActionBar and using an Option button at right corner, near search icon. I am successful to generate the onClick event for SEARCH button but I am getting problems in onClick event of the option Button.
Using the below code to add the buttons :
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
menu.add(0, 1, 1, "Search").setIcon(R.drawable.search_icon).setActionView(R.layout.action_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
menu.add(0, 2, 2, "Button").setIcon(R.drawable.menu_overflow_icon).setActionView(R.layout.action_button).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return super.onCreateOptionsMenu(menu);
}
And for click events :
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
switch (item.getItemId()) {
case 1:
search = (EditText) item.getActionView();
search.addTextChangedListener(filterTextWatcher);
search.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
break;
case 2:
Toast.makeText(ActivityScreen.this, "hit it", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
I have shown my requirement in the image attached below.
Replace your break; statements in the Switch of onOptionsItemSelected to return true;
Addition to the answer
final static int BUTTON_ID = 2;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add(Menu.NONE, BUTTON_ID, Menu.NONE, R.string.action_option_name);
item.setIcon(R.drawable.action_option);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case BUTTON_ID:
Toast.makeText(this, "clicked on 2", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
Note: I haven't added MenuItem.setActionView(R.layout.action_search). I tried to use the solution at https://stackoverflow.com/a/11292843/1567588. But its not working. And i have set GroupId and OrderId to Menu.None
My requirement is to open a menu when edittext is clicked with options "bold", "italic", "Underline", "Fonts" and "Colors".
Please can someone help me on this..
Here's how I ended up solving this:
EditText menuEdit = (EditText) activity.findViewById(R.id.menuImageView);
menuEdit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
activity.openOptionsMenu(); //This is the key method!
}
});
public boolean onCreateOptionsMenu(Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.demographics:
return true;
case R.id.settings:
Log.v("v", "settings clicked");
return true;
default:
return false;
}
}