# Android SeekBar (slider)

**URL:** <https://discourse.processing.org/t/android-seekbar-slider/1245>\
**Category:** Processing for Android\
**Created:** [June 25, 2018, 1:12pm UTC](https://discourse.processing.org/t/android-seekbar-slider/1245 "2018-06-25T13:12:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [June 25, 2018, 1:12pm UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/1 "2018-06-25T13:12:44Z")

</div>

Hi.  
I am trying to implement a java SeekBar programmatically in my sketch but without any success.  
Untill now I have the code below, but giving a lot of errors starting with “The constructor SeekBar(seekbar) is undefined”.  
I really need some help here.  
Thanks in advance.

```auto
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.app.Activity;
import android.content.Context;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.os.Looper;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.os.Bundle;

Activity act;
Context mC;
FrameLayout fl;
SeekBar seekBar;

@ Override 
  public void onStart() {
  super.onStart();
  act = this.getActivity();
  mC= act.getApplicationContext();
  act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
  SeekBar seekBar = new SeekBar(mC);
  Seekbar.setLayoutParams (
    new RelativeLayout.LayoutParams (
    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT
    )
    );
  fl = (FrameLayout)act.findViewById(R.id.content);
  FrameLayout.LayoutParams params1 = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);  
  fl.addView(seekBar);
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
      prinln(progress);
      }
   });
  }

```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 25, 2018, 6:40pm UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/2 "2018-06-25T18:40:45Z")

</div>

The error message talks about something wrong with your constructor. Then, your first stop should be the [Android API docs](https://developer.android.com/reference/android/widget/SeekBar).

I have written an example where changing the slider adjust the alpha transparency of a rectangle showing a linear gradient.

Kf

```java
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.app.Activity;
import android.content.Context;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.os.Looper;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.os.Bundle;

import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.R;

Activity act;
Context mC;
FrameLayout fl;
SeekBar seekBar;

final color BGCOLOR=color(10, 10, 250);
final int MAX_ALPHA=255;

color beginLGcolor=color(255, 0, 0,255);
color endLGcolor=color(255, 0, 0,0);

void settings() {

  size(displayWidth, displayHeight>>1,P2D);
}

void draw() {
  background(BGCOLOR);
  rectLinearGradient(width*0.25, height*0.25, width*0.75, height*0.75, beginLGcolor, endLGcolor);
}

void rectLinearGradient(float x0, float y0, float x1, float y1, color c1, color c2) {

  if (x0>x1) {
    println("Nothing done as x0, x1 are not valid");
    return;
  }

  if (y0>y1) {
    println("Nothing done as y0, y1 are not valid");
    return;
  }

  pushStyle();
  strokeWeight(1);
  float deltaRow=y1-y0;
  for (float row=y0; row<y1; row++) {
    stroke(lerpColor(c1, c2, (row-y0)/deltaRow));
    line(x0, row, x1, row);
  }
  popStyle();
}

@ Override 
  public void onStart() {

  super.onStart();

  act = this.getActivity();
  mC= act.getApplicationContext();

  act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

  SeekBar seekBar = new SeekBar(mC);

  seekBar.setLayoutParams (
    new RelativeLayout.LayoutParams (
    RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT
    )
    );
  seekBar.setPadding(100, 100, 200, 0);
  fl = (FrameLayout)act.findViewById(R.id.content);
  fl.addView(seekBar);

  seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

    @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
      ;
    }

    @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
    }

    @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
      println(progress,seekBar.getMax());
      float aFactor=progress/float(seekBar.getMax())*MAX_ALPHA;
      endLGcolor=color(255,0,0,aFactor);
    }
  }
  );
}

```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 25, 2018, 8:13pm UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/3 "2018-06-25T20:13:56Z")

</div>

This next is the same sketch as above but it uses **xml** to define the SeekBar object. Notice for this you need to follow some instructions at the end of this post. First, I will present the two files: the pde and the xml.

## PDE file

