How to disable the home button in android 4.0+ versions - android

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.

Related

Menu key is triggered twice android

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

Android webview fire custom CAB during longClick and Selection mode

I use this code to override webview Context Action Bar but during longclick the CAB appears but no text selection how can i only fire CAB when text selection mode?
public class MainActivity extends Activity {
ActionMode.Callback mCallback;
ActionMode mMode;
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview=webview = (WebView) findViewById(R.id.webView1);
webview.loadUrl("http://www.google.com");
webview.setOnLongClickListener(new OnLongClickListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressLint("NewApi")
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
if (mMode != null)
return false;
else
mMode = startActionMode(mCallback);
return true;
}
});
mCallback = new ActionMode.Callback() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
mMode = null;
}
#SuppressLint("NewApi")
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle("1 selected");
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#SuppressLint("NewApi")
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(getBaseContext(), "Selected Action1 ",
Toast.LENGTH_LONG).show();
mode.finish();
break;
}
return false;
}
};
}
}
during long press my custom CAB appear without text selection
how to make it appear only in the case of text selection?
many thanks
don't do this,it is not necessary to overwrite longClick, you should make your custom webview and do some thing that below link display:
Look At This Answer

Android back button Not working

public class Myactivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myactivity);
}
#Override
public void onDestroy() {
this.finish();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.myactivity, menu);
return true;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
super.loadUrl("file:///asset/www/index.html");
}
return super.onKeyDown(keyCode, event);
}
In Above I need to go to the index.html file when the back button is clicked.But whwn i try it i get folowning error.
11-02 12:25:00.677: E/WindowManager(24344): Activity org.apache.cordova.example.Myactivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#421fad78 that was originally added here
11-02 12:25:00.677: E/WindowManager(24344): android.view.WindowLeaked: Activity org.apache.cordova.example.Myactivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#421fad78 that was originally added here
the back button is supposed to finish the activity. Calling super.onKeyDown(keyCode, event) you are asking android to call, currentActivity.finish();.
override the onBackPressed() method. put your code in it.,it will work

android openPanel menu is empty

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);
}

Calling Custom Options menu when webview is running

I want to call my function onCreateOptionsMenu when the menu button is clicked when my webview is running. Right now when you click the Memnu button when the Webview is runing it displays some default menu not my custom menu.
How would I modify my code below to disable the default menu and display my custom menu? When the web view is not running it shows my custom menu when you click the menu button.
public class WebViewActivity extends Activity {
WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
final Activity mActivity = this;
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
// SETS DEFAULT MODE TO LANDSCAPE
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// Makes Progress bar Visible
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://Google.com");
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
mActivity.setTitle("Google.com");
mActivity.setProgress(progress * 100);
}
});
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// do your menu stuff here
return true;
}
else
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
}
You have to override the onKeyDown function, to intercept the Menu key. Otherwise the webview will consume it. Something like this:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// do your menu stuff here
return true;
}
else
return super.onKeyDown(keyCode, event);
}

Categories

Resources