How to change Android SeekBar track start position? - android

I'd like to set the SeekBars's track start position so it does not start from the left side of the seekbar, but form an arbitrary position. Here is a photoshop image how it should look like:
http://i.imgur.com/QCMEu.png
It should be just a graphical effect, the SeekBar's underlying logic is not changed.
I tried to change the Seekbar.getprogressDrawable.SetBounds() to change the track image position, but no luck.

add this proprety
android:progress="2"

You can set progress of SeekBar in xml as:
android:progress="10"
android:max="90" <!-- maximum seekbar progress -->
you can programmatically set the progress of SeekBar as:
seekBar.setProgress(10);

May be your problem is similar to Seekbar for two values [-50, 0, 50].
Thanks to Commonsware to point to right direction. I wrote a class inspired by code.google.com/p/range-seek-bar) to get the solution.
https://github.com/vashisthg/StartPointSeekBar

By Programatically,we can start the progress and set the maximum of seekbar progress.
//start from <starting value>
seekBar.setProgress(<strating_value>);
//Set to maximum Value<max_value>
seekBar.setMax(<max_value>);

You can add this:
android:rotation="180"
to the XML and then the seekbar will flip the way you want

Just faced the same problem.
That's how I have solved it.
Assume, we need seekbar starting from 10 and ending at 150.
#Override
protected void onCreate(Bundle savedInstanceState)
{...
yourSpinner = (Spinner) findViewById(R.id.your_spinner);
yourSpinner.setMax(150 - 10);
int newProgress;
yourSpinner.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
newProgress= 10 + progress;
Toast.makeText(getApplicationContext(), String.valueOf(newProgress), Toast.LENGTH_LONG).show();
}
});
}

Related

Android sliders with fixed numbers

I'm trying to implement a discrete slider with fixed values, but the only thing I can set is the valueFrom, valueTo and stepSize.
Here is my code how I'm trying to do
<com.google.android.material.slider.Slider
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="8dp"
app:tickColor="#color/colorSecondaryLight"
app:tickColorActive="#color/colorSecondary" />
Is there a way to set fixed values on the slider? (Values that I will use 2, 5, 10, 25, 50 and 100)
The solution that worked for me was this lib called BubbleSeekBar
Step 1 - Add the dependencies on your Gradle.
implementation "com.xw.repo:bubbleseekbar:3.20-lite"
Step 2 - Create your BubbleSeekBar on your XML and add the attributes that you need. For my case, the example below worked.
<com.xw.repo.BubbleSeekBar
android:id="#+id/bubbleSeekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:bsb_auto_adjust_section_mark="true"
app:bsb_hide_bubble="true"
app:bsb_min="2"
app:bsb_section_count="5"
app:bsb_section_text_position="below_section_mark"
app:bsb_seek_by_section="true" />
Step 3 - Since I needed custom options, I initialized on my onCreate with the array of values declared on my string.xml
bubbleSeekBar.setCustomSectionTextArray { sectionCount, array ->
array.clear()
for ((index, value) in resources.getStringArray(R.array.xxxx)
.withIndex()) {
array.put(index, value)
}
array
}
Step 4 - You can capture the changes or set a value with the methods below.
bubbleSeekBar.onProgressChangedListener
bubbleSeekBar.setProgress()
The lib is pretty good and worked for me. For more info take a look at the link at the top of this answer.
You'd be better off using SeekBar
<SeekBar
android:id="#+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:thumb="#drawable/ic_location"
android:theme="#style/Widget.AppCompat.SeekBar.Discrete" />
You can use a SeekBar and define the range of values, and in order to remove the smoothness/refining scrolling, set OnSeekBarChangeListener to it and override onProgressChanged() with the valid range of the SeekBar values you need by setting the progress again as below.
Example
Using a SeekBar with a max value of 100, and a step of 10; so the total valid values are 10.
SeekBar seekBar = findViewById(R.id.seekbar);
seekBar.setMax(100);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean
fromUser) {
seekBar.setProgress((progress / 10) * 10);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});

Change seekbar color depending on progress

