Disable android system volume overlay - android

Is there a way to disable/override the volume overlay/toast ? (its not actually a real toast)
I want to replace it, I already wrote a system Overlay which acts like the original, plus there are more sliders.
Now I just want to disable the original (image below) so it is not displayed.

You can try to override the following function to diable the volume buttons:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
boolean result;
switch( event.getKeyCode() ) {
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
result = true;
break;
default:
result= super.dispatchKeyEvent(event);
break;
}
return result;
}

Related

How to define up and down arrow in custom android keyboard

I have created android keyboard and I am creating arrows to move the cursor in the user input, I could do the left and right but I couldn't know how to write the code for the up and down .. here is the code
switch (arrow){
case KEY_LEFT:
CharSequence leftText = getCurrentInputConnection().getTextBeforeCursor(1000, 0);
int leftLen = leftText.length() ;
getCurrentInputConnection().setSelection(leftLen-1, leftLen-1);
break;
case KEY_RIGHT:
CharSequence rightText = getCurrentInputConnection().getTextBeforeCursor(1000, 0);
int rightLen = rightText.length() ;
getCurrentInputConnection().setSelection(rightLen+1, rightLen+1);
break;
case KEY_UP: case KEY_DOWN:
break;
}
can any one help me implementing the down and up ??
If you just want to move cursors
https://developer.android.com/reference/android/inputmethodservice/InputMethodService.html#sendDownUpKeyEvents(int)
Try use sendDownUpKeyEvents method
sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_RIGHT);
Will simply do the work.
First of all, I recommend doing something like this to check for key presses
myEditText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v,
int keyCode,
KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
moveCursorLeft();
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
moveCursorRight();
return true;
case KeyEvent.KEYCODE_DPAD_UP:
moveCursorUp();
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
moveCursorDown();
return true;
}
}
return false;
}
});
I'm going off the assumption that you're programming a text editor of some sort.
The implementation for up and down depends on what you want those buttons to do, and how you've implemented things so far. For example, you could have each line as its own EditText that is kept track of in an ArrayList. Then, each time you add a new line, you can add a new EditText to the ArrayList. And when a user presses up or down, you can just change the index of the ArrayList by 1 in the correct direction.
I imagine that would be a pretty inefficient way of doing it, but it would be easy to define what up and down do. It's difficult to say more about what you should do without seeing more of your code.

Disable option on soft-keyboard android programmatically

I want to know that if possible disable hide option on a soft-keyboard programmatically.
Hide Option
//Disable return button
#Override
public boolean dispatchKeyEvent(KeyEvent event){
if(event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_BACK:
return true;
}
}
return super.dispatchKeyEvent(event);
}
This code disable, as you can see, the return button. For your case you should replace KEYCODE_BACK with another constant. I don't know for sure if it is one for option button but you can check the list right here: https://developer.android.com/reference/android/view/KeyEvent.html

How does the home button or volume up/down button works (Android)? Does pressing this button generates a hardware interrupt?

I was wondering how does the home button or volume up/down button works in Android? Does pressing this button generates a hardware interrupt?
I would like to execute a piece of code with higher privilege (in kernel) by pressing this home/volume key. Is it possible? Any pointers?
if pressing these button generates a hardware interrupt, I think I have to modify the interrupt handler to execute the code I want to execute. Is this correct?
Thanks in advance!
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU: // Menu button
doSomething();
return true;
case KeyEvent.KEYCODE_HOME: // Home button
doSomething();
return true;
case KeyEvent.KEYCODE_VOLUME_UP: // Volume Up key
doSomething();
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN: // Volume Down key
doSomething();
return true;
}
return super.onKeyDown(keycode, e);
}
You can find more keys here.

Android: Hide Volume change bar from device?

is there a way to hide the volume change bar/notification (however you might call it..btw. how do you call it?) ?
i attached a screenshot above. this bar is shown everytime i change the volume (at least on my nexus test device). or is this a nexus special?
It is the default post-honeycomb volume slider which appears when you press volume up/down buttons.
You cannot hide it in every screen of the system, but you can hide it in your activities.
Add this to an activity where you want to hide it and change the volume manually. Or you can just do nothing in case KeyEvent.KEYCODE_VOLUME_UP / DOWN if you don't want to change the volume. Remember to return true which menans you consumed the event. If you return false out there you will get double effect when the default volume behaviour will be triggered after your code.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_VOLUME_UP:
manager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE,
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
manager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
return true;
default:
return super.onKeyDown(keyCode, event);
}
}
Where AudioManager manager is retrieved as
manager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
Kotlin version for custom handling/ignoring:
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
return when(event?.keyCode){
KeyEvent.KEYCODE_VOLUME_DOWN->{
true //Do what you want or just return here
}
KeyEvent.KEYCODE_VOLUME_UP->{
true//Do what you want or just return here
}
else->super.onKeyDown(keyCode, event)
}
}

How to redefine the "volume" button on android

I'd like to redefine the "volume" button on an android phone. For example,When I press the increase or decrease, the volume will not be change, but only to print a word.
Just override the OnKeyDown method like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
//Do whatever you want to do on Volume Up
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
//Do whatever you want to do on Volume Down
return true
}
return false;
}
PROTIP: If you want this behaviour on all your activities and not only one, just do this in MainActivity.java (or whatever you want to call it) and make every other Activity extend MainActivity.
PROTIP 2: Don't do this unless it is absolutely necessary and you notify the user it works like that. Android users usually complain about it not having a common behaviour between apps.
You have to override dispatchKeyEvent() method like this:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
//TODO
Toast.makeText(this,"Your First Word",Toast.LENGTH_SHORT).show();
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
Toast.makeText(this,"Your Second Word",Toast.LENGTH_SHORT).show();
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}

Categories

Resources