Seekbar in android studio not changing volume - java

So I have been trying to add 4 seekBars into a basic app in order to get to grips with android studio. However, the seekbars do not seem to change the volume. Can anyone explain why this might be? Thanks
package com.example.example.examples;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class seekBarActivity extends MainActivity {
public AudioManager audioManager = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Suggests an audio stream whose volume should be changed by the hardware volume control
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
this.setVolumeControlStream(AudioManager.STREAM_RING);
this.setVolumeControlStream(AudioManager.STREAM_ALARM);
this.setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);
setContentView(R.layout.activity_main);
initControls();
}
private void initControls() {
//Return the handle to a system-level service - 'AUDIO'.
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//Find the seekbar 1
SeekBar mediaVlmSeekBar = (SeekBar) findViewById(R.id.seekBar1);
//Set the max range(Volume in this case) of seekbar
//for Media player volume
mediaVlmSeekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
//Set the progress with current Media Volume
mediaVlmSeekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
//Find the seekbar 2
SeekBar ringerVlmSeekBar = (SeekBar) findViewById(R.id.seekBar2);
//Set the max range(Volume in this case) of seekbar for Phone ringer volume
ringerVlmSeekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_RING));
//Set the progress with current Ringer Volume
ringerVlmSeekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_RING));
//Find the seekbar 3
SeekBar alarmVlmSeekBar = (SeekBar) findViewById(R.id.seekBar3);
//Set the max range(Volume in this case) of seekbar for Alarm volume
alarmVlmSeekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM));
//Set the progress with current Alarm Volume
alarmVlmSeekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_ALARM));
//Find the seekbar 4
SeekBar notifyVlmSeekBar = (SeekBar) findViewById(R.id.seekBar4);
//Set the max range(Volume in this case) of seekbar for Notification volume
notifyVlmSeekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION));
//Set the progress with current Notification Volume
notifyVlmSeekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION));
try {
//Listener to receive changes to the SeekBar1's progress level
mediaVlmSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar arg0) {
}
public void onStartTrackingTouch(SeekBar arg0) {
}
//When progress level of seekbar1 is changed
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
}
});
//Listener to receive changes to the SeekBar2's progress level
ringerVlmSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar arg0) {
}
public void onStartTrackingTouch(SeekBar arg0) {
}
//When progress level of seekbar2 is changed
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_RING, progress, 0);
}
});
//Listener to receive changes to the SeekBar3's progress level
alarmVlmSeekBar
.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar arg0) {
}
public void onStartTrackingTouch(SeekBar arg0) {
}
//When progress level of seekbar3 is changed
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, progress, 0);
}
});
//Listener to receive changes to the SeekBar4's progress level
notifyVlmSeekBar
.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar arg0) {
}
public void onStartTrackingTouch(SeekBar arg0) {
}
//When progress level of seekbar4 is changed
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, progress,0);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}

Related

My seekbar doesn't work in a BottomSheetDialog

I created a BottomSheetDialog with a checkbox and a SeekBar. I want that if the checkbox is checked, the seekbar can be used. My code works but only if i use setContentView(MyLayout), but that makes the layout appair on screen and i want that it works on the dialog. How can i make it work without that? My code here:
Ps: I am sorry for my english, but it isn't my first language.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
toolbar.setOverflowIcon(getDrawable(R.drawable.ic_baseline_more_vert_24));
ImageButton menu_filtri = findViewById(R.id.button_filtri);
menu_filtri.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(MainActivity.this,R.style.BottomSheetDialogTheme);
View BottomSheetView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.layout_bottom_sheet,(LinearLayout)findViewById(R.id.bottom_sheet_container));
bottomSheetDialog.setContentView(BottomSheetView);
bottomSheetDialog.show();
setContentView(R.layout.layout_bottom_sheet);
range_distanza = (CheckBox) findViewById(R.id.next_checkbox);
seekbar = (SeekBar) findViewById(R.id.range_seekbar);
set_seekbar();
}
});
}
public void set_seekbar() {
range_distanza.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean ischecked) {
seekbar.setEnabled(ischecked);
}
});
}
In Kotlin: Use the below code for return value after you seek the bar.
seekBar.setOnSeekBarChangeListener(new AppCompatSeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// Your logic
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});

