java - onTouchListener with ViewFlipper doesn't work immediately -


i have created activity using scrollview. inside scrollview have created linearlayout viewflipper of 2 children layouts , have set viewflipper "invisible". before viewflipper have 2 buttons , want set application load right child of viewflipper depending on button user pressed. viewflipper's children layouts buttons. made them add id in array when pressed , want let them in pressed status until user press them again in order release them. have done it, have faced 2 problems:

1) when pressing button, viewflipper draws relative layout, ontouchlistener doesn't work until press other button , viewflipper draw other layout. looks working correctly when changing layouts. know how make work instantly without switching between layouts?

2) ontouchlistener doesn't work totally right. mean, want set button.setpressed(true) or false if has been set true, looks random if work correctly. sometimes, press button , nothing happens , after few seconds try again , works fine. know how can solve or has suggest better way make buttons stay pressed?

my activity xml file is:

<linearlayout 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:gravity="center_horizontal" android:orientation="vertical" >      <scrollview     android:id="@+id/scrollview"     android:layout_width="match_parent"     android:layout_height="wrap_content" >      <linearlayout         android:id="@+id/formlayout"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:orientation="vertical" >      <textview         android:id="@+id/selecttxt"         android:layout_width="wrap_content"         android:layout_height="wrap_content"     android:layout_gravity="center_horizontal"     android:text="select option" />      <linearlayout             android:id="@+id/choosebuttons"             android:layout_width="wrap_content"             android:layout_height="120dp"             android:layout_margintop="10dp"             android:layout_marginbottom="10dp"             android:layout_gravity="center_horizontal"             android:orientation="horizontal" >          <button                    android:id="@+id/firstbtn"                    android:layout_width="120dp"                    android:layout_height="fill_parent"                    android:layout_marginbottom="1dp"                    android:layout_margintop="2dp"                    android:layout_marginright="5dp"                    android:text="option 1"                    android:textcolor="@android:color/white"                    android:background="@drawable/option1"/>         <button                   android:id="@+id/secondbtn"                   android:layout_width="120dp"                   android:layout_height="fill_parent"                   android:layout_marginbottom="1dp"                   android:layout_margintop="2dp"                   android:layout_marginleft="5dp"                   android:text="option 2"                   android:textcolor="@android:color/white"                   android:background="@drawable/option2"/>      </linearlayout>         <textview             android:id="@+id/subchoices"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_margintop="10dp"             android:layout_marginbottom="20dp"             android:layout_gravity="center_horizontal"             android:text="select 5 options" />         <viewflipper           android:id="@+id/optionsflipper"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:visibility="invisible">          <include android:id="@+id/include1" layout="@layout/option1layout" />         <include android:id="@+id/include2" layout="@layout/option2layout" />            </viewflipper>         <button             android:id="@+id/okbtn"             android:text="ok"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_gravity="center_horizontal"             android:layout_margintop="10dp"/>      </linearlayout>     </scrollview> 

here java code:

