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);
}
Related
I have activity with custom actionbar(Basically custom layout xml), therefore I have created a custom menu button as well in layout file and showing a popup,
I am usingthis callback to showing same popup on tap of hardware option menu, but give a weird behaviour e.g this will open custom menu whenvery onresume is run.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//return super.onCreateOptionsMenu(menu);
showHideMenu();
return false;
}
can you please suggest where I can placeshowHideMenu(); will which work like custom option menu
//showHideMenu() code
private void showHideMenu() {
if (mPopUpMenu.isShowing())
dismissPopUpMenu();
else
showMenu();
}
private void dismissPopUpMenu() {
// dismiss menu
if (mPopUpMenu != null)
mPopUpMenu.dismiss();
}
You should look for an up keyevent:
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
showHideMenu();
return true;
}
return super.onKeyUp(keyCode, event);
}
Include this for PopUp:
mPopUpMenu.getContentView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// ... payload action here. e.g. popupMenu.dismiss();
return true;
}
return false;
}
});
Reference:
Detecting physical Menu key press in Android
you can use PopupMenu for showing custom menu.
for this you have to find a view to attach menu.and then you can wrote
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu,popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
for more information you can see http://www.androidhive.info/2011/09/how-to-create-android-menus/
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
I have search view in my fragment. when I click on it , keyboard is open and I can type text. I want when I click on search button in keyboard , my query send to my server and get result but I don't know how get search event. any solution?
You have to extend OnQueryTextListener, attach the listener and implement onQueryTextSubmit.
Example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
searchView = (SearchView) menu.findItem(R.id.mActionSearch).getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
//Do something here
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
Pozzo Apps Answer is right
but for api below 11 and compat library you can use something like this :
MenuItem search_menu=(MenuItem)menu.findItem(R.id.action_search);
SearchView searchView =(SearchView)MenuItemCompat.getActionView(search_menu);
You can also apply setOnKeyListener on search view like as below:
searchview.setOnKeyListener(new View.OnKeyListener(
{
Public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
switch(keyCode)
{
Case KeyEvent.KECODE_ENTER:
// Apply action which you want on search key press on keypad
return true;
default:
break;
}
} return false;
}
});
You have to add new OnQueryTextListener, and implement onQueryTextSubmit. This also works in a fragment.
Example:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main_search, menu);
SearchView sv = (SearchView) menu.findItem(R.id.action_search).getActionView();
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
//Do something here
Toast.makeText(getActivity(), "Search: " + query, Toast.LENGTH_SHORT ).show();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
super.onCreateOptionsMenu(menu,inflater);
}
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 have added a button in the action bar, it works fine but if I press the physical menu button, the menu appears action_settings. Instead I want to use only the menu in the ActionBar and not that of the physical menu button.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
getActivity().getMenuInflater().inflate(R.menu.menu_scegli, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_c:
Intent intent = null;
intent = new Intent(getActivity(), Inserisci_.class);
startActivity(intent);
return true;
case R.id.action_settings:
return false;
default:
return super.onOptionsItemSelected(item);
}
}
You can try overriding onKeyDown method in your activity and returning true if the menu key was pressed - this will effectively prevent android from internally handling the menu key:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_MENU) {
return true;
}
else {
return false;
}
}