Change Android volume bar - android

When you are in an Instagram story, and you decide to raise or lower the volume, this custom bar appears
Instead, the normal volume bar looks like this
I've researched ways to do it, but haven't found any.
How do I make this effect in Android Studio?
PD: I want to make this effect only when a person is using my application NOT to change the volume bar on the Android system

Use Key Event to find control of volume up and down button like this.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
return true;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
And then create a custom seek bar and control the seek bar progress with the button control.
Hope it works.

Related

Are Android system widgets within app configurable?

When I press one of the volume hardware buttons, Android's system is shown a depicted. Naturally this also happens when I press whithin my app.
Is it possible to configure the style of these Android system stuff like volume? Or at least when I open these system stuff in my app?
EDIT: As recommended in the comments, I've overwritten onKeyDown, but the adjustStreamVolume switches between 0 and 1 only.
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
when (event?.keyCode) {
KeyEvent.KEYCODE_VOLUME_UP -> {
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE)
return true
}
KeyEvent.KEYCODE_VOLUME_DOWN -> {
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE)
return true
}
else -> {
return super.onKeyDown(keyCode, event)
}
}
}
The streamVolume does not increase well.
Unfortunately, when I press continuously Volume_up, the 'getStreamVolume()' value remains t 1
Volume dialog
For the volume, it is very easy to replace the volume dialog with a custom one, from android-hide-volume-change-bar-from-device you can replace it with something simple such as a progressbar in your activity, because Android provide an interface for it.
Notification dialog
You can also (if you are very motivated) have a custom display for the incoming notifications. You will need to have a service implementing NotificationListenerService running, it will have to intercepts the notifications, and send them to the main activity to display them the way you want.
Other dialogs
Others, such as the power menu will not be changeable for obvious reasons.
Conclusion
You cannot modify the style of the android system without root permission, because it is encoded in the system app.
Android offers you different way to change its behavior but it has its limitations.
Here is an example for the volume:
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ProgressBar
android:id="#+id/progress_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
AudioManager manager;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
progressBar = findViewById(R.id.progress_horizontal);
updateVolume();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_VOLUME_UP:
updateVolume();
manager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE,
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
updateVolume();
manager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
return true;
default:
return super.onKeyDown(keyCode, event);
}
}
private void updateVolume()
{
int currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
progressBar.setProgress((int) ((float) currentVolume/maxVolume*100));
}
}
Edit
Some people said the original volume bar is still visible.
I directly tried the code I provided on my S10 Android 10 (Samsung framework) and the default volume bar is not visible. Therefore I also tried it on my S4 running LineageOS 10, and same result. No volume bar appear.
About the adjustStreamVolume or adjustVolume they both allow you to change volume channel but the adjustStreamVolume allow you to change a specific one for example, media, call etc. In my example it is only editing the media.
At application Level yes you can do anything because every action has an event, you can create your own actions with UI of your own choice on the events of your choosing, e.g hiding the STOCK VOLUME DIALOG and showing CUSTOM DIALOG, but at OS level no you can't.
Reason
Android Framework and any other architectural Component work on the
basics of events, like in Android OS if you do something, an event is
initialized and a broadcast is sent to all the framework and it can be
read by any application having right PERMISSIONS and IDS for
those Broadcasts, by using BROADCAST RECEIVERS and SERVICES in case of Notifications for special
broadcasts.
How can you achieve your goals
I will not tell you every function and method for what you want to do
but will tell you the recipe for achieving it, with the help of
Andoird's Official DOCs and with great people at Stackoverflow
For all the events you can you this overriding method
#Override
public boolean onKey**`YourKeyEvent`**(int keyCode, KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.Your_Event_One:
//your actions (perform code on some keyEvent)
break;
case KeyEvent.Your_Event_Two:
//your actions (perform code on some keyEvent)
break;
}
}
above code is great for most of the events but for some, you will be needing Broadcast Reciever aswell
LIST OF KEY_EVENTS (Android's Docs)
List Of Key Codes (Android's Code)
EDIT
For volume change please have a look at This
post, by using this post you can create a callback on volume rocker action, and then hide the stock dialog, and show your own, by using the service on the home screen.
NOTE: I've never been exposed to hiding the stock UI components in past so I am not sure about that part.

How to stop Touch To Explore feature of TalkBack in my app

I'm working on a project for blind people, there're a lot of troubles I need to fix if the user activates TalkBalk on his phone.
I'm creating a soft keyboard for blind people, the blind will tap with single finger on the circles "Braille Cell Dots" to generate a Braille code, then he types the character/number/symbols he wants as they presented in Braille language.
My problem now is Touch To Explore feature of TalkBack, the user will not be able to make a single tap on the screen because this action now handled by TalkBack, the user must double tap on the dot and this is not good for my app!
How to generate a single tap even if Touch to Explore feature is enabled in TalkBack?
Based on this answer which has solved my problem, as the single touch event converted to hover event if touch to explore is enabled, I've added onHoverEvent to call onTouchEvent and works fine:
#Override
public boolean onHoverEvent(MotionEvent event) {
//Move AccessibilityManager object to the constructor
AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
if (am.isTouchExplorationEnabled()) {
return onTouchEvent(event);
} else {
return super.onHoverEvent(event);
}
}
And handle hover action to onTouchEvent:
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_HOVER_ENTER:
//Your Code
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_HOVER_MOVE:
//Your Code
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_HOVER_EXIT:
//Your Code
break;
}
return true;
}
I'll edit my question to be more cleaner :)
You don't. It's a terrible idea. Come up with gestures and mechanisms that fit within what TalkBack allows. If you could annotate a specific feature or mechanism of your app that is not allowed to work with talkback, I could recommend an alternative. What gesture is it that's not working?

