Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display label using Slider of Material component?

I am using the Slider of Material component:

https://github.com/material-components/material-components-android/blob/master/docs/components/Slider.md

I am trying to display a label when the thumb is moving, which seem to be supported with the labelFormatter attribute.

here is what my code looks like:

Slider s = new Slider(context);
    s.setLabelFormatter(new Slider.LabelFormatter() {
        @NonNull
        @Override
        public String getFormattedValue(float value) {
            return "MY STRING";
        }
    });

When I go line by line with debugger it goes through this function:

private void drawLabelText(@NonNull Canvas canvas, int width, int top) {
labelTextPaint.getTextBounds(labelText, 0, labelText.length(), labelTextBounds);
int left = trackSidePadding + (int) (thumbPosition * width) - labelTextBounds.width() / 2;
canvas.drawText(labelText, left, top - labelTextTopOffset - thumbRadius, labelTextPaint);  }

but no text is displayed only the slider...

I am kind of new in androïd and I am surely missing something.

Thanks for help :)

EDIT 1:

Here is the whole code simplified :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    CoordinatorLayout layout = new CoordinatorLayout(this);
    setContentView(layout);

    // I am Using Coordinator Layout for current activity so...
    CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    //Setting gravity to CENTER
    layoutParams.gravity = Gravity.CENTER;

    Slider slider = new Slider(this);
    slider.setLabelFormatter(new Slider.LabelFormatter() {
        @NonNull
        @Override
        public String getFormattedValue(float value) {
            return "MY STRING";
        }
    });

    layout.addView(slider, layoutParams);
}

But still not working ...

output result

enter image description here

https://youtu.be/obV4K-Nxu-0

EDIT 2:

Updating Material component from version: '1.2.0-alpha02' to version: '1.2.0-alpha05' fixed the issue.

like image 303
mache Avatar asked Apr 02 '20 15:04

mache


1 Answers

Actually i wanted to comment but because of Reputation Constraints i couldn't, Looks like you are adding a view programatically in your activity. Here i have implemented new MaterialComponent Slider with LabelFormatter

    ViewGroup group = findViewById("YOUR_ACTIVITY_LAYOUT");

    // I am Using Coordinator Layout for current activity so...
    CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    //Setting gravity to CENTER
    layoutParams.gravity = Gravity.CENTER;

    Slider slider = new Slider(this);
    slider.setLabelFormatter(new Slider.LabelFormatter() {
        @NonNull
        @Override
        public String getFormattedValue(float value) {
            return "MY STRING";
        }
    });

    group.addView(slider, layoutParams);
}

And then there you go... Slider Demo

like image 131
Amanth Rai Avatar answered Nov 13 '22 04:11

Amanth Rai