Updating int value from Seekbar Java

I've this Seekbar and an int defined outside the seekbar. The value of the int is the seekbar progress. All is fine, except that I want the value of the int to change as the progress of the seekbar changes. Right now, it gets the value from the seekbar only when the program is launched.
int tableNumber = seekBar.getProgress();
SeekBar seekBar = findViewById(R.id.seekBar);
seekBar.setMin(1);
seekBar.setMax(20);
seekBar.setProgress(10);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Log.i("Number", Integer.toString(i));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
You have to update the tableNumber variable every time the seekbar progress changes.
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Log.i("Number", Integer.toString(i));
tableNumber = i; // add this line
}
declare tableNumber outside method and set its value in onProgressChanged
int tableNumber = 0; // declare above method
public void someInitiatingMethodMaybeOnCreate(){
SeekBar seekBar = findViewById(R.id.seekBar);
tableNumber = seekBar.getProgress(); // initial set
seekBar.setMin(1);
seekBar.setMax(20);
seekBar.setProgress(10);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Log.i("Number", Integer.toString(i));
tableNumber = i; // set current progress
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}

Multiple seekbars - set listeneners more efficiently?

I have 12 seekbars in an activity. Each seekbar needs a listener. At the moment I am manually setting a listener for each seekbar, this doesnt seem particularly efficient.
Can anyone recommend a more efficient practice to set the seekbars???
Try something like this:
public class MainActivity extends AppCompatActivity {
SeekBar seekBar1;
SeekBar seekBar2;
SeekBar.OnSeekBarChangeListener mlistener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar1 = findViewById(R.id.seekBar_1);
seekBar2 = findViewById(R.id.seekBar_2);
mlistener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
switch (seekBar.getId()) {
case R.id.seekBar_1:
//do something
break;
case R.id.seekBar_2:
//do something else
break;
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
seekBar1.setOnSeekBarChangeListener(mlistener);
seekBar2.setOnSeekBarChangeListener(mlistener);
}
If your SeekBars all require different interaction logic then obviously you will need to provide unique code to each of the objects. However, you can re-use listeners if each SeekBar is doing the same thing. For example:
public class SeekBarTest extends Activity{
private Context myContext;
public SeekBarTest(Context c){
myContext = c;
}
private void initialize(){
SeekBar seekbar1 = new SeekBar(myContext);
SeekBar seekbar2 = new SeekBar(myContext);
SeekBar seekbar3 = new SeekBar(myContext);
SeekBar.OnSeekBarChangeListener myListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
//logic
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
//logic
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
//logic
}
};
seekbar1.setOnSeekBarChangeListener(myListener);
seekbar2.setOnSeekBarChangeListener(myListener);
seekbar3.setOnSeekBarChangeListener(myListener);
}
}
Instead of creating switch in onProgressChanged method you can send variables to SeekBar initiating method.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options);
SeekBar volBar = initiateSeekBar(R.id.volume,R.id.volumeValue,0,1,"%");
SeekBar playSpeedBar = initiateSeekBar(R.id.autoPlay,R.id.autoPlayValue,100, 100,"ms");
}
private SeekBar initiateSeekBar(int barId, int textId, final int min, final int step, final String unit) {
SeekBar bar = findViewById(barId);
final TextView text = findViewById(textId);
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int value = min + progress * step;
text.setText(value + " " + unit);
autoPlayDelayTime = value;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return bar;
}
You can just make a reference equality, like this:
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
seekBar?. let {
if (this.seekBar1 === it) {
...
}
if (this.seekBar2 === it) {
...
}}
...
}
and call outside:
seekBar1.setOnSeekBarChangeListener(this)
seekBar2.setOnSeekBarChangeListener(this)

Android Java Implement KEYCODE_VOLUME_UP and DOWN to my code

