Hello guys, there is a good news for all. Now I found the solution to override the home button in Android application.
This is layout file main.xml
This answer is taken from the stackoverflow question http://stackoverflow.com/questions/10025660/override-home-and-back-button-is-case-a-boolean-is-true/10025904#10025904
Hope this will help you in your application. Thanks.
public class DisableHardButton extends Activity {
TextView mTextView;
ToggleButton mToggleButton;
boolean isLock=false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView=(TextView) findViewById(R.id.tvInfo);
mToggleButton=(ToggleButton) findViewById(R.id.btnLock);
mToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isLock=isChecked;
onAttachedToWindow();
}
});
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if ( (event.getKeyCode() == KeyEvent.KEYCODE_HOME) && isLock) {
mTextView.setText("KEYCODE_HOME");
return true;
} else {
return super.dispatchKeyEvent(event);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if( (keyCode==KeyEvent.KEYCODE_BACK) && isLock) {
mTextView.setText("KEYCODE_BACK");
return true;
}else {
return super.onKeyDown(keyCode, event);
}
}
@Override
public void onAttachedToWindow() {
System.out.println("Onactivity attached :"+isLock);
if(isLock) {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}else {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION);
super.onAttachedToWindow();
}
}
}
This is layout file main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hi! This is the testing of override home button" />
<ToggleButton
android:id="@+id/btnLock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="UnLocked"
android:textOn="Locked" />
</LinearLayout>
This answer is taken from the stackoverflow question http://stackoverflow.com/questions/10025660/override-home-and-back-button-is-case-a-boolean-is-true/10025904#10025904
Hope this will help you in your application. Thanks.
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteit doesn't work on ics
ReplyDeleteAny solution to run in ics?
ReplyDeletejava.lang.IllegalArgumentException: Window type can not be changed after the window is added.
ReplyDeleteNot working............:(
ReplyDelete