SetText Value of Current System Volume - java

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.

Related

Android Java Sum of values from Two SeekBar's using setOnSeekBarChangeListener or Button

being new in Android I'm trying to just simply add (sum) adjusted values from two SeekBars using setOnSeekBarChangeListener or using Button.
The project below is work fine but the sum still is 0. I can't find a solution. Thanks for your help. Usually, I'm using Spring Boot and Vaadin and it is so simple :)
private int progress1 = 0;
private int progress2 = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textViewMain;
Button button;
final TextView textViewMain_ = findViewById(R.id.textViewMain);
Button buttonSum = findViewById(R.id.button);
SeekBar seekBar = findViewById(R.id.seekBar);
final TextView textViewSeekBar = findViewById(R.id.textView2);
SeekBar seekBar2 = findViewById(R.id.seekBar2);
final TextView textViewSeekBar2 = findViewById(R.id.textView3);
seekBar.setMax(600);
seekBar.setProgress(5);
seekBar2.setMax(900);
seekBar2.setProgress(5);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public int progress1 = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progress1, boolean fromUser) {
this.progress1 = progress1;
// getUpdateTotal();
//textViewSeekBar.setText(Integer.parseInt(String.valueOf(seekBar)));//todo not work
textViewSeekBar.setText(Integer.toString(progress1));
Log.i("Seeker1: ", Integer.toString(progress1));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public int progress2 = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progress2, boolean fromUser) {
this.progress2 = progress2;
//getUpdateTotal();
textViewSeekBar2.setText(Integer.toString(progress2));
Log.i("Seeker2: ", Integer.toString(progress2));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
buttonSum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getSummaryTwoSeekBarValue(progress1, progress2);
Log.i("", getSummaryTwoSeekBarValue(progress1, progress2));
}
});
}
public String getSummaryTwoSeekBarValue(int progress1, int progress2) {
final TextView textViewMain_ = findViewById(R.id.textViewMain);
int total = progress1 + progress2;
String totalString = String.valueOf(total);
Log.i("", totalString);
textViewMain_.setText(String.valueOf(totalString));
return totalString;
}
}```
[enter image description here][1]
[1]: https://i.stack.imgur.com/HDx12.png
You are defining progress1 and progress2 in both the main class and in the listener, so the class variable will keep being 0, you have to use (your class name).this.progress1 = progress1; and same in the second seek bar to modify the main class one instead of the listener one
private int progress1;
private int progress2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textViewMain;
final TextView textViewMain_ = findViewById(R.id.textViewMain);
SeekBar seekBar = findViewById(R.id.seekBar);
final TextView textViewSeekBar = findViewById(R.id.textView2);
SeekBar seekBar2 = findViewById(R.id.seekBar2);
final TextView textViewSeekBar2 = findViewById(R.id.textView3);
seekBar.setMax(600);
seekBar.setProgress(5);
seekBar2.setMax(900);
seekBar2.setProgress(5);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress1, boolean fromUser) {
getSummaryTwoSeekBarValue(progress1,progress2);
textViewSeekBar.setText(Integer.toString(progress1));
Log.i("Seeker1: ", Integer.toString(progress1));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress2, boolean fromUser) {
getSummaryTwoSeekBarValue(progress1,progress2);
textViewSeekBar2.setText(Integer.toString(progress2));
Log.i("Seeker2: ", Integer.toString(progress2));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public String getSummaryTwoSeekBarValue(int progress1, int progress2) {//int progress1, int progress2
this.progress1=progress1;
this.progress2=progress2;
final TextView textViewMain_ = findViewById(R.id.textViewMain);
int total = progress1 + progress2;
String totalString = String.valueOf(total);
Log.i("", totalString);
textViewMain_.setText(String.valueOf(totalString));
return totalString;
}
}```

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)

I cannot get OnSeekBarChangeListener to work in Android code

