Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom ProgressBar widget
    primarykey
    data
    text
    <p>I am trying to do something similar to <a href="https://stackoverflow.com/questions/3682021/uislider-with-certain-possible-values">this</a> but in Android.</p> <p><img src="https://i.stack.imgur.com/QjLXY.png" alt="alt text"></p> <p>In Android I can extend the <code>ProgressBar</code> but I am doubting of how to add the <code>TextViews</code> on top. In iphone it was easy because I can use absolute positions, but not here.</p> <p>Any ideas?</p> <p>EDIT: I decided to use <code>SeekBar</code> instead of <code>ProgressBar</code> to add the thumb drawable. I commented below. Some points to notice:</p> <ul> <li>I am using hardcoded values, actually three but it can be more or less.</li> <li>When the thumb is moved it moves to 50 but it should move to the different options.</li> <li>I am using pixels instead of dpi. I should fix that.</li> <li>I need to solve the lack of animation when the thumb moves.</li> </ul> <p>My progress so far:</p> <pre><code>public class SliderFrameLayout extends FrameLayout implements OnSeekBarChangeListener { private SeekBar mSlider; private Context mContext; private int mSize = 3; private TextView[] mTextViews; private String[] mTexts = {"Nafta", "Gas", "Gasoil"}; public SliderFrameLayout(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; setWillNotDraw(false); mTextViews = new TextView[mSize]; addSlider(); addTextViews(); } private void addTextViews() { for ( int i=0 ; i &lt; mSize ; i++ ) { TextView tv; tv = new TextView(mContext); tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); tv.setText(mTexts[i]); mTextViews[i] = tv; addView(tv); } } private void addSlider() { FrameLayout fl = new FrameLayout(mContext, null); LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); fl.setLayoutParams(params); fl.setPadding(30, 30, 30, 0); mSlider = new SeekBar(mContext, null); LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); //lp.setMargins(30, 30, 30, 0); //mSlider.setPadding(30, 30, 30, 0); mSlider.setLayoutParams(lp); //mSlider.setMax(mSize-1); mSlider.setThumbOffset(30); //mSlider.setProgressDrawable(mContext.getResources().getDrawable(R.drawable.slider_track)); //mSlider.setThumb(mContext.getResources().getDrawable(R.drawable.slider_thumb)); mSlider.setOnSeekBarChangeListener(this); //addView(mSlider); fl.addView(mSlider); addView(fl); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Rect rectf = new Rect(); mSlider.getLocalVisibleRect(rectf); Log.d("WIDTH :",String.valueOf(rectf.width())); Log.d("HEIGHT :",String.valueOf(rectf.height())); Log.d("left :",String.valueOf(rectf.left)); Log.d("right :",String.valueOf(rectf.right)); Log.d("top :",String.valueOf(rectf.top)); Log.d("bottom :",String.valueOf(rectf.bottom)); int sliderWidth = mSlider.getWidth(); int padding = sliderWidth / (mSize-1); for ( int i=0 ; i &lt; mSize ; i++ ) { TextView tv = mTextViews[i]; tv.setPadding(i* padding, 0, 0, 0); } } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStopTrackingTouch(SeekBar seekBar) { Log.d("SEEK", "value: " + seekBar.getProgress()); seekBar.setProgress(50); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload