How to make textviews visible depending on progress of seekbar

Tags: , , , ,



I am trying to make 4 different textviews visible depending on the progress of my seekbar (one visible at a time). I set the textviews to invisible and the seekbar max to 100. It works fine for the values below 24, but the app crashes as soon as the seekbar exceeds 25.

I am an absolute beginner programmer and trying to teach myself, I thought about using a while loop instead, but I can´t make it work either.

I would greatly appreciate any help.

Code:

public class MainActivity extends AppCompatActivity {

SeekBar seekBar;
TextView textView;
TextView textView2;
TextView textView3;
TextView textView4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    seekBar = findViewById(R.id.seekBar);
    seekBar.setMax(100);
    textView = (TextView)findViewById(R.id.textView);
    button = (Button)findViewById(R.id.button);

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                if (seekBar.getProgress() < 24) {
                    textView.setVisibility(View.VISIBLE);
                } else if (seekBar.getProgress() >= 25 && seekBar.getProgress() < 49) {
                    textView2.setVisibility(View.VISIBLE);
                } else if (seekBar.getProgress() >= 50 && seekBar.getProgress() < 74) {
                    textView3.setVisibility(View.VISIBLE);
                } else if (seekBar.getProgress() >= 75) {
                    textView4.setVisibility(View.VISIBLE);
                }

activity_main:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Answer

You have used findViewById to link a resource to your textView object. You need to do the same for textView2, textView3 and textView4. I presume you are getting a NullPointerException when you try to call a method on textView2.



Source: stackoverflow