-1

I am building and android application where I am using seekbar for maximum and minimum value.

I had used an example from this link - http://www.kpbird.com/2011/05/android-seek-bar-with-two-thumb.html

But when I use it in fragment Activity I am unable to get seekbar value. Here is an code that I edit as need -

package tabsswipe;

public class FragmentPlay extends Fragment {

String selItem,selItem2,selItem3,selItem4;
TextView tv1,tv2;
SeekBarWithTwoThumb swtt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_play, container, false);

     getActivity().getActionBar().setBackgroundDrawable(
                new ColorDrawable(Color.parseColor("#0077d1")));
        ActionBar mActionBar = getActivity().getActionBar();
        getActivity().getActionBar().setIcon(
           new ColorDrawable(getResources().getColor(android.R.color.transparent)));    
        mActionBar.setDisplayShowHomeEnabled(true);
        mActionBar.setDisplayShowTitleEnabled(false);
        LayoutInflater mInflater = LayoutInflater.from(getActivity());
        View mCustomView = mInflater.inflate(R.layout.custom_actionbar2, null);
        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);

        ImageButton btn = (ImageButton) getActivity().findViewById(R.id.button);
        btnt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                viewCategory();

            }
        });


    return rootView;
    }

    private void viewCategory() {

        AlertDialog.Builder viewDialog = new AlertDialog.Builder(getActivity());

        viewDialog.setTitle("selecte");

        LayoutInflater li = (LayoutInflater) 
   getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View dialogView = li.inflate(R.layout.customealertdialogbox, null);
        viewDialog.setView(dialogView);

        viewDialog.setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        new DownloadJSON().execute();   
                    }
                });

        viewDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    }
                });

        swtt = (SeekBarWithTwoThumb) dialogView.findViewById(R.id.myseekbar);
        swtt.setSeekBarChangeListener(getActivity());

        //Button btn = (Button) dialogView.findViewById(R.id.button1);
        //btn.setOnClickListener(new OnClickListener() {

            //@Override
            //public void onClick(View v) {
                // TODO Auto-generated method stub
                //Toast.makeText(getActivity(), "this is my Toast message!!! =)",
                        //   Toast.LENGTH_LONG).show();
            //}
    //  });
        viewDialog.show();




    }
    public class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH); 
    Toast.makeText(getActivity(), year,
               Toast.LENGTH_LONG).show();
    // Create a new instance of DatePickerDialog and return it

    return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
    Calendar c = Calendar.getInstance();
    c.set(year, month, day);

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(c.getTime());
    Toast.makeText(getActivity(), year,
               Toast.LENGTH_LONG).show();
    }

}

}

Here is seek bar library -

package libseekbar;