How to disable Android gamepad default controls?

I'm trying to make an app which is controlled by a gamepad. I've gotten it to work alright, but Android has some default controls that it uses for navigation when a gamepad is plugged in, such as the B button takes you back a menu. I want to be able to use the buttons that Android has defaults for. Is there a way to disable the default Android controls? I can't find any thing about the default Android gamepad controls, let alone how to disable them.
I figured it out. For anyone who needs this in the future, here's how to do it. When you add in the onKeyDown override command, this is what it looks like.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return super.onKeyDown(keyCode, event);
}
As I understand it, that return line gives the Android system access to the button presses. However if you make it always return true, the Android system never sees the input. For example:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BUTTON_A)
{
buttonAPressed = true;
}
return true;
}
I don't know if this is the best way to do it, but that's my work around to it. Hope this helps anyone that needs it!

Android [ENTER]-key action of a multiline edittext in a dialog

I've create a dialog with a multiline edittext-field. The problem is that the [ENTER]-key of the soft keyboard is closing the keyboard instead of creating a new line. With imeOptions, it's possible to configure a lot, but not a newline-command... How can I accomplish this?
Building for a Galaxy Tab 2 with Android 4.0.3.
I found out that setting the raw inputtype of the EditText to multiline is working where the "normal" input type isn't.
final EditText remark = new EditText(MyClass.this);
remark.setRawInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
This did it for me.
You might be able to accomplish this by creating a new TextWatcher. Register this textwatcher to your EditText and implement a breakline when receiveing the return key.
EDIT:
To handle an individual key press, implement onKeyDown() or onKeyUp() as appropriate. Usually, you should use onKeyUp() if you want to be sure that you receive only one event. If the user presses and holds the button, then onKeyDown() is called multiple times.
For example:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
/* This is a sample for handling the Enter button */
return true
default:
return super.onKeyUp(keyCode, event);
}
}
Source:
Android :
http://developer.android.com/training/keyboard-input/commands.html
A list of the KeyEvents:
http://developer.android.com/reference/android/view/KeyEvent.html

Disable the Search Button (Quick Search Box) in Android

I'm developing an application right now and I need to disable the quick-search-box as somehow it dismisses my dialog boxes that request info to keep people who are not supposed to be using my application out [its in development, and its on the market - it makes it much easier to keep people up to date]. Listeners for all types of dismissing dialogs are never triggered - and I don't know why. I've looked everywhere and I get no result on how to disable this. (2.1 and up).
How to disable QSB..? was a good start, but it doesn't work. I don't know why google insists I use this... I have absolutely no need for this in my application.
How can I go about fixing this... or do I have to try another sort of DRM?
You can block the search on your activity or dialog by implementing this and returning false:
public boolean onSearchRequested() {
return false;
}
UPDATE:
Code works on dialogs, too
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_SEARCH && event.getRepeatCount() == 0)
{
return true;
}
else
{
return false;
}
}
Returning true means that we are handling the Search Event.

Categories

Resources