If I the press menu key, it is triggered twice in all of my app's Activities
I tried to override onKeyUp and onKeyDown but no use any suggestions as to why is this happening?? Thanks in advance code goes on like this..
public class MainActivity extends ActionBarActivity {
private MainActivityDrawer mainActivityDrawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mainActivityDrawer = (MainActivityDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_main_activity_drawer_in);
mainActivityDrawer.setUp(R.id.fragment_main_activity_drawer_in, (DrawerLayout) findViewById(R.id.mainDrawer1), toolbar);
new clicklisteners().execute();
DrawerLayout drawerLayout = ((DrawerLayout) (findViewById(R.id.mainDrawer1)));
drawerLayout.setStatusBarBackground(R.color.PrimaryColorDark);
registerReceiver(err, new IntentFilter("ERROR_LOG_BUTTON_CLICKED"));
//toolbar.setOnKeyListener(this);
}
#Override
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
return false;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return false;
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_UP)
{
if(event.getKeyCode()==KeyEvent.KEYCODE_MENU)
{
ShowToast("Hello");
return true;
}
}
if(event.getKeyCode()==KeyEvent.KEYCODE_BACK)
{
if(mainActivityDrawer.isOpened())
{
mainActivityDrawer.close();
}
else
{
finish();
}
}
return false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
......
}
It seems the library 'com.android.support:appcompat-v7:22.0.0' has a problem with the menu button ,When i made that v7:22.0.0 to v7:21.0.0 the menu key works properly
Related
I tried following code but it is not getting to disable the home button above 4.0 versions in android
public class DHActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dh);
getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dh, menu);
return true;
}
#Override
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
public boolean onKeyDown(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_HOME) {
Log.i("TESTE", "BOTAO HOME");
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed() {
}
}
In the lower versions of android you can catch the home button , in later versions for security reason Google not allowing to override home button press.
I'm using SearchView while my activity is extanding Sherlock Library.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
inflater.inflate(R.menu.countriesxml, menu);
SearchView mSearchView = (SearchView) menu.findItem(R.id.searchall)
.getActionView();
mSearchView.setQueryHint("Search for smartphones");
MenuItem menuItem = menu.findItem(R.id.searchall);
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// TODO Auto-generated method stub
item.collapseActionView();
indicator.setVisibility(View.VISIBLE);
pager.setVisibility(View.VISIBLE);
lvth.setVisibility(View.GONE);
return true;
}
});
return;
}
Please note that i tried to use OnCloseListener() but it didn't work.
What's wrong?
have a look at your code at this section:
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
Replace it with below code and try again.
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
Try using OnFocusChangeListener instead
menuItem.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean queryTextFocused) {
if (!queryTextFocused) {
// Code here
}
}
});
I have a tabActivity with multiple activities in one tab.
The following code work on android 2.3 but it's not working on android 4.2
ActivityStack.java
public class ActivityStack extends ActivityGroup {
..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// // what is the current activity?
menu.add(0, 0, 0, "holder");
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// start a new
menu.clear();
// add some menu options
getLocalActivityManager().getCurrentActivity().onPrepareOptionsMenu(menu);
return super.onPrepareOptionsMenu(menu);
}
..
Activity1Tab1.java
here I have a button from where I am calling Activity2Tab1.java onClickListener
Intent acIntent = new Intent();
acIntent.setClass(getParent(),
Activity2Tab1.class);
ActivityStack activityStack = (ActivityStack) getParent();
activityStack.push("SecondActivity", acIntent);
Activity2Tab1.java
..
here I have multiple layouts...defined
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{ //add menus or inflate here
Log.d(TAG, "onPrepareOptionMenu");
if (!isMainMenuVisible) {
pushMainMenuUp();
} else {
pushMainMenuDown();
}
return true;
}
Need some help!!!
Neither in Activity1Tab1 or in Activity2Tab1 the override method onKeyUp() IS NEVER CALLED. The only called methods are from StackActivity. WHY?
After some research done and some thinking I manage to make this code work.
Instead of using onPrepareOptionMenu(menu) and onCreateOptionMenu(menu) I've override the following method in StackActivity:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.d(TAG, keyCode+"");
getLocalActivityManager().getCurrentActivity().onKeyUp(keyCode, event);
return super.onKeyUp(keyCode, event);
}
and in Activity1Tab1 and Activity2Tab1 I had the method:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.d(TAG, ""+event.getAction());
if (keyCode == KeyEvent.KEYCODE_MENU) {
Log.d(TAG, "MENU_BUTTON_PRESSED");
if (!isMainMenuVisible) {
pushMainMenuUp();
} else {
pushMainMenuDown();
}
}
return super.onKeyUp(keyCode, event);
}
I want to handle collapsing state but I can not handle onActionExpandListener. I use actionbarsherlock and here are my codes. Where do I make mistake?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
menu.getItem(0).setVisible(aramaOnOff);
menu.getItem(1).setVisible(filterOnOff);
SearchView searchView = (SearchView)menu.findItem(R.id.imgBuyutec).getActionView();
searchView.setOnQueryTextListener(this);
searchViewItem = menu.findItem(R.id.imgBuyutec);
searchViewItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
});
return true;
}
Use appcompat-v7 , http://developer.android.com/guide/topics/ui/actionbar.html
and then use the following code,
MenuItemCompat.setOnActionExpandListener(searchViewItem,new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
//whatever
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//whatever
return true;
}
});
Here is my code. The onKey() method is never called when typing into the search box. Am I doing something wrong in setting up the listener? I have a breakpoint in the onKey() method, which is how I know it doesn't fire.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.locations_map_menu, menu);
ActionBar ab = getActionBar();
ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar));
ab.setDisplayShowTitleEnabled(false);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
return true; // This code never fires
}
});
return true;
}
Add an OnQueryTextListener, see my answer here
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
// Do something
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
// Do something
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
if you want to get a key on serach view
you can use below code
TextView searchText = (TextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
return false;
}
});