sivanesan1
sivanesan1

Reputation: 839

Android sliders programmatically

enter image description here> i want to implement slider bar in my application without using xml. i try to search using google but not able to get any sample code.how to create slider bar in android, pls guide me,

Upvotes: 1

Views: 1894

Answers (1)

user3751016
user3751016

Reputation: 36

This should work, it uses the built in SeekBar. The onProgressChanged is passed the location of the progress and an integer between the value 0-100 when it is changed.

import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.SeekBar;

public class SeekBarExample extends Activity {
    LinearLayout ll;
    SeekBar sBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ll = new LinearLayout(this);
        sBar = new SeekBar(this);
        SeekBar.OnSeekBarChangeListener abc = 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) {
                //Executed when progress is changed
                System.out.println(progress);
            }
        };
        sBar.setOnSeekBarChangeListener(abc);
        LayoutParams sBarLayParams=new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        ll.addView(sBar,sBarLayParams);
        setContentView(ll);
        }
}

Upvotes: 2

Related Questions