I wonder how to implement KEYCODE_VOLUME_UP and DOWN to my code. So every time when I press the volume up or down key will simultaneously reflect on my Notification Volume Control app.
The method I want to implement is as follows which will make my own app volume up and down when the physical volume up and down button is pressed:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return true;
default:
return false;
}
}
The code I want to implement to is:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
run();
}
public void run()
{
mgr=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
final LayoutInflater inflater =
(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
final View ViewLayout = inflater.inflate(R.layout.activity_main,
(ViewGroup)findViewById(R.id.activity_main));
alarm=(SeekBar)ViewLayout.findViewById(R.id.alarm);
music=(SeekBar)ViewLayout.findViewById(R.id.music);
ring=(SeekBar)ViewLayout.findViewById(R.id.ring);
system=(SeekBar)ViewLayout.findViewById(R.id.system);
voice=(SeekBar)ViewLayout.findViewById(R.id.voice);
alarmVol=(TextView)ViewLayout.findViewById(R.id.alarmVol);
musicVol=(TextView)ViewLayout.findViewById(R.id.musicVol);
ringVol=(TextView)ViewLayout.findViewById(R.id.ringVol);
systemVol=(TextView)ViewLayout.findViewById(R.id.systemVol);
voiceVol=(TextView)ViewLayout.findViewById(R.id.voiceVol);
alertDialogBuilder = new AlertDialog.Builder(this);
//alertDialogBuilder.setIcon(R.mipmap.ic_launcher);
//alertDialogBuilder.setTitle("Volume Control");
alertDialogBuilder.setView(ViewLayout);
initBar(alarm, AudioManager.STREAM_ALARM, alarmVol);
initBar(music, AudioManager.STREAM_MUSIC, musicVol);
initBar(ring, AudioManager.STREAM_RING, ringVol);
initBar(system, AudioManager.STREAM_SYSTEM, systemVol);
initBar(voice, AudioManager.STREAM_VOICE_CALL, voiceVol);
alertDialogBuilder.create();
alertDialogBuilder.show();
}
private void initBar(SeekBar bar, final int stream, final TextView textView)
{
final float maxVolume=mgr.getStreamMaxVolume(stream);
float curVol = mgr.getStreamVolume((stream));
bar.setMax((int)maxVolume);
bar.setProgress((int)curVol);
bar.setKeyProgressIncrement(1);
textView.setText(String.valueOf((int)(curVol/maxVolume*100)));
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar bar, int progress,
boolean fromUser) {
mgr.setStreamVolume(stream, progress,
AudioManager.FLAG_PLAY_SOUND);
textView.setText(String.valueOf((int)
((progress/maxVolume)*100)));
}
public void onStartTrackingTouch(SeekBar bar) {
}
public void onStopTrackingTouch(SeekBar bar) {
}
});
}
}

SetText Value of Current System Volume

i have this code and i would like to set text in the various TextView that says the current volume level. Example: System Volume: 35%. Obviously, this number has to change when I "move" the SeekBar. This is the code.
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class Activity_JellyBean extends DashBoardActivity
{
/** Called when the activity is first created. */
private SeekBar volumeSeekbar = null;
private SeekBar alarmSeekbar = null;
private SeekBar systemSeekbar = null;
private SeekBar notificationSeekbar = null;
private SeekBar ringSeekbar = null;
private AudioManager audioManager = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_jellybean);
initControls();
}
private void initControls()
{
try
{
volumeSeekbar = (SeekBar)findViewById(R.id.seekBar1);
alarmSeekbar = (SeekBar)findViewById(R.id.seekBar2);
systemSeekbar = (SeekBar)findViewById(R.id.seekBar3);
notificationSeekbar = (SeekBar)findViewById(R.id.seekBar4);
ringSeekbar = (SeekBar)findViewById(R.id.seekBar5);
final TextView media = (TextView)findViewById(R.id.textView1);
TextView ring = (TextView)findViewById(R.id.textView2);
TextView alarm = (TextView)findViewById(R.id.textView3);
TextView noti = (TextView)findViewById(R.id.textView4);
TextView ringe = (TextView)findViewById(R.id.textView5);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
alarmSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_ALARM));
alarmSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_ALARM));
systemSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_SYSTEM));
systemSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_SYSTEM));
notificationSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION));
notificationSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_NOTIFICATION));
ringSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_RING));
ringSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_RING));
volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
#Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});
alarmSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
#Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_ALARM,
progress, 0);
}
});
systemSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
#Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM,
progress, 0);
}
});
notificationSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
#Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,
progress, 0);
}
});
ringSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
#Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_RING,
progress, 0);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Thanks in advance.

Categories

Resources