public class optionsselection extends activity  {     protected button ok, option1, option2;     protected button suboption1, suboption2, suboption3, suboption4, suboption5;     protected viewflipper optionsflipper;     protected int optionid;     protected boolean status, exists;     protected int[] suboptions = new int[3];     protected int counter=0;      @override     protected void oncreate(bundle savedinstancestate)      {         super.oncreate(savedinstancestate);         setcontentview(r.layout.options);         option1 = (button) findviewbyid(r.id.firstbtn);         option2 = (button) findviewbyid(r.id.secondbtn);         ok = (button) findviewbyid(r.id.okbtn);         optionsflipper = (viewflipper)findviewbyid(r.id.optionsflipper);          option2.setonclicklistener(new view.onclicklistener()          {             @override             public void onclick(view v)              {                 if (optionid == 2)                 {                     for(int i=0;i<suboptions.length;i++)                         suboptions[i] = 20;                 }                 exists = false;                 optionid = 1;                 counter = 0;                 optionsflipper.setdisplayedchild(1);                 suboption1 = (button) findviewbyid(r.id.option1suboption1);                 suboption2 = (button) findviewbyid(r.id.option1suboption2);                 suboption3 = (button) findviewbyid(r.id.option1suboption3);                 suboption4 = (button) findviewbyid(r.id.option1suboption4);                 suboption5 = (button) findviewbyid(r.id.option1suboption5);                 setontouchlisteners();                 optionsflipper.setvisibility(view.visible);                 status = false;              }          });           option1.setonclicklistener(new view.onclicklistener()          {             @override             public void onclick(view v)              {                 if (optionid == 1)                 {                     for(int i=0;i<suboptions.length;i++)                     {                         suboptions[i] = 20;                     }                 }                 exists = false;                 optionid = 2;                 counter = 0;                 optionsflipper.setdisplayedchild(2);                 suboption1 = (button) findviewbyid(r.id.option1suboption1);                 suboption2 = (button) findviewbyid(r.id.option1suboption2);                 suboption3 = (button) findviewbyid(r.id.option1suboption3);                 suboption4 = (button) findviewbyid(r.id.option1suboption4);                 suboption5 = (button) findviewbyid(r.id.option1suboption5);                 setontouchlisteners();                 optionsflipper.setvisibility(view.visible);                 status = false;             }          });          ok.setonclicklistener(new view.onclicklistener()          {                @override             public void onclick(view v)              {                 for(int i=0;i<suboptions.length;i++)                 {                     system.out.println(suboptions[i]);                 }              }         });      }   public class ontouchlistener implements view.ontouchlistener {     @override     public boolean ontouch(view v, motionevent event)      {            if (status == true)         {             v.setselected(false);             v.setpressed(false);             status=false;             return true;         }         else if (status == false)         {             v.setselected(true);             v.setpressed(true);             status=true;                         /*getting suboption button tag , convert string in order remove first 6 characters*/             string buttonclicked = v.gettag().tostring();             int buttonid = integer.parseint(buttonclicked.substring(6));             switch (buttonid)             {             case 1:                 {                     system.out.println("case 1!");                     if (optionid == 1)                         addtoarray(suboptions, 1);                     else if (optionid == 2)                         addtoarray(suboptions, 6);                     else                         system.out.println("error occurred in assign of key");                 }                 break;             case 2:                 {                     if (optionid == 1)                         addtoarray(suboptions, 2);                     else if (optionid == 2)                         addtoarray(suboptions, 7);                     else                         system.out.println("error occurred in assign of key");                 }                 break;             case 3:                 {                     if (optionid == 1)                         addtoarray(suboptions, 3);                     else if (optionid == 2)                         addtoarray(suboptions, 8);                     else                         system.out.println("error occurred in assign of key");                 }                 break;             case 4:                 {                     if (optionid == 1)                         addtoarray(suboptions, 4);                     else if (optionid == 2)                         addtoarray(suboptions, 9);                     else                         system.out.println("error occured in assign of key");                 }                 break;             case 5:                 {                        if (optionid == 1)                         addtoarray(suboptions, 5);                     else if (optionid == 2)                         addtoarray(suboptions, 10);                     else                         system.out.println("error occured in assign of key");                 }                 break;             }              system.out.println("pressed changed true");             return true;         }         else             return true;     } };      public void addtoarray(int[] ar, int a)     {         (int i=0;i<ar.length;i++)          {             if (ar[i] == a)             exists = true;               system.out.println("already exists");         }         if((counter<=3)&&(exists==false))         {             ar[counter] = a;             counter++;         }         else if ((counter>3)&&(exists==false))         {             counter = 0;             ar[counter] = a;             counter++;         }         else         {             system.out.println("already exists...");         }         system.out.println("added new integer array");     }      public void setontouchlisteners()     {         suboption1.setontouchlistener(new ontouchlistener());         suboption2.setontouchlistener(new ontouchlistener());         suboption3.setontouchlistener(new ontouchlistener());         suboption4.setontouchlistener(new ontouchlistener());         suboption5.setontouchlistener(new ontouchlistener());     }  } 

i finally, used fragments instead of viewflipper , togglebuttons instead of simple buttons , worked fine!


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -