Saturday, 17 July 2021

Android: Width of ImageView not changing from SeekBar listener

I am creating a simple Android app, in which the user can drag a SeekBar to the left to reveal less of an image, and right to reveal more of an image. This is accomplished through attaching a listener to the SeekBar, in which the width of the ImageView is changed proportionally to the SeekBar's progress. The problem is that no matter what position the slider is in, the width of the ImageView remains the same, as shown in the screenshots below:

Progress = 0%:

enter image description here

Progress = 100%

enter image description here

This seems strange, considering that in the SeekBar's listener, when I output the value I am setting the ImageView width to, that value is correct.

Below is my code:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:gravity="center"
    android:orientation="vertical">
    <SeekBar
        android:id="@+id/simpleSeekBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />


    <LinearLayout
        android:layout_height="300px"
        android:layout_width="300px"
        android:orientation="vertical"
        android:background="#0000FF">

        <ImageView
            android:layout_height="300px"
            android:layout_width="300px"
            android:src="@drawable/android"
            android:scaleType="matrix"
            android:id="@+id/iv"/>

    </LinearLayout>
</LinearLayout>

MainActivity.java:

package com.example.changingimageviewwidth2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView iv = findViewById(R.id.iv);

        SeekBar seekbar = findViewById(R.id.simpleSeekBar);
        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                iv.getLayoutParams().width = 300 * progress/100;
                System.out.println(300 * progress/100);
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                //write custom code to on start progress
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }
}

Note: The image is NOT supposed to be resized when the slider is moved.



from Android: Width of ImageView not changing from SeekBar listener

No comments:

Post a Comment