I have a progressbar that i have defined for accepting the rating
RATINGbar = (SeekBar)findViewById(R.id.RATINGseekBarID); // make seekbar object
RATINGbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
RATINGtextProgress = (TextView)findViewById(R.id.RATINGtextViewProgressID);
RATINGtextProgress.setText("Rating:: "+progress);
seekBar.setMax(5);
}
});
This Bar accepts maximum of 5 number as i have defined !
when i select it and scroll horizontally, it accepts values as
1-2-3-4-5
How to make the progress bar take values like
1-1.5-2-2.5-3-3.5-4-4.5-5
what changes should i need to make here
How to make my progress bar accept values in interval of 0.5
Hope i am clear
How to make my progress bar accept values in interval of 0.5
Short answer:
You can't. ProgressBar takes only int values for actual and max progress and you can't change this behaviour. This how it's designated by OS.
Update: Look also at this thread.
You can't change ProgressBar intervals to float. Instead, try using RatingBar.
Related
Is there a way to set the color of a progressBar in XML, while the progress is going on?
I want to change the color of my progress at 80% to red.
Is there a way to to this in xml? So I just can copy paste my progressBar without coding it manually everytime...
No, you cannot change the color at runtime using xml.
If you can change your ProgressBar to SeekBar, you can easily listen to the progress change & as it reaches 80, you can change the color.
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
So I have a seekbar that I want to display or setProgress the value of that I get from SharedPreferences String, but since I can't use decimals, I am not 100% sure how to do this, can someone point me in the right direction?
String BarValue = "3.5";
Bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
float value = 0;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
value = ((float)progress / 5);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
SharedPreferences.Editor e = sp.edit();
e.putString("barValue", String.valueOf(value));
e.apply();
}
});
Android Seekbar does not support float, so use integer in Seekbar and convert that to float as you want. I think that's the best way.
For example if you want to use single decimal place values like 11.6 you can set 116(11.6 * 10) to the Seekbar, and on retrieval divide that by 10. It will do the job.
Android default SeekBar doesn't provide decimal value, if you want than you can use any custom SeekBar from following,
https://github.com/woxingxiao/BubbleSeekBar
https://github.com/syedowaisali/crystal-range-seekbar
https://github.com/vashisthg/StartPointSeekBar
I am using the typeface in my text view and it is all looking good. Now I want to set the seekbar which will help the user to adjust the text size under the text view.
I have just used the seekbar and played with it. this is how I am using my Seekbar to make the text larger
/// SeekBar Progress
sk.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int p=0;
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if(p<30)
{
p=30;
sk.setProgress(p);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
p=progress;
myTextView.setTextSize(p);
}
});
this is working and changing the size of text in the text view, But now there comes the problem :
Problem :
Problem is , the size is increasing and decreasing also when we take the seek bar to the left from its mid point. where as I want to set the minimum and maximum value. So to set the minimum and maximum I need to get the size of text inside the text view . and I have not found any method to get the textsize like for example myTextview.getTextSize() , there is no such method like it.
Let me tell you I am setting the size of the text view from the dimens file in the different values folder for supporting all the device and to make my text look good on tablets and small devices.
So now All I need to get is My text size inside the Text view . Please help me in getting the text size. thank you.
There is TextView.getTextSize(). You can also go TextView.getPaint().getTextSize().
try this
text_one.setText("your text");
text_one.measure(0, 0); //must call measure!
text_one.getMeasuredHeight(); //get height
text_one.getMeasuredWidth();
Do check that your myTextView is indeed TextView and not just View when you initialize it. e.g.
TextView myTextView = (TextView) findViewById(yourTextViewId);
I think the simplest answer is
myTextView.getTextSize();
Actually I was casting my value wrong. and Now it is solved. after doing this
float size = myTextView.getTextSize();
My SeekBar thumb must move within range 20 to 5000.
I need to prevent the thumb to move below 20. when this amount is reached, a toast message appear telling the user "minimum value reached".
However when i slide to minimum value and forced slide on limit, that is multiple slide towards 20, several popup appears onscreen. How can i limit to only one popup on screen on multiple slide towards minimum or maximum value.
Here is my .setOnSeekBarChanged listener :
sbMensualite.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = progress / 1;
progress = progress * 1;
if (progress<20){
progress=20;
}
else {}
if (duree_int>=Constant.DUREE_MAX_VALUE){
Toast.message="durée maximum atteinte";
m.startActivity(new Intent(m, Toast.class));
}else if (duree_int<=Constant.DUREE_MIN_VALUE){
Toast.message="durée minimum atteinte";
m.startActivity(new Intent(m, Toast.class));
}else {
//set progress duree android
sbDDuree.setProgress(duree_int);
//set amount of month for duree
txDDure.setText(Util.doubleNoDp(duree));
}
//set mensualite amount
txMensualite.setText(Util.decimalFormat(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I need to prevent the thumb to move below 20. when this amount is reached, a toast message appear telling the user "minimum value reached".
First. Make the range of your slider be 0-4980. And every time you read the value add +20. If you want to set the value programmatically from a variable add -20.
Second, you really should NOT show a toast to tell that the minimum value was reached, because, using the method described in the previous paragraph, this will be obvious.
EDIT:
Would limiting the duree_int variable work for you???
if (duree_int>Constant.DUREE_MAX_VALUE){
duree_int = Constant.DUREE_MAX_VALUE;
Toast.message="durée maximum atteinte";
m.startActivity(new Intent(m, Toast.class));
}else if (duree_int<Constant.DUREE_MIN_VALUE){
duree_int = Constant.DUREE_MIN_VALUE;
Toast.message="durée minimum atteinte";
m.startActivity(new Intent(m, Toast.class));
}else {
To cancel your existing toast and show a new one after, try this:
Toast myToast = new Toast(getApplicationContext());
sbMensualite.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = progress / 1;
progress = progress * 1;
if (progress<20){
progress=20;
}
if (duree_int>=Constant.DUREE_MAX_VALUE){
myToast.cancel(); //hide existing toast
myToast.setText("durée maximum atteinte");
myToast.setDuration(Toast.LENGHT_SHORT);
myToast.show();
}else if (duree_int<=Constant.DUREE_MIN_VALUE){
myToast.cancel(); //hide existing toast
myToast.setText("durée minimum atteinte");
myToast.setDuration(Toast.LENGHT_SHORT);
myToast.show();
}else {
//set progress duree android
sbDDuree.setProgress(duree_int);
//set amount of month for duree
txDDure.setText(Util.doubleNoDp(duree));
}
//set mensualite amount
txMensualite.setText(Util.decimalFormat(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I would like to have the color of the progress bar (specifically the secondary progress that is updated with setSecondaryProgress) to change dynamically at runtime when it crosses a certain point.
I am already able to substitute my own drawable into the bar during my Activity's onCreate function (changing color from yellow to green) by calling setProgressDrawable and substituting a resource I've copied from the default Android version of a horizontal progress (SDK/platforms/android-2.1/data/res/drawable/progress_horizontal.xml). However, updating this at runtime (yes, from the UI thread via an AsyncTask) causes the entire progress bar except for the thumbtab to go black or transparent.
I think this is what you are looking for
This can be achived by creating custom drwables for background and progress of seak bar
See this link for sample code.
Pls see the below links also
http://javatechig.com/android/android-seekbar-example/
Change the color of a seekbar on onProgressChanged
SeekColorActivity.java
package com.exercise.seekcolor;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.SeekBar;
public class SeekColorActivity extends Activity {
private int seekR, seekG, seekB;
SeekBar redSeekBar, greenSeekBar, blueSeekBar;
LinearLayout mScreen;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mScreen = (LinearLayout) findViewById(R.id.myScreen);
redSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_R);
greenSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_G);
blueSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_B);
updateBackground();
redSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
greenSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
blueSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
}
private SeekBar.OnSeekBarChangeListener seekBarChangeListener
= new SeekBar.OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
updateBackground();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
};
private void updateBackground()
{
seekR = redSeekBar.getProgress();
seekG = greenSeekBar.getProgress();
seekB = blueSeekBar.getProgress();
mScreen.setBackgroundColor(
0xff000000
+ seekR * 0x10000
+ seekG * 0x100
+ seekB
);
}
}
A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys.
When SeekBarChange, the onProgressChanged() of OnSeekBarChangeListener will be called, and the background of the screen(LinearLayout) will be updated according to the value (SeekBar.getProgress()).
main.xml