I have a SeekBar in one of my activites in my app.
That seekbar gives me an importance of the object I create. Importance can be 0,1 or 2.
I'd like to dinamically change the background color of my SeekBar when I'm using it. How can I do that ?
Here are the colors I'd like to use :
Importance 0 : "#ff8080"
Importance 1 :"#ff0000"
Importance 2 :"#4d0000"
Thank you for your help, and tell me if you need more code !
BarreImportance.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(i==0)
{ //it does not accept my colors..
BarreImportance.getProgressDrawable().setColorFilter(Color.parseColor("#ff8080"));
}
if(i==1)
{
//it changes the whole background, not what I want... BarreImportance.setBackgroundColor(Color.parseColor("#ff0000"));
}
You need to use OnSeekBarChangeListener's onProgressChanged callback and change the color from inside there like this: mySeekBar.getProgressDrawable().setColorFilter(..) and mySeekBar.getThumb().setColorFilter(..)

How to get the font size set in the Textview

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();

SeekBar: ProgressDrawable does only show after first progress Change

I want to implement a easy hsv color picker that fit well into my app. As known form color Pickers I would like to change the background of the seekBars according to the value of the other seekBars.
But when I start the activity I am seeing this:
After the first touch of one of the seekBars (no matter which) I get this result and after the first touch the layout stays like this and does not change with further touches (which is also what I would have expected according to my layout, the 2 seekBars on top are 20dip and the 1 below is as high as the thumb):
Like seen in the second picture I would like to have the seekBar from beginning.
Here my layout:
<SeekBar
android:id="#+id/SbColorPickerHue"
android:layout_width="match_parent"
android:layout_height="20dip"
android:max="360"
android:thumb="#drawable/colorpickerthumb" />
<SeekBar
android:id="#+id/SbColorPickerSaturation"
android:layout_width="match_parent"
android:layout_height="20dip"
android:max="255"
android:thumb="#drawable/colorpickerthumb"/>
<SeekBar
android:id="#+id/SbColorPickerValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:thumb="#drawable/colorpickerthumb"/>
With a OnSeekBarChangeListener in onProgressChanged() and of course in onCreate (to initalize the seekBars) I am calling the following function (To safe you a lot of code only the Hue Gradient is shown):
private void setGradients() {
mHueGradient = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,mColors);
mHueGradient.setGradientType(GradientDrawable.LINEAR_GRADIENT);
mHueGradient.setCornerRadius(mRadius);
mHueGradient.setStroke(mStrokeWidth, mStrokeColor);
mSbHue.setBackgroundDrawable(mHueGradient);
}
I tried many different things already:
set the Gradients twice in onCreate
set the Gradients again in onRescume
as can be seen in the layout xml I set some of the SeekBar to a fixed height (20dp) and others to wrap_content. The behaviour changed a little (as can be seen in the pictures). But still not satisfying.
Invalidate either the seekBar or the Drawable
Force Layout
Measure the drawable
Result is always the same.
What am I doing wrong here? Am I missing somthing?
Edit 1:
The SeekBar and the ChangeListener are defined as follows:
mSbHue = (SeekBar)findViewById(R.id.SbColorPickerHue);
setGradients();
mSbHue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
setGradients();
}
});
If you move setGradients() after the listener it works.
mSbHue = (SeekBar)findViewById(R.id.SbColorPickerHue);
mSbHue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
setGradients();
}
});
setGradients();

Unclickable Seekbar in android listview

I have a Listview in my application. Listview rows are clickable. I have introduced a seek bar in each row of a Listview. Despite settings android:clickable="false" for Seekbar in layout xml, I am still able to click on it and move seek bar as desired. I don't want Seekbar to be clickbale but I do want Listview row to clickable.
Any pointers will be appreciated.
Here's my layout file
android:progressDrawable="#drawable/slider_range"
android:thumb="#drawable/slider_thumb"
android:layout_width="80dip"
android:layout_height="12dip"
android:focusable="false"
android:clickable="false"
android:longClickable="false"
seekBarObj.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
int originalProgress;
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
//Nothing here..
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
originalProgress = seekBar.getProgress();
}
#Override
public void onProgressChanged(SeekBar seekBar, int arg1, boolean fromUser) {
if( fromUser == true){
seekBar.setProgress( originalProgress);
}
}
});
Try setting the enabled attribute to false. You may have to manually adjust the color if you want to avoid it appearing "greyed out"
e.g.
seekBar.setEnabled(false);
I've now got this working using both setFocusable and setEnabled to false, I've implemented a solution to enable the seekbar for a single adjustment when the list item is clicked too if you are interested.
Add focusable=false to the seekbar.

Categories

Resources