public class SeekBarWithTwoThumb extends ImageView {

private String TAG = this.getClass().getSimpleName();
private Bitmap thumb = BitmapFactory.decodeResource(getResources(),R.drawable.leftthumb);
private int thumb1X, thumb2X;
private int thumb1Value, thumb2Value;
private int thumbY;
private Paint paint = new Paint();
private int selectedThumb;
private int thumbHalfWidth;
private SeekBarChangeListener scl;


public SeekBarWithTwoThumb(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public SeekBarWithTwoThumb(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SeekBarWithTwoThumb(Context context) {
    super(context);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if (getHeight() > 0)
        init();
}

private void init() {
    printLog("View Height =" + getHeight() + "\t\t Thumb Height :"+ thumb.getHeight());
    if (thumb.getHeight() > getHeight())
        getLayoutParams().height = thumb.getHeight();

    thumbY = (getHeight() / 2) - (thumb.getHeight() / 2);
    printLog("View Height =" + getHeight() + "\t\t Thumb Height :"+ thumb.getHeight() + "\t\t" + thumbY);

    thumbHalfWidth = thumb.getWidth()/2;
    thumb1X = thumbHalfWidth;
    thumb2X = getWidth()/2 ;
    invalidate();
}
public void setSeekBarChangeListener(SeekBarChangeListener scl){
    this.scl = scl;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(thumb, thumb1X - thumbHalfWidth, thumbY,paint);
    canvas.drawBitmap(thumb, thumb2X - thumbHalfWidth, thumbY,paint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int mx = (int) event.getX();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        if (mx >= thumb1X - thumbHalfWidth
                && mx <= thumb1X + thumbHalfWidth) {
            selectedThumb = 1;
            printLog("Select Thumb 1");
        } else if (mx >= thumb2X - thumbHalfWidth
                && mx <= thumb2X + thumbHalfWidth) {
            selectedThumb = 2;
            printLog("Select Thumb 2");
        }
        break;
    case MotionEvent.ACTION_MOVE:
        printLog("Mouse Move : " + selectedThumb);

        if (selectedThumb == 1) {
            thumb1X = mx;
            printLog("Move Thumb 1");
        } else if (selectedThumb == 2) {
            thumb2X = mx;
            printLog("Move Thumb 2");
        }
        break;
    case MotionEvent.ACTION_UP:
        selectedThumb = 0;
        break;
    }

    if(thumb1X < 0)
        thumb1X = 0;

    if(thumb2X < 0)
        thumb2X = 0;

    if(thumb1X > getWidth() )
        thumb1X =getWidth() ;

    if(thumb2X > getWidth() )
        thumb2X =getWidth() ;

    invalidate();
    if(scl !=null){
        calculateThumbValue();
        scl.SeekBarValueChanged(thumb1Value,thumb2Value);
    }
    return true;
}

private void calculateThumbValue(){
    thumb1Value = (100*(thumb1X))/(getWidth());
    thumb2Value = (100*(thumb2X))/(getWidth());
}
private void printLog(String log){
    Log.i(TAG, log);
}

interface SeekBarChangeListener{
    void SeekBarValueChanged(int Thumb1Value,int Thumb2Value);
}

public void setSeekBarChangeListener(FragmentActivity activity, SeekBarChangeListener scl) {
    // TODO Auto-generated method stub
    this.scl = scl;
}   
}
Hitesh Matnani
  • 533
  • 2
  • 8
  • 26

2 Answers2

0

As you can see by reading the code of the library, it uses a callback method to notify you when the slider values have changed (SeekBarValueChanged(int, int)).

So what you need to do is implement the SeekBarChangeListener inside your fragment

public Class FragmentPlayo extends Fragment implements SeekBarChangeListener {

and then implement the method (callback) inside your Fragment to do whatever you want to do when the values change :

private void SeekBarValueChanged(int thumb1Value, int thumb2Value) {
//Your code goes here when the values have changed
}

Hope this helps!

Edit : You'll also have to add

swtt = (SeekBarWithTwoThumb) dialogView.findViewById(R.id.myseekbar);
swtt.setSeekBarChangeListener(this);

As it means "Hey, tell me when values change, I'll take care of them!"

NSimon
  • 5,212
  • 2
  • 22
  • 36
  • See my edit, you need to change swtt.setSeekBarChangeListener(getactivity()); to swtt.setSeekBarChangeListener(this) – NSimon Nov 10 '14 at 12:15
  • It was to emphasize on the change, you *of course* don't need the 2 stars before, as well as the 2 after ;) – NSimon Nov 10 '14 at 12:17
  • I am doing like this "swtt.setSeekBarChangeListener(getActivity());"but still this showing an error - The method setSeekBarChangeListener(SeekBarWithTwoThumb.SeekBarChangeListener) in the type SeekBarWithTwoThumb is not applicable for the arguments (FragmentActivity) – Hitesh Matnani Nov 10 '14 at 12:19
  • It's because you're still calling it with 'getActivity()' instead of 'this'. Replace `swtt.setSeekBarChangeListener(getActivity());` with `swtt.setSeekBarChangeListener(this);` – NSimon Nov 10 '14 at 12:24
  • Then my application is unfortunately app stopped error showing - 11-10 18:24:54.732: E/InputEventReceiver(21048): Exception dispatching input event. in logcat – Hitesh Matnani Nov 10 '14 at 12:55
  • Ok, then maybe you'll have to change your 'customealertdialogbox.xml' to include a fragment that you'll create, where you'll handle the seekBarChangeListener there. That way the alertDialog will display a customView coming from your newly created Fragment, and the slideValueChangeListener should work there – NSimon Nov 10 '14 at 13:15
  • No I am not using any fragment in customealertdialogbox.xml – Hitesh Matnani Nov 10 '14 at 13:17
  • Yeah but you should. You're inflating a layout for the alertDialog, with a SeekBarWithTwoThumb in there. But you may have to replace that, because that way you'll be able to handle the changes on the slider. It would be something like : alertDialog inflates 'customealertdialogbox.xml' 'customealertdialogbox.xml' is made of a fragment only, let's say 'CustomAlertDialogFragment' you create CustomAlertDialogFragment as a normal fragment, which you implement SeekBarChangeListener. That should do it. – NSimon Nov 10 '14 at 13:22
0

@Hitesh Use this code in your Dialog box.

    tv1.setText("Thumb 1 Value :" +Thumb1Value);
    tv2.setText("Thumb 2 Value :"+Thumb2Value) ;
Daljeet
  • 1,573
  • 2
  • 20
  • 40