```java
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.app.Activity;
import android.content.Context;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.os.Looper;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.os.Bundle;

import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.R;
import android.view.LayoutInflater;
import android.view.View;

Activity act;
Context mC;
FrameLayout fl;

final color BGCOLOR=color(10, 10, 250);
final int MAX_ALPHA=255;

color beginLGcolor=color(255, 0, 0, 255);
color endLGcolor=color(255, 0, 0, 0);

void settings() {
  size(displayWidth, displayHeight>>1, P2D);
}

void draw() {
  background(BGCOLOR);
  rectLinearGradient(width*0.25, height*0.25, width*0.75, height*0.75, beginLGcolor, endLGcolor);
}

void rectLinearGradient(float x0, float y0, float x1, float y1, color c1, color c2) {

  if (x0>x1) {
    println("Nothing done as x0, x1 are not valid");
    return;
  }

  if (y0>y1) {
    println("Nothing done as y0, y1 are not valid");
    return;
  }

  pushStyle();
  strokeWeight(1);
  float deltaRow=y1-y0;
  for (float row=y0; row<y1; row++) {
    stroke(lerpColor(c1, c2, (row-y0)/deltaRow));
    line(x0, row, x1, row);
  }
  popStyle();
}

@ Override 
  public void onStart() {

  super.onStart();

  act = this.getActivity();
  mC= act.getApplicationContext();

  act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

  LayoutInflater inflater = getActivity().getLayoutInflater();
  final View inflator = inflater.inflate(processing.test.sketch_180625c.R.layout.seekbar_layout, null); //processing.test.inflatordemo.
  SeekBar seekBar = (SeekBar)inflator.findViewById(processing.test.sketch_180625c.R.id.seekBar);

  seekBar.setPadding(100, 100, 200, 0);
  fl = (FrameLayout)act.findViewById(R.id.content);
  fl.addView(seekBar);

  seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

    @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
      ;
    }

    @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
    }

    @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
      println(progress, seekBar.getMax());
      float aFactor=progress/float(seekBar.getMax())*MAX_ALPHA;
      endLGcolor=color(255, 0, 0, aFactor);
      //Toast.makeText(mC, "Progress "+progress, Toast.LENGTH_SHORT).show();
    }
  }
  );
}

```

## XML file

```xml
<SeekBar xmlns:android="http://schemas.android.com/apk/res/android"
	  android:id="@+id/seekBar"  
	  android:layout_width="match_parent"  
	  android:layout_height="wrap_content"
	  android:layout_marginStart="8dp" 
	  android:layout_marginEnd="8dp"           
	  android:layout_marginTop="372dp" />

```

## REMARKS:

1. Notice that the name of the sketch is important. In this case, the sketch’s name is _sketch\_180625c_. I am running without saving this test, but for any case, you need to use the name of the actual sketch, or the temp name given by Processing. For the inflator to work, you need to use the package name of your app. In testing mode, Processing Android assigns the package name for you using the following schema: `processing.test.sketch_180625c`. Notice the sketch name is used here in the last part of this notation. So keep this in mind when modifying or inflating any component defined by an xml file.
2. The `xml` needs to be placed in the following folder: **res/layout**. Notice this folder is **not** nested inside the data folder. The resultant layout of the project is the following:

```auto
C:.
│ AndroidManifest.xml
│ AppLifeCycle.pde
│ sketch.properties
│ sketch_180625c.pde
│
├───code
│ sketch.properties
│
└───res
    └───layout
            seekbar_layout.xml

```

Kf

 ![seekbar](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/1X/aaa44d800ab298a15f28d288fdbb98fd0312d130.gif)

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [June 26, 2018, 5:30pm UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/4 "2018-06-26T17:30:09Z")

</div>

Thank you so much! The first code is working perfectly, and I saw where I went wrong.  
Your second code made me jump, this actually works, and will be awesome.  
I have an issue however because the button of the slider alone is on the top, and the slider in the center.  
I tried to fix it by first adding a relative layout, like you did in your first code, but still without success after a lot of hours. Would you be so kind to post an example, so that on this relative other objects can be added?

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 26, 2018, 6:07pm UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/5 "2018-06-26T18:07:18Z")

</div>

Are you suing the first or the second code? You should create a linear layout (One of the options anyways) and lay your components there. You can try checking for `LinerLayout` in the old forum and work from those examples. If you are stuck, post your code.

