Android SeekBar (slider)


#21

@kfrajer I’ve tried your proposals, but without success .
As a workarround I set the views invisible in the setup() with a runable and add a full screen white colored view, adding it as the last in the fl.addView()'s (The views overlap each other in the order you write them in the code).
This way you see a white screen for half a second before your sketch is displayed.
It’s doable but I think there must exist a better way.


#22

@noel

Modifying my latest code in this post (you need those two extra xml files in the res/values/ folder), I was able to toggle visibility of seekbar and two (of three) buttons and I didn’t have any issue, or at least, not as you described. Notice I used a global boolean field to keep track of the button’s visibility field as I wasn’t sure if View.GONE would make me have to redefine my button. So it looks like it does not destroy the view, it just removes it from the layout. INVISIBLE hides it but it doesn’t remove it according to the docs.

Kf

//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;

boolean currVisibility=true;

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_180704c.R.string.string_button_text_change);  //https://developer.android.com/guide/topics/resources/string-resource#String
  btTvId=processing.test.sketch_180704c.R.id.button_text_change; //CompatUtils.getUniqueViewId();      //com.example.sketch_180704c.
  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);
          Button textSwitchButton = (Button)act.findViewById(btTvId);
          Button toastButton = (Button)act.findViewById(btToastId);

          //Elliminates extra else's 
          println("State bar: \t\t"+ (bar==null ? "null" : "OK") );
          println("State textSwitchButton:\t"+ (textSwitchButton==null ? "null" : "OK") );
          println("State toastButton: \t"+ (toastButton==null ? "null" : "OK") );

          if (bar!=null) {
            bar.setVisibility(bar.getVisibility()==View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
          } 
          
          if (textSwitchButton!=null)textSwitchButton.setVisibility(currVisibility ? View.INVISIBLE : View.VISIBLE);   
          if (toastButton!=null) toastButton.setVisibility(currVisibility ? View.GONE : View.VISIBLE);         
          currVisibility =! currVisibility;  //Toggle
        }
      }
      );
    }
  }
  );

  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.setVisibility(View.INVISIBLE);
  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);
}