I have created an activity where using a ScrollView. Inside the ScrollView I have created a LinearLayout with a ViewFlipper of two children layouts and have set ViewFlipper to "invisible". Before the ViewFlipper I have two buttons and I want to set my application to load the right child of ViewFlipper depending on the button that user pressed. ViewFlipper's children layouts are with buttons. I made them add their ID in an array when pressed and I want to let them in pressed status until user press them again in order to release them. I have almost done it, but I have faced two problems:
1) When pressing the button, ViewFlipper draws the relative layout, but onTouchListener doesn't work until I press the other button and ViewFlipper will draw the other layout. Then it looks it is working correctly when changing layouts. Does anyone know how to make it work instantly without switching between layouts?
2) onTouchListener doesn't work totally right. I mean, I want to set button.setPressed(true) or false if already has been set to true, but looks like it is random if it will work correctly. Sometimes, I press a button and nothing happens and after a few seconds I try again and works fine. Does anyone know how can I solve it or has to suggest a better way to 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 an 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 is my 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 and convert it to string in order to remove the 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 to true");
return true;
}
else
return true;
}
};
public void addToArray(int[] ar, int a)
{
for (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 to 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 and toggleButtons instead of simple Buttons and worked fine!