-3

I use SeekBar in my app with ticks. Currently this SeekBar is defined in xml file:

<SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/Base.Widget.AppCompat.SeekBar.Discrete"/>

and it looks ok but I want to define this element programmatically so I've added this instead:

SeekBar seekBar = new SeekBar(getContext(), null, R.style.Widget_AppCompat_SeekBar_Discrete);

but then the seekBar is invisible. Does anyone know what's wrong? It's visible if I use only:

SeekBar seekBar = new SeekBar(getContext();

but i need ticks.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Giks91
  • 273
  • 3
  • 5
  • 20

1 Answers1

0

this works for me:

First, create a layout (e.g. layout_seekbar.xml):

<?xml version="1.0" encoding="utf-8"?>
<SeekBar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/Widget.AppCompat.SeekBar.Discrete" />

then, you can programmatically add the seekbar in your code:

SeekBar bar = (SeekBar)LayoutInflater.from(this).inflate(R.layout.layout_seekbar, null);
bar.setMax(10);
...
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    ...
    }
    public void onStartTrackingTouch(SeekBar seekBar) {}
    public void onStopTrackingTouch(SeekBar seekBar) {}
});

myLinearLayout.addView(bar);

greetings, hope that helps

Cor
  • 389
  • 1
  • 2
  • 14