1

I have created a reusable custom component (a custom view with a canvas drawing a bitmap), it's a slider that can be used for anything (volume control/rating systems). The problem is I need to monitor it's progress, something like the seekbar onProgressChanged.

I have no clue I would implement this. I can't use the seekbar and try re use this, I need to use the custom component..

Would anyone have any idea on this?

My layout is as follows:

enter image description here so I need to monitor the movement of my sliding image and try to calculate some sort of progress from that or is there an easier way?

any input on this would be appreciated, it's my first effort at custom components

EDIT

Here is how my xml layout looks:

   <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id = "@+id/slider1"
        android:layout_alignParentTop="true">

        <com.cs.customSlider.slider.app.sliderView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/sliderView"
            sliderView:diagonalSlide="false"
            sliderView:translateAxisX="false"/>
        <ImageView
            android:id="@+id/slider_frame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/slider_frame"
            android:layout_centerHorizontal="true">
        </ImageView>

</RelativeLayout>
DJ-DOO
  • 4,545
  • 15
  • 58
  • 98

1 Answers1

2

Disclaimer: this probably won't work 100% as I intend, but this should get you started.

Your customView obviously extends View. So use getLeft() to get the position of your view. Use getParent() to get the ViewGroup that holds this view, which is probably a linear or relative layout and get it's width.

Now you have a rough idea of your view's position in the parent, the farther right, the more "progress." Subtract the width of your customView from the width of the parentView because we are measuring from theleft side, and obviously the left side of the customView can never touch the rightside of the parent view.

Hope that helps get you there...

    View view = ...;

    //position of my view;
    int pos = view.getLeft();

    //Number of pixels my view can slide across the screen.
    ViewGroup vg = (ViewGroup) view.getParent();
    int width = vg.getMeasuredWidth();

    //right most position my view can reach,
    int available_width = width - view.getMeasuredWidth();

    float progress = ((float) pos) / available_width;
NameSpace
  • 10,009
  • 3
  • 39
  • 40
  • thanks for your help..the problem being, when I implement the above code width is zero, available width is also zero...it does get the parent correctly...as does int pos... – DJ-DOO Mar 18 '14 at 15:16
  • Rather than call this from the activity I do this work in the custom view – DJ-DOO Mar 18 '14 at 15:39
  • see if get getMeasuredWidth() gives you a value, instead of getWidth() – NameSpace Mar 18 '14 at 15:40
  • the width of the viewgroup is also the same width of the view..so available width is then 0, pos is also 0 so 0/0..progress is NaN – DJ-DOO Mar 18 '14 at 16:13
  • sorry to keep adding comments, this is also the case if I use getHeight() if I'm scrolling up and down...pos = 0, viewgroup height = view height so progress = NaN – DJ-DOO Mar 18 '14 at 16:31
  • positions start at zero, they are relative to the parent view. Only u know the xml/view hierarchy, so you have to figure out if you're getting the correct ViewGroup (parent) to measure width of the screen. If position stays stuck at zero for your slider it may mean you have a parent tightly wrapping your childView, e.g. like a border. Sorry, I can only guess at the problems given the info. – NameSpace Mar 18 '14 at 16:58
  • one more thing, there's 2 coordinate systems, relative and absolute in android.. you could try using the absolute ones if relative gives you too much trouble [see here](http://stackoverflow.com/questions/2224844/how-to-get-the-absolute-coordinates-of-a-view) Just don't mix and match the two. – NameSpace Mar 18 '14 at 17:12
  • I have edited my question to reflect the part of the layout that I'm trying to get the measurements from...is this incorrect? – DJ-DOO Mar 18 '14 at 17:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49987/discussion-between-namespace-and-dj-doo) – NameSpace Mar 18 '14 at 22:10