I am trying to set the max for my seekbar but it still defaults to 100.
XML
<SeekBar
android:id="#+id/seekBarRow1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:progress="0"
/>
Defining and setting max
final SeekBar seekBarRow1 = (SeekBar)findViewById(R.id.seekBarRow1);
seekBarRow1.setMax(50);
Don't know if it is effecting it but here is my listener
seekBarRow1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
progress = progress * 2;
numWeightsRow1 = progress;
numWeightsRow1 = numWeightsRow1 / 2;
numRow1.setText("" + progress);
updateWeight();
}
});
I have tried adding to my seekbar in xml but it still defaults to 100.
android:max="50"
Can you please tell, For What purpose you want to use SeekBar?
I mean, do you want to use SeekBar with MediaPlayer or with any other widget?
Suppose if you want to use SeekBar with MediaPlayer, then below is the best example for SeekBar with SeekBarChangeListener
Seekbar: Android Online/Local Audio Media Player with Play/Pause, Rewind, Forward, Previous and Next functionality with Seekbar
Related
so i have been trying to make this BMI calculator in android studio, where your BMI will instantly get calculated when you drag Progress-bar to any value .
To execute this, what i did was-
inside the the Bundle class,
i directly declared 2 variable and assigned one of them to get the data from the Height progress bar and another to get the data from weight progress bar.
and then i wrote the typical code for the calculation and the text setting.
And it did not work
For myself being a very new to this, i really cant find my mistake here.
so as a result what i got was- a still BMI which was the result for the initial value of the progress-bar that i have set for the height and weight.
i am getting a feeling that i made a very silly mistake somewhere that i still can not notice.
Would you be kind enough to point that out?
the Java code that i have used is below, please check it-
The code for the BMI is at the end. I feel the problem is lying there.
here is the screenshot of the app-
screenshot2
screenshot1
here is my code--
public class MainActivity extends AppCompatActivity {
TextView mheighttext,mweighttext2,mbmitext,mfunnymsg;
SeekBar mbar1, mbar2;
RelativeLayout mweightlayout, mlayout3;
ImageView mgincbtn1,mgincbtn2,mgdecbtn1,mgdecbtn2;
String mbmi, bmitext;
int wt= 45;
String wtwt="45";
int ht= 158;
String htht="158";
float rslt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
mheighttext=findViewById(R.id.heighttext);
mweighttext2=findViewById(R.id.weighttext2);
mbar1= findViewById(R.id.bar1);
mbar2=findViewById(R.id.bar2);
mgincbtn1=findViewById(R.id.gincbtn1);
mgincbtn2=findViewById(R.id.gincbtn2);
mgdecbtn1=findViewById(R.id.gdecbtn1);
mgdecbtn2=findViewById(R.id.gdecbtn2);
mbmitext=findViewById(R.id.bmitext1);
mfunnymsg=findViewById(R.id.funnymsg);
mbar1.setMax(246);
mbar1.setProgress(160);
mbar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
ht=progress;
htht=String.valueOf(ht);
mheighttext.setText(htht);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mbar2.setMax(244);
mbar2.setProgress(50);
mbar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
wt=progress;
wtwt=String.valueOf(wt);
mweighttext2.setText(wtwt);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mgincbtn1.setOnClickListener(v -> {
ht=ht+1;
if (ht>=0 && ht<247) {
mbar1.setProgress(ht);
htht = String.valueOf(ht);
mheighttext.setText(htht);
}
});
mgdecbtn1.setOnClickListener(v -> {
ht=ht-1;
if (ht>=0 && ht<247) {
mbar1.setProgress(ht);
htht = String.valueOf(ht);
mheighttext.setText(htht);
}
});
mgincbtn2.setOnClickListener(view -> {
wt=wt+1;
if (wt>=0 && wt<244) {
mbar2.setProgress(wt);
wtwt = String.valueOf(wt);
mweighttext2.setText(wtwt);
}
});
mgdecbtn2.setOnClickListener(v -> {
wt=wt-1;
if (wt>=0 && wt<244) {
mbar2.setProgress(wt);
wtwt = String.valueOf(wt);
mweighttext2.setText(wtwt);
}
});
int htt = mbar1.getProgress();
int wtt = mbar2.getProgress();
float httt=htt/100;
rslt= wtt/(httt*httt);
mbmi=Float.toString(rslt);
mbmitext.setText(mbmi);
}
}
}
You would want to put the snippet of code that calculates the BMI (perhaps the last 4 lines of code) in both onProgressChanged() to instantly calculate BMI as height/weight gets changed. Since you're setting global variables for both height and weight, you'll get current values for each of them.
You can also make a function for calculating the BMI like this:
private float calculateBMI(int height, int weight) {
float ht = height / 100;
float result = weight / (ht * ht);
return result;
}
and have this function call in both onProgressChanged() like so (for the weight for example):
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
wt = progress;
mweighttext2.setText(String.valueOf(getBMI(ht, progress)));
}
Move this your code...
int htt = mbar1.getProgress();
int wtt = mbar2.getProgress();
float httt=htt/100;
rslt= wtt/(httt*httt);
mbmi=Float.toString(rslt);
mbmitext.setText(mbmi);
to a method say
public void computeBMI(){
int htt = mbar1.getProgress();
int wtt = mbar2.getProgress();
float httt=htt/100;
rslt= wtt/(httt*httt);
mbmi=Float.toString(rslt);
mbmitext.setText(mbmi);
}
Then add a button in your UI say mCompute. In your oncreate method you can then implement that buttons onClickListener to call computeBMI() above.
That is:
yourButton.setOnclickListener((v)->{
computeBMI();
});
I'm creating a small paint android app using android studio java. I create in the menu an icon for changing the color of the drawing. Once the user click on it a new AlertDialog should appear showing 4 seekbars for ARGB colors. I ended with this code but I still don't know why it's not working. Can someone please help me?
private Drawing draws;
private SeekBar alphaSeekBar;
private SeekBar redSeekBar;
private SeekBar greenSeekBar;
private SeekBar blueSeekBar;
private void showColorDialog(){
currentAlertDialog = new AlertDialog.Builder(this);
View view = getLayoutInflater().inflate(R.layout.color_dialog, null);
alphaSeekBar = view.findViewById(R.id.alphaSeekBar);
redSeekBar = view.findViewById(R.id.redSeekBar);
greenSeekBar = view.findViewById(R.id.greenSeekBar);
blueSeekBar = view.findViewById(R.id.blueSeekBar);
colorView = view.findViewById(R.id.colorView);
alphaSeekBar.setOnSeekBarChangeListener(colorSeekBarChanged);
redSeekBar.setOnSeekBarChangeListener(colorSeekBarChanged);
greenSeekBar.setOnSeekBarChangeListener(colorSeekBarChanged);
blueSeekBar.setOnSeekBarChangeListener(colorSeekBarChanged);
int color = draws.getDrawingColor();
alphaSeekBar.setProgress(Color.alpha(color));
redSeekBar.setProgress(Color.red(color));
greenSeekBar.setProgress(Color.green(color));
blueSeekBar.setProgress(Color.blue(color));
Button setColorButton = view.findViewById(R.id.setColorButton);
setColorButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pikassoView.setDrawingColor(Color.argb(
alphaSeekBar.getProgress(),
redSeekBar.getProgress(),
greenSeekBar.getProgress(),
blueSeekBar.getProgress()
));
colorDialog.dismiss();
}
});
currentAlertDialog.setView(view);
currentAlertDialog.setTitle("Choose color");
colorDialog = currentAlertDialog.create();
colorDialog.show();
} private SeekBar.OnSeekBarChangeListener colorSeekBarChanged = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
draws.setBackgroundColor(Color.argb(
alphaSeekBar.getProgress(),
redSeekBar.getProgress(),
greenSeekBar.getProgress(),
blueSeekBar.getProgress()
));
colorView.setBackgroundColor(Color.argb(
alphaSeekBar.getProgress(),
redSeekBar.getProgress(),
greenSeekBar.getProgress(),
blueSeekBar.getProgress()
));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
First, this alphaSeekBar.setProgress(Color.alpha(color)); is wrong. setProgress is meant for setting int value to show how much progress seekbar has.
If your seekBar Max is set to 100 (alphaSeekBar.setMax(100);) then this alphaSeekBar.setProgress(50); means seekBar is progressed half way.
Second, to set colors use this:
alphaSeekBar.setProgressTintList(Color.alpha(color));
alphaSeekBar.setThumbTintList(Color.alpha(color));
Or, use this:
alphaSeekBar.getProgressDrawable().setColorFilter(Color.alpha(color), PorterDuff.Mode.MULTIPLY);
try different tint modes in place of PorterDuff.Mode.MULTIPLY to get your desired result
good luck
EDIT:
For setting a custom view to Alert Dialog
currentAlertDialog = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
View view = getLayoutInflater().inflate(R.layout.color_dialog, null);
currentAlertDialog.setView(view);
then you can get those seekbar references like below:
alphaSeekBar = currentAlertDialog.findViewById(R.id.alphaSeekBar);
redSeekBar = currentAlertDialog.findViewById(R.id.redSeekBar);
greenSeekBar = currentAlertDialog.findViewById(R.id.greenSeekBar);
blueSeekBar = currentAlertDialog.findViewById(R.id.blueSeekBar);
colorView = currentAlertDialog.findViewById(R.id.colorView);
I'm trying to change the font of the overall app by using a seekbar in the settings activity that I've created. Below is what I have gotten from online so far, but it changes only the small amount of text with the ID "changeFont":
// Fonts Scale Slider
fontScaleSlider = findViewById(R.id.fontScaleSlider);
view = findViewById(R.id.changeFont);
prefs = getPreferences(MODE_PRIVATE);
float fs = prefs.getFloat("fontsize", 10);
fontScaleSlider.setProgress((int)fs);
view.setTextSize(fontScaleSlider.getProgress());
fontScaleSlider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar fontScaleSlider){
prefs = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor ed = prefs.edit();
ed.putFloat("fontsize", view.getTextSize());
ed.commit();
}
#Override
public void onStartTrackingTouch(SeekBar fontScaleSlider) {
}
#Override
public void onProgressChanged(SeekBar fontScaleSlider, int progress,
boolean fromUser){
view.setTextSize(progress);
// Set text size of the whole app here
}
And the XML file:
<SeekBar
android:id="#+id/fontScaleSlider"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="13dp"
android:layout_marginEnd="13dp"
android:layout_marginBottom="82dp"
android:max="30"
app:layout_constraintBottom_toTopOf="#+id/logInButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fontSliderLabel" />
In the implemented method onProgressChanged you can save the seek bar value to the shared preferences.
#Override
public void onProgressChanged(SeekBar fontScaleSlider, int progress,boolean fromUser){
SharedPreferences.Editor ed = prefs.edit();
ed.putInt("fontSize", progress);
ed.apply();
}
And then use the saved value to set the text size of each text when the activity starts
int textSize = prefs.getInt("fontSize", defaultValue); // put your default value here like 16
textView.setTextSize(textSize);
//similarly set the textsize for other views
I am using JMF for playing mp3 Audio file.It is playing but I have no idea how to control its volume? I am using
Player audioPlayer = Manager.createRealizedPlayer(url);
audioPlayer.start();
See Player.getGainControl() which:
Gets the object for controlling this Player's audio gain. If this player does not have a GainControl, getGainControl returns null. For example, getGainControl might return null if the Player does not play audio data.
On GainControl:
GainControl is an interface for manipulating audio signal gain.
Gain and Gain Measures
Gain is a multiplicative value applied to an audio signal that modifies the amplitude of the signal. This interface allows the gain to be specified in either decibels or using a floating point value that varies between 0.0 and 1.0.
I did it on Android in this fashion.
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
final int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volumecontrol = (SeekBar) findViewById(R.id.seekBar);
volumecontrol.setMax(maxVolume);
volumecontrol.setProgress(curVolume);
volumecontrol.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromuser) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress, 0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
E/AndroidRuntime(7183): Caused by: java.lang.ClassCastException: android.widget.SeekBar cannot be cast to android.widget.EditText
is this true?
if you can't use a seekbar to change text in a EditText element then what should i use?
a static TextView?
because i need a slider aka SeekBar for my app
thanks in advance
here's the code
private OnSeekBarChangeListener customSeekBarListener = new OnSeekBarChangeListener()
{
// update currentCustomPercent, then call updateCustom
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
// sets currentCustomPercent to position of seekBars's thumb
currentCustomPercent = seekBar.getProgress();
updateCustom(); // updates EditTexts for custom part and Total
} // end method on progressChanged
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
} // end method onStartTrackingTouch
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
}; // end onSeekBarChangeListener
the program doesn't error but it force closes... and the logcat say's that this is screwed up.. 6 hours worth of work... down the toilet... #flush
Probably you are casting seekBarobject to editText in method:
onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
If yes, just do the following:
onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
editText.setText(""+progress);
}