I'm trying to use OOP in my Java/Android code, but I don't understand why OnSeekBarChangeListener is not working in my new class.
Is it prohibited to use a OnSeekBarChangeListener outside main activity?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = new MediaPlayer();
timescroller = (SeekBar) findViewById(R.id.seekBar);
TimeScroller Tm = new TimeScroller();
Tm.mp = mp;
Tm.ts = timescroller;
Tm.mTimeScroller();
timescroller.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(b){
mp.seekTo(i);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
mp.setVolume(0,0);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
mp.setVolume(1,1);
}
});
}
And here is my class
package com.phillipvm.gridlayout;
import android.media.MediaPlayer;
import android.widget.SeekBar;
public class TimeScroller {
MediaPlayer mp;
SeekBar ts;
public void mTimeScroller(MediaPlayer _mp,SeekBar _ts){
mp =_mp;
ts = _ts;
ts.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(b){
mp.seekTo(i);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
mp.setVolume(0,0);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
mp.setVolume(1,1);
}
});
}
}
i can't see the code where you are using the class!
you are calling Tm.mTimeScroller(); but in your class the method takes 2 parameters !

How do I disable my Seekbar in Android with a Switch?

I want to disable my Seekbar and buttons with a switch which is Powerswitch
Here is a code snippet:
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
String sval = String.valueOf(i);
speakerVal.setText(sval);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
// button - Favorite Channel ABC
cabc = (Button) findViewById(R.id.buttonABC);
cabc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Set Favorite
curChannel.setText("007");
}
});
and here is my switch code:
final Switch switch1 = (Switch) findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(this);
more switch code:
public void onSwitchClicked(View view) {
final TextView PowerSwitch = (TextView) findViewById(R.id.PwrSwitchVal);
Switch sw = (Switch) view;
if (sw.isChecked()) {
PowerSwitch.setText("ON");
pwr = true;
} else {
PowerSwitch.setText("OFF");
pwr = false;
}
}
I know I can use the switch.isChecked(), but when I change the seekba1.setEnabled(false), I can't seem to get it to turn back on.
I haven't even tried to disable the buttons yet.
any help would be appreciated.
In your onSwitchClicked method, you should be calling seekbar.setEnabled in the if checked if-else statement. If checked seekbar.setEnabled(true) else seekbar.setEnabled(false)
try this.
switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0,
boolean arg1) {
if (arg1) {
seekbar1.setEnabled(true);
}
else{
seekbar1.setEnabled(false);
}
}
});

EditText search item in listview doesn't work

Hi guys i'm implementing a edittext in my listview to filter the items. I'm following this example, but nothing happen. It doesn't work and when i try to filter it doesn't do what i want.. This is my part of code of edittext:
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setTextFilterEnabled(true);
lv.setAdapter(listadaptor);
inputSearch.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.listadaptor.getFilter().filter(cs);
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
};
My listview is a list that show all application installed in the device.. I have this to load the list:
private class LoadApplications extends AsyncTask<Void, Void, Void> {
public ProgressDialog progress = null;
#Override
protected Void doInBackground(Void... params) {
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
listadaptor = new ApplicationAdapter(MainActivity.this,R.layout.snippet_list_row, applist);
return null;
}
#Override
protected void onCancelled() {
super.onCancelled();
}
protected void onDestroy() {
if(progress!=null)
if(progress.isShowing()){
progress.dismiss();
}
}
#Override
protected void onPostExecute(Void result) {
setListAdapter(listadaptor);
progress.dismiss();
super.onPostExecute(result);
}
#Override
protected void onPreExecute() {
progress = ProgressDialog.show(MainActivity.this, null,
"Caricamento applicazioni in corso...");
super.onPreExecute();
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
so listadaptor is my adapter.. Someone can help me? Thanks
Try this one ..
ArrayList<String> type_name_copy = new ArrayList<String>();
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable theWatchedText) {
ArrayList<String> type_name_filter = new ArrayList<String>();
String text = theWatchedText.toString();
for (int i = 0; i < array.size(); i++) {
if ((array.get(i).toLowerCase()).contains(text
.toLowerCase())) {
type_name_filter.add(array.get(i));
}
}
type_name_copy = type_name_filter;
listUpdate(type_name_copy);
}
});
}
public void listUpdate(ArrayList<String> data) {
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data));
}
}
I suggest you to use AutocompleteTextView which is Edittext with in-built Search functionality in android.

Categories

Resources