Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SeekBarChangeListener and TouchListener No Events

Tags:

android

input

I'm having an issue with button click events and seekbar events not firing. When I drag on my seekbar, the slider changes position visually but there's no event being fired in my OnSeekBarChangedListener. I also have an ImageButton that should receive click events but it does not fire either. The MyView at the bottom here handles touch events properly. It holds a GLSurfaceView that I can pan around and pinch-zoom on. What could be causing the events to not fire?

Here's my layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tec_root">

    <ImageButton
        android:id="@+id/button_star"
        android:contentDescription="@string/star_description"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginTop="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:src="@drawable/star_empty"
        android:background="#00000000"
        android:scaleType="fitXY"/>

    <LinearLayout
        android:id="@+id/view_plot"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/button_star"/>

    <SeekBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/button_star"
        android:layout_toStartOf="@id/button_star"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:progress="50"
        android:layout_alignBaseline="@id/button_star"
        android:id="@+id/seekbar" />

</RelativeLayout>

And here are the listeners:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.my_activity);

    prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    starred = prefs.getBoolean("starred", false);

    final ImageButton starButton = (ImageButton) findViewById(R.id.button_star);
    starButton.setImageDrawable(ContextCompat.getDrawable(this, (starred ? R.drawable.star_filled :
            R.drawable.star_empty)));

    starButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            starred = !starred;

            prefsEditor = prefs.edit();
            prefsEditor.putBoolean("starred", starred);
            prefsEditor.apply();

            starButton.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),
                    (starred ? R.drawable.star_filled : R.drawable.star_empty)));
        }
    });

    ((SeekBar) findViewById(R.id.seekbar)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            Log.e("TEST", "Seekbar onProgressChanged called");
            Toast.makeText(getApplicationContext(), "Seekbar fired", Toast.LENGTH_SHORT).show();
            // respond to update
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    MyView myView = new MyView(this, points, false);
    ((LinearLayout) findViewById(R.id.view_plot)).addView(myView);
}
like image 277
David M Avatar asked Jul 16 '15 15:07

David M


1 Answers

try this, replace LinearLayout with FrameLayout, it's not best at perfomance but works well

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tec_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <SeekBar
                android:id="@+id/seekbar"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:background="#ABABAB"
                android:layout_weight="1"
                android:progress="50" />

            <ImageButton
                android:id="@+id/button_star"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:background="#00000000"
                android:contentDescription="@string/star_description"
                android:scaleType="fitXY"
                android:src="@drawable/star_empty" />
        </LinearLayout>

        <FrameLayout
            android:id="@+id/view_plot"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"      
            />
 </LinearLayout>
like image 139
Linh Nguyen Avatar answered Nov 18 '22 12:11

Linh Nguyen