How to give number of seekbar on android.
please answer to my question becouse I need it.
Thank you
Xml:
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Java:
public class Setting extends ActionBarActivity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
}
}
You can use "DiscreteSeekBar" from below url:
https://github.com/AnderWeb/discreteSeekBar
Thanks.
First, set a max value for the SeekBar in XML.
Add the following attribute to the SekkBar:
android:max="40"
Then in code:
SeekBar seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
int currentValue = seekBar1.getProgress();
getProgress() will return the current value (from 0 to 40);
But you should use a listener for changes and get new value:
seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
currentValue = progress;
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar)
{
}
});
Try this for 100 number as maximum seek:
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:max="100"/>
Selection of seek bar,
onProgressChanged provide real time selection change as you seek, but when you stop then onProgressStop is encountered, then do as you want :
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progress = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
progress = progresValue;
textView.setText(progress + "/" + seekBar.getMax());
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Do something here, if you want to do anything at the start of
// touching the seekbar
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Display the value in textview
textView.setText(progress + "/" + seekBar.getMax());
}
});
Related
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) {
}
});
}
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)
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();
}
}
}
I'm using the following code for a slide in / slide out toggle for an AppBarLayout.
public void showToolbar(boolean show) {
if (appBar == null) {
Log.e(TAG, "showToolbar: Toolbar is null");
return;
}
boolean toolbarShown = Utils.isViewVisible(appBar);
Log.d(TAG, "showToolbar: shown:" +shown);
boolean changed = (show != toolbarShown);
if (changed) {
if (show) {
Log.d(TAG, "showToolbar: showing");
appBar.setVisibility(View.VISIBLE);
appBar.animate()
.translationY(0)
.setInterpolator(new DecelerateInterpolator())
.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
appBar.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animator) { }
#Override
public void onAnimationCancel(Animator animator) { }
#Override
public void onAnimationRepeat(Animator animator) { }
})
.start();
} else {
Log.d(TAG, "showToolbar: hiding");
appBar.animate()
.translationY(-toolbar.getBottom())
.setInterpolator(new DecelerateInterpolator())
.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) { }
#Override
public void onAnimationEnd(Animator animator) {
appBar.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationCancel(Animator animator) { }
#Override
public void onAnimationRepeat(Animator animator) { }
})
.start();
}
} else {
Log.d(TAG, "showToolbar: no change");
}
}
The animation works perfectly except for the first time showToolbar(true) is called to show the toolbar. The view just shows up without animation the first time. I have scoured the site and found similar questions but the solutions doesn't seem to be working for me.
It might be worth noting that this happens only when we want the appBar to be hidden first. My guess is that maybe for the animation to
Update 1:
public static boolean isViewVisible(View view) {
if (View.VISIBLE == view.getVisibility()) return true;
else return false;
}
Update 2
I have removed isViewWithinScreenBounds() method because that check isn't really needed.
Be sure to set the initial values for visibility and translationY.
If you want your toolbar to be initially hidden and show up with the first animation, be sure to set android:visibility="invisible" and NOT "gone", and negative android:translationY like -56dp.
Try with this
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:translationY="-120dp"
android:layout_height="120dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SOME TEXT HERE" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Activity code
#BindView(R.id.app_bar)
AppBarLayout appBar;
#BindView(R.id.toolbar)
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.some_layout);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
animateToolbar(false); //Just for testing purpose - delay execute of function animateToolbar() for 4 seconds
}
}, 4000);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
animateToolbar(true);
return super.onCreateOptionsMenu(menu);
}
private void animateToolbar(boolean show) {
if (show) {
// As you can see in the xml layout initial position of the appBar is translated by its height to the top of the screen "-" sign
// Slide int from out of the screen to initial position -> from -120 dp (height of the app bar, see xml) to 0 dp
appBar.animate()
.translationY(0)
.setDuration(1000)
.start();
} else {
// Slide out from initial position to the top of the screen -> from 0 dp to -120 dp (height of the app bar, see xml)
appBar.animate()
.translationY(-appBar.getHeight())
.setDuration(1000)
.start();
}
}
Above the generateData() method, i declared an integer variable bw, i want it to be equal to the value of the current seekbar progress, how to do that? or actually can i directly use the value of progress for calculation??
Sorry it may sound a stupid question but i am absolute new to programming.
If I put bw=progress; inside the onprogresschanged, the outcome of bw*10 and bw*9 will become 0 no matter how i slide the seekbar.
public class MainActivity extends AppCompatActivity {
int bw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyAdapter adapter = new MyAdapter(this, generateData());
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
final TextView seekBarValue = (TextView) findViewById(R.id.valuetextview);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
bw=progress;
seekBarValue.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
//Detail of drugs in following formatL "Drug Name", doseperkg,"frequency"
private ArrayList<DrugItem> generateData() {
ArrayList<DrugItem> items = new ArrayList<DrugItem>();
items.add(new DrugItem("Ceclor", bw * 10, "mg every 8 hours"));
items.add(new DrugItem("Cedax", bw * 9, "mg every 24 hours"));
items.add(new DrugItem("Zinnat", bw * 10, "mg every 12 hours"));
items.add(new DrugItem("Zithromax", bw * 10, "mg every 24 hours"));
return items;
}
}
yes obvious.
In below method int progress this is your main point value. U can use it for calculation .
but make sure work around in onProgressChanged method.
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// your calculation...
}
in the callback methods of seekbar
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
bw=progress;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
in addition to that you may want to set a maximum value to seekbar's seek
seekBar.setMax(max_value-min_value);