Kf

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [June 26, 2018, 7:59pm UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/6 "2018-06-26T19:59:18Z")

</div>

@kfrajer OK, I will work on this, but in the meantime, just to complete my project I will use the first code. My problem now is to dismiss the slider, but using

> seekBar.setVisibility(View.GONE);

is not working. Shouldn’t it?

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 26, 2018, 8:23pm UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/7 "2018-06-26T20:23:52Z")

</div>

Check this: [https://forum.processing.org/two/discussion/26349/android-input-box](https://forum.processing.org/two/discussion/26349/android-input-box)

Kf

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [June 26, 2018, 9:31pm UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/8 "2018-06-26T21:31:28Z")

</div>

@kfrajer I added the below to the code, bu it gives me the error

> seekBar.setVisibility(View.GONE);  
> View cannot be resolved

Code:

```auto
void mousePressed() {
  if (mouseY > height/2) {
    act.runOnUiThread(
      new Runnable() {
      public void run() {
        seekBar.setVisibility(View.GONE);
      }
    }
    );
  }
}

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [June 27, 2018, 11:47am UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/9 "2018-06-27T11:47:13Z")

</div>

Changing

> seekBar.setVisibility(View.GONE);

with

> fl.removeView(seekBar);

in the mouusepressed() function above does not give any error but also does not remove the view.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 27, 2018, 12:09pm UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/10 "2018-06-27T12:09:58Z")

</div>

> [@noel](#):
>
> View cannot be resolved

You need to add `import android.view.View;`

Kf

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [June 27, 2018, 2:20pm UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/11 "2018-06-27T14:20:18Z")

</div>

Thanks, of course 😵

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [June 27, 2018, 5:04pm UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/12 "2018-06-27T17:04:13Z")

</div>

@kfrajer Still in trouble, using seekBar.setVisibility(View.GONE); will compile, but crash as soon as tapped. Using fl.setVisibility(View.GONE); in the runable will remove the FrameLayout, and a blank screen appears. The draw() function does not function anymore.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [June 28, 2018, 12:47am UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/13 "2018-06-28T00:47:36Z")

</div>

@kfrajer  
seekBar.setVisibility(View.GONE); will work properly whenever and wherever you put it in the code between initialzation of the SeekBar and the end of the onStart.  
However using it in the mousePressed function will lett the program crash. Any idea where to start to solve this problem??

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 28, 2018, 2:50am UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/14 "2018-06-28T02:50:32Z")

</div>

> [@noel](#):
>
> However using it in the mousePressed function will lett the program crash

Is `seekBar` defined in the global scope?

Here is an example using buttons, textView, seekBar, Color, resources (ids and strings) and LinerLayout.

Need to consider that for this test the name of my temporal sketch was **sketch\_180627b**. This is important as you will need to change this to the name of your sketch in the code below. This will allow you to access resources defined as XML. Folder structure and files shown below.

Kf

```auto
C:\Users\C\AppData\Local\Temp\untitled3773019319882703841sketches>tree /f
Folder PATH listing for volume Windows8_OS
Volume serial number is 4E33-E988
C:.
└───sketch_180627b
    │ AndroidManifest.xml
    │ AppLifeCycle.pde
    │ sketch.properties
    │ sketch_180627b.pde
    │
    ├───code
    │ sketch.properties
    │
    └───res
        └───values
                ids.xml
                strings.xml

```

## FILE: ids.xml

```xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="id" name="button_text_change" />
</resources>

```

## FILE: strings.xml

```xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="string_button_text_change">Random Msg!</string>
</resources>

```

## FILE: sketch\_180627b.pde

```java
//REFERENCE: https://discourse.processing.org/t/android-seekbar-slider/1245/2
//REFERENCE: https://forum.processing.org/two/discussion/26349/android-input-box
//REFERENCE: https://forum.processing.org/two/discussion/26515/scrollable-image-with-scrollview

import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.app.Activity;
import android.content.Context;
import android.widget.FrameLayout;
import android.widget.Toast;
import android.os.Looper;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.os.Bundle;

import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.R;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.LinearLayout;
import android.graphics.Color;

import processing.android.CompatUtils;

final String MSGS[]={"Today is fun", "Yesterday is sunny", "Tomorrow is a new day", "See you soon", "Hey, what's up?"};
final int UNKNOWN_ID=-1;
final color BGCOLOR=color(10, 10, 250);
final int MAX_ALPHA=256;
final int MAX_RED=256;

Activity act;
Context mC;
FrameLayout fl;
//SeekBar seekBar;

int tvId=UNKNOWN_ID;
int btTvId=UNKNOWN_ID;
int btToastId=UNKNOWN_ID;
int btSliderId=UNKNOWN_ID;
int sliderId=UNKNOWN_ID;

color beginLGcolor=color(255, 0, 0, 255);
color endLGcolor=color(255, 0, 0, 0);
float cProgressValueNorm=0;

void settings() {

  size(displayWidth, displayHeight>>1, P2D);
}

void draw() {
  background(BGCOLOR);
  rectLinearGradient(width*0.25, height*0.25, width*0.75, height*0.75, beginLGcolor, endLGcolor);
}

void rectLinearGradient(float x0, float y0, float x1, float y1, color c1, color c2) {

  if (x0>x1) {
    println("Nothing done as x0, x1 are not valid");
    return;
  }

  if (y0>y1) {
    println("Nothing done as y0, y1 are not valid");
    return;
  }

  pushStyle();
  strokeWeight(1);
  float deltaRow=y1-y0;
  for (float row=y0; row<y1; row++) {
    stroke(lerpColor(c1, c2, (row-y0)/deltaRow));
    line(x0, row, x1, row);
  }
  popStyle();
}

void mouseReleased() {
  println(tvId, btTvId, btToastId, btSliderId, sliderId);
}

@ Override 
  public void onStart() {

  super.onStart();

  act = this.getActivity();
  mC= act.getApplicationContext();
  // OOOOOOOOOOOOOOOO---------------------------OOOOOOOOOOOOOOOO

  // OOOOOOOOOOOOOOOO---------------------------OOOOOOOOOOOOOOOO

  act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

  TextView tv = new TextView(this.getActivity());
  tv.setTextSize(24);
  tv.setTextColor(Color.rgb(20, 0, 0));
  tv.setText ("No message yet!");  
  tv.setGravity(Gravity.RIGHT);
  tvId=CompatUtils.getUniqueViewId();
  tv.setId(tvId);

  Button textSwitchButton= new Button(act);
  textSwitchButton.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 
    RelativeLayout.LayoutParams.WRAP_CONTENT));
  textSwitchButton.setTextSize(10);
  textSwitchButton.setText (processing.test.sketch_180627b.R.string.string_button_text_change); //https://developer.android.com/guide/topics/resources/string-resource#String
  btTvId=processing.test.sketch_180627b.R.id.button_text_change; //CompatUtils.getUniqueViewId(); //com.example.sketch_180627b.
  textSwitchButton.setId(btTvId);
  textSwitchButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View V) {

      //Code here
      act.runOnUiThread(
        new Runnable() {
        public void run() {

          TextView tvx = (TextView)act.findViewById(tvId);   
          if (tvx!=null) {
            String msg=MSGS[int(random(MSGS.length))];
            tvx.setText(msg);
          } else {
            println("TV view was not found!");
          }
        }
      }
      );
    }
  }
  );

  Button toastButton= new Button(act);
  //https://stackoverflow.com/questions/5191099/how-to-set-relativelayout-layout-params-in-code-not-in-xml
  RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
    RelativeLayout.LayoutParams.WRAP_CONTENT); 
  toastButton.setLayoutParams(params);
  //toastButton.setGravity(Gravity.CENTER_HORIZONTAL);
  toastButton.setTextSize(10);
  toastButton.setText ("Toast!");
  btToastId=CompatUtils.getUniqueViewId();
  toastButton.setId(btToastId);
  toastButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View V) {

      //Code here
      Toast.makeText(mC, "Hello World!", Toast.LENGTH_SHORT).show();
    }
  }
  );

  Button sliderControlButton= new Button(act);
  sliderControlButton.setTextSize(10);
  sliderControlButton.setTextColor(Color.rgb(0, 0, 0));
  sliderControlButton.setText ("Toggle Slider");
  //sliderControlButton.setX(width*0.60);
  //sliderControlButton.setY(200);
  btSliderId=CompatUtils.getUniqueViewId();
  sliderControlButton.setId(btSliderId);
  sliderControlButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View V) {

      //Code here
      act.runOnUiThread(
        new Runnable() {
        public void run() {
          SeekBar bar = (SeekBar)act.findViewById(sliderId);
          if (bar!=null) {

            if (bar.getVisibility()!=View.VISIBLE)bar.setVisibility(View.VISIBLE);            
            else bar.setVisibility(View.INVISIBLE);
          } else {
            println("Slider view was not found!");
          }
        }
      }
      );
    }
  }
  );

  SeekBar seekBar = new SeekBar(mC);
  //seekBar.setLayoutParams (
  // new RelativeLayout.LayoutParams (
  // RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT
  // )
  // );  
  seekBar.setPadding(100, 100, 200, 0);
  sliderId=CompatUtils.getUniqueViewId();
  seekBar.setId(sliderId);
  seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
      ;
    }
    @Override
      public void onStopTrackingTouch(SeekBar seekBar) {

      //Value compute on onProgressChanged()
      act.runOnUiThread(
        new Runnable() {
        public void run() {

          Button but = (Button)act.findViewById(btSliderId);   
          if (but!=null) {
            but.setTextColor(Color.rgb(cProgressValueNorm*MAX_RED, 0, 0));
          }
        }
      }
      );
    }
    @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
      println(progress, seekBar.getMax());
      cProgressValueNorm = progress/float(seekBar.getMax());
      float aFactor=cProgressValueNorm * MAX_ALPHA;
      endLGcolor=color(255, 0, 0, aFactor);
    }
  }
  );

  LinearLayout layoutContainer=new LinearLayout(act);
  layoutContainer.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
  layoutContainer.setOrientation(LinearLayout.VERTICAL);  
  layoutContainer.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
  layoutContainer.addView(tv);
  layoutContainer.addView(textSwitchButton);
  layoutContainer.addView(toastButton, params);
  layoutContainer.addView(sliderControlButton);
  layoutContainer.addView(seekBar);

  fl = (FrameLayout)act.findViewById(R.id.content);
  fl.addView(layoutContainer);
}

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [June 28, 2018, 9:59am UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/15 "2018-06-28T09:59:13Z")

