GAMA
GAMA

Reputation: 5996

different values in onProgressChanged() and onStopTrackingTouch in SeekBar

I'm dynamically modifying height of the button based on seekbar progress.

Now I want to retreive height of the button. So I used btn1.getHeight() in onProgressChanged() method of SeekBar.
But it gives incorrect/old set of values for button height.

Instead, if I used btn1.getHeight() in onStopTrackingTouch(), I'm getting correct values.

This simply means , when I try to retreive height of the button in onProgressChanged() , UI changes are visible on screen but yet not registered wchich results into getting incorrect/old values.

How to get correct values in onProgressChanged() ?

Is there other way round to do this? Any help appreciated.

Edit

@Luksprog : I made following changes :

<SeekBar
    android:id="@+id/seekBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:maxHeight="9dp"
    android:minHeight="9dp"
    android:padding="10dp"
    android:max="100"
    android:progressDrawable="@drawable/seekbar_progress"
    android:thumb="@drawable/shine_btn" />

<RelativeLayout
    android:id="@+id/Graph01"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:layout_below="@id/seekBar"
    android:layout_marginTop="50dp"
    android:gravity="bottom" >

    <Button
        android:id="@+id/btnGraph01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"                        
        android:layout_alignParentBottom="true"
        android:background="#99f" />

    <com.example.calci.views.MyTextView
        android:id="@+id/tvGraph01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/btnGraph01" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/Graph02"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:layout_below="@id/seekBar"
android:layout_toRightOf="@id/Graph01"
    android:layout_marginTop="50dp"
    android:gravity="bottom" >

    <Button
        android:id="@+id/btnGraph02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"                        
        android:layout_alignParentBottom="true"
        android:background="#99f" />

    <com.example.calci.views.MyTextView
        android:id="@+id/tvGraph02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/btnGraph02" />
</RelativeLayout>

</RelativeLayout>

and in activity

RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(20,
            2 * progress * 1);
    lp2.setMargins(55, 0, 0, 0);
    btnGraph01.setLayoutParams(lp2);

RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(
            new ViewGroup.MarginLayoutParams(20,
                    RelativeLayout.LayoutParams.WRAP_CONTENT));
    lp3.setMargins(55, 0, 0, 30);
    tvGraph01.setLayoutParams(lp3);
    tvGraph01.setTextSize(6);
    tvGraph01.setGravity(Gravity.CENTER);

But TextView is getting displayed inside button... Why it is so?

PLEASE CORRECT ME IF I"M WRONG...

Upvotes: 0

Views: 1409

Answers (1)

user
user

Reputation: 87064

First I'm guessing this is related to your previous question. I wouldn't use weights, instead I would simply use a RelativeLayout with the Buttons attached to the bottom, this way the Buttons will increase in height upwards. Like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/parent" >

    <SeekBar
        android:id="@+id/sb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" 
        android:max="200"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="#99cc00"
        android:text="Button"
        android:onClick="test" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@id/button1"
        android:background="#0077cc"
        android:text="Button" />

</RelativeLayout>

Second, I don't see why you can't get the desired height. With my code above by using button.getHeight() you'll get the previous height store(as the new height will not be stored until you get out of onProgressChanged). But this shouldn't stop you from getting the new height because you are actually calculating that value yourself with :

progress * 10

Code:

@Override

    public void onProgressChanged(SeekBar seekBar, final int progress,
                        boolean fromUser) {
                    LayoutParams lp = findViewById(R.id.button1).getLayoutParams();
                    lp.width = 50;
                    lp.height = progress * 10;
                    findViewById(R.id.button1).setLayoutParams(lp);
                    Log.e("XZX", "Progress : " + progress + " |Old height : "
                            + findViewById(R.id.button1).getHeight()
                            + "  |New height : " + (progress * 10));
                }

Edit: Your layout file is wrong:

<RelativeLayout
    android:id="@+id/Graph01"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:layout_below="@id/seekBar"
    android:layout_marginTop="50dp" >

    <Button
        android:id="@+id/btnGraph01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"                        
        android:layout_alignParentBottom="true"
        android:background="#99f" />

    <com.example.calci.views.MyTextView
        android:id="@+id/tvGraph01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/btnGraph01" />
</RelativeLayout>

Now in code there is no need to modify the LayoutParams of tvGraph01, it will follow the Button as it grows/shrinks, also get the current LayoutParams of the Button and modify it instead of creating new ones again and again:

RelativeLayout.LayoutParams lp2 = (RelativeLayout.LayoutParams)btnGraph01.getLayoutParams();
lp2.width = 20;
lp2.height = 2 * progress * 1;
lp2.setMargins(55, 0, 0, 0); // <= do you really need the margin? what is its purpose?
btnGraph01.setLayoutParams(lp2);
tvGraph01.setTextSize(6);

Upvotes: 2

Related Questions