Simply code in Andoid Mode

edited March 2018 in Android Mode

In this code bellow i tried to load different texts(defferent names) from a list after touching the "Details" button on my device..The problem is that it shows me the name only fore some seconds but i want to stay there invariably..

    import ketai.ui.*;
    import android.view.MotionEvent;

    Button on_button;  // the button
    int clk = 1;  

    KetaiGesture gesture;

    KetaiList klist;

    KetaiList selectionlist;
    ArrayList<String> textlist = new ArrayList<String>();

    void setup() {  
      size(1000,600);
      orientation(LANDSCAPE);
      textSize(28);
      textAlign(CENTER);
      gesture = new KetaiGesture(this);
       on_button = new Button("Details", 820, 420, 100, 50);
      textlist.add("Patient 1");
      textlist.add("Patient 2");
      textlist.add("Patient 3");
      textlist.add("Patient 4");
      textlist.add("Patient 15");
    }



    void mousePressed()
    {
      if (on_button.MouseIsOver()) {
        selectionlist = new KetaiList(this,textlist); 
      }
    }  
     void onKetaiListSelection(KetaiList klist)
    {
      String selection = klist.getSelection();
      if (selection == "Patient 1")
      fill(150);
         text("Kathy Smith",30,40);


       if (selection == "Patient 2")
         fill(150);
         text("John Doe",30,40);


       if (selection == "Patient 3")

         fill(150);

         text("Sue White",30,40);

       // if (selection == "Patient 4")



    }
    void draw() {  
     background(255);
        on_button.Draw();



    }
    // ==========================================================
     class Button {
      String label; // button label
      float x;      // top left corner x position
      float y;      // top left corner y position
      float w;      // width of button
      float h;      // height of button

      // constructor
      Button(String labelB, float xpos, float ypos, float widthB, float heightB) {
        label = labelB;
        x = xpos;
        y = ypos;
        w = widthB;
        h = heightB;
      }

      void Draw() {
        fill(150);
        stroke(141);
        rect(x, y, w, h, 10);
        textAlign(CENTER, CENTER);
        fill(0);
        text(label, x + (w / 2), y + (h / 2));
      }

      boolean MouseIsOver() {
        if (mouseX > x && mouseX < (x + w) && mouseY > y && mouseY < (y + h)) {
          return true;
        }
        return false;
      }
      public boolean surfaceTouchEvent(MotionEvent event) {
      // Call to keep mouseX and mouseY constants updated
      //super.surfaceTouchEvent(event);
      // Forward events
      return gesture.surfaceTouchEvent(event);
    }
    }

Answers

  • edited March 2018 Answer ✓

    @ermina===

    • some errors in your code (for testing strings dont use == but equals); and write your texts inside draw with a boolean; see the code below (i have added some methods in order that the list can be much more long: let us say 200 or 300 items!) without hardcoding; of course with this code you have to add texts (names) to each item: as it is when you click "patient 15" you will get the last selected! - create an arrayList for names also - Now i dont know what happens with ketail list when the number of items is a big number. As for me i would use android listView and scrollView and instead of putting the details as you do i would create a fragment for showing them.

          import android.view.ViewGroup.LayoutParams;
          import ketai.ui.*;
          import android.view.MotionEvent;
      
          Button on_button;  // the button
          int clk = 1;  
      
          KetaiGesture gesture;
      
          KetaiList klist;
      
          KetaiList selectionlist;
          ArrayList<String> textlist = new ArrayList<String>();
      
          boolean[] sel;
          int nbreItems;
          int select=-220;
      
          void setup() {  
            size(1000,600);
            orientation(LANDSCAPE);
            textSize(28);
            textAlign(CENTER);
      
            gesture = new KetaiGesture(this);
             on_button = new Button("Details", 820, 420, 100, 50);
            textlist.add("Patient 1");
            textlist.add("Patient 2");
            textlist.add("Patient 3");
            textlist.add("Patient 4");
            textlist.add("Patient 15");
            nbreItems = textlist.size();
            sel = new boolean[nbreItems];
            for(int i =0;i<nbreItems; i++){
             sel[i]= false;
      
            }
          }
      
      
          void mousePressed()
          {
            if (on_button.MouseIsOver()) {
              selectionlist = new KetaiList(this,textlist); 
      
            }
          } 
      
           void onKetaiListSelection(KetaiList klist)
          {
            String selection = klist.getSelection();
      
            if (selection.equals( "Patient 1")){
      
            fill(150);
      
            updateSelect(0);
      
            klist.refresh();
          }
      
      
      
             if (selection.equals( "Patient 2")){
             println("DEUX");
      
                updateSelect(1);
      
               klist.refresh();
             }
      
      
             if (selection.equals( "Patient 3")){
           println("trois");
      
            updateSelect(2);
      
               fill(150);
      
               klist.refresh();
             }
      
      
      
      
      
      
      
          }
          void draw() {  
           background(255);
          println(select);
      
              on_button.Draw();
      
              if(select==0){
      
              text("Kathy Smith",300,400);
              }
      
              if(select==1){
      
             text("amelie JOe",300,400);
              }
      
              if(select==2){
      
      
              text("chris Harrisson",300,400);
              }
      
      
      
          }
          private void updateSelect(int s){
          for(int i =0; i<sel.length;i++){
                  if(i==s){
                    sel[i]=true;
                    select= i;
                  }else{
              sel[i]= false;
                }
            }
          }
          // ==========================================================
           class Button {
            String label; // button label
            float x;      // top left corner x position
            float y;      // top left corner y position
            float w;      // width of button
            float h;      // height of button
      
            // constructor
            Button(String labelB, float xpos, float ypos, float widthB, float heightB) {
              label = labelB;
              x = xpos;
              y = ypos;
              w = widthB;
              h = heightB;
            }
      
            void Draw() {
              fill(150);
              stroke(141);
              rect(x, y, w, h, 10);
              textAlign(CENTER, CENTER);
              fill(0);
              text(label, x + (w / 2), y + (h / 2));
      
            };
      
      
      
            boolean MouseIsOver() {
              if (mouseX > x && mouseX < (x + w) && mouseY > y && mouseY < (y + h)) {
                return true;
              }
              return false;
            }
            public boolean surfaceTouchEvent(MotionEvent event) {
            // Call to keep mouseX and mouseY constants updated
            //super.surfaceTouchEvent(event);
            // Forward events
            return gesture.surfaceTouchEvent(event);
          }
          }
      
  • Could you please show me a similar example with android listView and scrollView and instead of putting the details, to create a fragment for showing them, as you said??And what happens if i want to add a second button??Is the same way to do it???

  • @ermina===

    • first thing: put answered for others

    • second :try to elaborate some code for a listView: i have here put examples && snippets for textView, webview, videoView and for all Views it's the same way to use

    • then open a new topic with your code on the forum

    • as for a second button of course it is the same thing and it's easy as you have already a button class

  • If i add a second button with the same way and with a KetaiList also how will i manage a second onKetaiListSelection(KetaiList klist)??Only just with a change on the name klist???

  • @ermina===

    sorry, i thought it was a button like your "details"; if it is for a list you have to create another instance of KetaiList called as you want and add to it some on selectionListener like for the first list; what i dont know because i dont use ketai is if you can parameter the lists (setting X...Y...and so on)... Yet i am not sure to understand exactly what you want! Try to explain better using your example.

  • For example i have made a programm which is in my discussions with the title of "ploting data". This programm loads csv files which are ECG signals, by pushing a button and make them plots.I was thinking about adding one more button like details to show some texts, names, etc...

Sign In or Register to comment.