</div>

Awesome !! This clarifies a whole lot. It is a good thing that you only can give a like once, otherwise I would hurt my finger 😃.  
The only minor change I had to make was making the color’s red component an integer like : but.setTextColor(Color.rgb(int(cProgressValueNorm\*MAX\_RED), 0, 0));  
Below is a code for an vertical SeekBar. I found a trick by playing around. There sure will a proper way but this what I found out. When you rotate the seekBar 90° It will turn around it’s right upper corner and will become invisible. However when you do 1 pixel padding on the FrameLayout and paint it’s background, the SeekBar becomes visible again. Just don’t ask me why 😁.  
Once again thank you very much for all the help.

```auto
import android.view.ViewGroup; 
import android.widget.SeekBar; 
import android.app.Activity; 
import android.content.Context; 
import android.widget.RelativeLayout;
import android.widget.FrameLayout;
import android.view.WindowManager; 
import android.view.inputmethod.InputMethodManager; 
import android.view.View;
import android.view.ViewGroup.LayoutParams; 
import android.R;

Activity act; 
Context mC; 
FrameLayout fl; 
SeekBar seekBar;
int pgr;
int seekBarWidth = 500;

void settings() { 
  size(displayWidth, displayHeight);
}

void setup() {
  textSize(32);
}

void draw() { 
  background(50, 37, 115);
  text(pgr, width/2, height/2);
}

@ Override public void onStart() { 
  super.onStart(); 
  act = this.getActivity(); 
  mC= act.getApplicationContext(); 
  act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 

  SeekBar seekBar = new SeekBar(mC); 
  seekBar.setLayoutParams ( new RelativeLayout.LayoutParams ( seekBarWidth, LayoutParams.WRAP_CONTENT ) ); 
  seekBar.setX(width/2);
  seekBar.setY(height/2);
  seekBar.setRotation(90);
  seekBar.setMax(250);
  seekBar.setProgress(50);
  // seekBar.setBackgroundColor(Color.RED);  

  fl = (FrameLayout)act.findViewById(R.id.content); 
  fl.addView(seekBar); 
  fl.setBackgroundColor(#321173 ); // YOUR BACKGROUND COLOR
  fl.setPadding(1, 1, 1, 1);

  seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
    @Override 
      public void onStartTrackingTouch(SeekBar seekBar) {
    }
    @Override 
      public void onStopTrackingTouch(SeekBar seekBar) {
    } 
    @Override 
      public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) { 
      pgr = progress;
    }
  } 
  );
}

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [June 30, 2018, 10:54am UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/16 "2018-06-30T10:54:48Z")

</div>

@kfrajer In your code you setted the button text color in the onStopTrackingTouch() function.  
I want to pass the progress value to the TextView by the code below, but the seekBarValue always is null. Where am I wrong? (no errors in console).

```auto
seekBar.setOnSeekBarChangeListener(
    new SeekBar.OnSeekBarChangeListener() { 
    @Override 
      public void onStartTrackingTouch(SeekBar seekBar) {
    }
    @Override 
      public void onStopTrackingTouch(SeekBar seekBar) {
    }
    @Override 
      public void onProgressChanged (SeekBar seekBar, final int progress, boolean fromUser) {
      act.runOnUiThread( new Runnable() { 
        public void run() { 
          TextView seekBarValue = (TextView)act.findViewById(tvId);
          // Button but = (Button)act.findViewById(btSliderId); 
          println(seekBarValue)
          if (seekBarValue != null) { 
          // but.setTextColor(Color.rgb(cProgressValueNorm*MAX_RED, 0, 0));
            seekBarValue.setText(String.valueOf(progress));
          }
        }
      }
      );
    }
  }
  );

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [July 1, 2018, 8:29am UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/17 "2018-07-01T08:29:23Z")

