0

Is it possible to use the value of the seek bar in android to add a view to the activity?

For example, if the user selects and scrolls to the value of 2 on the seek bar this will have loaded two smaller image views within the same activity

Any help is appreciated.

grrigore
  • 1,050
  • 1
  • 21
  • 39
Brummerly
  • 90
  • 1
  • 11
  • I think it is. What did you try? – grrigore Aug 27 '18 at 13:34
  • I havent tried anything as yet it was just a thought. I was thinking something along the lines of using the: public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser, specifically the progress value to possibly call an extra imageview. I am just unsure how to make sure the image views line up correctly and to actually get it working. – Brummerly Aug 27 '18 at 13:40
  • Check out [this](https://stackoverflow.com/a/15326403/7972851) answer on how to do that. I think you can use a GridView and set its number of rows or columns according to that. Also check out [this](https://stackoverflow.com/questions/33314550/android-create-grid-with-dynamic-size-in-gridlayout-or-gridview). – grrigore Aug 27 '18 at 13:42
  • 1
    @Brummerly create a layout with seek bar and a listview, create listview items with Image views, when user scrolls and chooses any value, insert some objects into adapter and bingo, you will get that much image views. – Tejashwi Kalp Taru Aug 27 '18 at 13:43

1 Answers1

3

I've been working on this question and this is my solution. It might not be perfect so please let me know if there is something to be improved.

This is the result.

1. Create a new project and download these sample images. Save the image files into the project's res/drawable/ directory.

2. Open the res/layout/activity_main.xml file and insert the following:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:max="10" />
    </LinearLayout>
</ScrollView>

3. Open MainActivity.java and insert the following code for the onCreate() method:

MainActivity.java

package com.grrigore.dynamicgridview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {

    private int numberOfCurrentImages = 0;
    private int[] images = {
            R.drawable.sample_0,
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3,
            R.drawable.sample_4,
            R.drawable.sample_5,
            R.drawable.sample_6,
            R.drawable.sample_7,
            R.drawable.sample_0,
            R.drawable.sample_1,
            R.drawable.sample_2
    };

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

        final LinearLayout linearLayout = findViewById(R.id.linearLayout);

        SeekBar seekBar = findViewById(R.id.seekBar);

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

                for (int i = numberOfCurrentImages; i < progress; i++) {
                    ImageView imageView = new ImageView(MainActivity.this);
                    imageView.setImageResource(images[numberOfCurrentImages]);
                    linearLayout.addView(imageView);
                    numberOfCurrentImages++;
                }

                if (progress == 0 && linearLayout.getChildCount() > 1) {
                    linearLayout.removeViews(1, numberOfCurrentImages);
                    numberOfCurrentImages = 0;
                }

                if (progress < numberOfCurrentImages) {
                    for (int i = numberOfCurrentImages; i > progress; i--) {
                        linearLayout.removeViewAt(i);
                    }
                    numberOfCurrentImages = progress;
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}

Thanks to:

grrigore
  • 1,050
  • 1
  • 21
  • 39