</div>

Solved! I forgot to se the Layout params.😈

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [July 2, 2018, 3:21pm UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/18 "2018-07-02T15:21:26Z")

</div>

@kfrajer In your template code I want to start with the seekBar unvisable so i added the line

> seebBar.setVisibility(View.INVISIBLE);

below the line

> seekBar.setId(sliderId);

And this really starts the sketch with the slider ‘off screen’. But then the toggle button will not toggle anymore!  
I want to start my sketch empty and, with a mouseclick, let the slider showing up. I,ve also tried

> SeekBar bar = (SeekBar)act.findViewById(sliderId);  
> if (bar.getVisibility()!=View.VISIBLE)  
> bar.setVisibility(View.VISIBLE);

without success and all kind of other things but nothing worked out.  
How to do this?

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [July 2, 2018, 6:01pm UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/19 "2018-07-02T18:01:49Z")

</div>

I found a solution by not setting

> seebBar.setVisibility(View.INVISIBLE);

in the onStart(), but setting

```auto
 act.runOnUiThread( new Runnable() { 
      public void run() { 
        SeekBar bar = (SeekBar)act.findViewById(sliderId);
        bar.setVisibility(View.INVISIBLE);
      }
    }
    );

```

in the setup()  
The problem is however, when running the program the bar will show up for a second before it becomes invisible.  
I really would like to know how to this properly ( the correct way).

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [July 2, 2018, 8:35pm UTC](https://discourse.processing.org/t/android-seekbar-slider/1245/20 "2018-07-02T20:35:09Z")

</div>

I would need to setup the Android project to test it. I can’t atm. I can suggest you try to call this `onResume()`. are you adding this dynamically in `onCreate()`? Could you set the visibility state there? [I read your post above] try setting the visibility after you added to the view in `onCreate()`. I am not sure if this would work but it is worth to try. Lastly, You should check the life cycle of a fragment as it provides other points in your fragment life cycle where you could update the view and its elements. However, I think my first proposed method should work aka. performing this update in the `onResume()` call.

Kf

[Next page](https://discourse.processing.org/t/android-seekbar-slider/1245.md?page=2)
