I have a test code using the View Property Animation to switch locations of two buttons. The code is as below:
public class TestAnimationActivity extends Activity {
private View mainView;
private View TempView;
private Button B1, B2, B3;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate your view
setContentView(R.layout.main_test);
mainView = findViewById(R.id.mainTest);
B1 = (Button) mainView.findViewById(R.id.B1);
B2 = (Button) mainView.findViewById(R.id.B2);
B3 = (Button) mainView.findViewById(R.id.B3);
OnClickListener OCL = new OnClickListener(){
#Override
public void onClick(View v) {
if(TempView == null){
TempView = v;
}else{
int originalPos1[] = new int[2];
TempView.getLocationOnScreen( originalPos1 );
int originalPos2[] = new int[2];
v.getLocationOnScreen( originalPos2 );
v.animate().translationX((float)originalPos1[0] - (float)originalPos2[0]);
v.animate().translationY((float)originalPos1[1] - (float)originalPos2[1]);
TempView.animate().translationX((float)originalPos2[0] - (float)originalPos1[0]);
TempView.animate().translationY((float)originalPos2[1] - (float)originalPos1[1]);
TempView = null;
}
}};
B1.setOnClickListener(OCL);
B2.setOnClickListener(OCL);
B3.setOnClickListener(OCL);
}
}
The XML file is as below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainTest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/B1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#B4B4B4"
android:gravity="center" />
<Button
android:id="#+id/B2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#470047"
android:gravity="center" />
<Button
android:id="#+id/B3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center" />
</LinearLayout>
When I was testing this code on my phone. The animation did not work properly.
Two buttons will switch position the first time, but the button will go to wrong position if I continue clicking them. It seems that the button still calculate the position it should move to based on its original location when the app launched.
Anyone knows what is going wrong?
Related
I have to create a Stack-bar graph and my requirement is to graph should look like this.
I have checked many graph libraries but no one is providing this type of UI.
is this possible to achieve?
So I finally found a way for that.
Step 1: Add 3 views with respective colors and a button like in below XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<View
android:layout_width="75sp"
android:layout_height="15sp"
android:layout_marginTop="100sp"
android:layout_marginStart="10sp"
android:background="#ff0000"
android:id="#+id/viewOverdue"/>
<View
android:layout_width="75sp"
android:layout_height="15sp"
android:layout_marginTop="100sp"
android:layout_marginStart="2sp"
android:background="#FFDD00"
android:layout_toEndOf="#+id/viewOverdue"
android:id="#+id/viewActive"/>
<View
android:layout_width="75sp"
android:layout_height="15sp"
android:layout_marginTop="100sp"
android:layout_marginStart="2sp"
android:background="#00FF00"
android:layout_toEndOf="#+id/viewActive"
android:id="#+id/viewDone"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Overdue"
android:id="#+id/editOverdue"
android:layout_below="#+id/viewActive"
android:layout_marginTop="100sp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Active"
android:layout_below="#+id/editOverdue"
android:id="#+id/editActive"
android:layout_marginTop="2sp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Done"
android:layout_below="#+id/editActive"
android:id="#+id/editDone"
android:layout_marginTop="2sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:id="#+id/btnSubmit"
android:layout_below="#id/editDone"
android:layout_marginTop="20sp"/>
</RelativeLayout>
This is how it will Look:
Do note that views are set to invisible initially.
Step 2: The coding part:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private EditText typeActive, typeDone, typeOverdue;
private Button btnSubmit;
private View viewDone, viewOverDue, viewActive;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
typeActive = findViewById(R.id.editActive);
typeDone = findViewById(R.id.editDone);
typeOverdue = findViewById(R.id.editOverdue);
btnSubmit = findViewById(R.id.btnSubmit);
viewDone = findViewById(R.id.viewDone);
viewActive = findViewById(R.id.viewActive);
viewOverDue = findViewById(R.id.viewOverdue);
viewDone.setVisibility(View.INVISIBLE);
viewOverDue.setVisibility(View.INVISIBLE);
viewActive.setVisibility(View.INVISIBLE);
//Type all the number of tasks and press the submit button
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String tasksActive = typeActive.getText().toString();
final String tasksDone = typeDone.getText().toString();
final String tasksOverdue = typeOverdue.getText().toString();
int intTaskActive = Integer.parseInt(tasksActive);
int intTaskDone = Integer.parseInt(tasksDone);
int intTaskOverdue = Integer.parseInt(tasksOverdue);
int totalTasks = intTaskActive + intTaskOverdue + intTaskDone;
float percentageActive = (intTaskActive*100.0f)/totalTasks;
float percentageDone = (intTaskDone*100.0f)/totalTasks;
float percentageOverdue = (intTaskOverdue*100.0f)/totalTasks;
int intPercActive = (int)percentageActive;
int intPercDone = (int)percentageDone;
int intPercOverdue = (int)percentageOverdue;
viewDone.setVisibility(View.VISIBLE);
viewOverDue.setVisibility(View.VISIBLE);
viewActive.setVisibility(View.VISIBLE);
viewDone.setLayoutParams(new RelativeLayout.LayoutParams(intPercDone*10, 50));
viewActive.setLayoutParams(new RelativeLayout.LayoutParams(intPercActive*10, 50));
viewOverDue.setLayoutParams(new RelativeLayout.LayoutParams(intPercOverdue*10, 50));
}
});
}
}
This is the output after execution:
So how this works is you type the number of tasks in the edittext. It doesn't matter how you get the number of tasks but you need to convert it to percentage of total length of the stackBar or whatever you call it.
So finally you just assign each view the respective width as per the number.
This is my child xml file(repeat.xml) which i want to add in main xml file
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginTop="200dp"
android:id="#+id/divider_parent"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<EditText
android:layout_width="350dp"
android:layout_height="50dp"
android:text="enter name here"
android:id="#+id/tv2"
android:layout_marginTop="90dp"/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="edit"
android:id="#+id/b1"
android:layout_marginTop="160dp"/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="save"
android:id="#+id/b3"
android:layout_marginTop="160dp"
android:layout_marginLeft="110dp"/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="button2"
android:id="#+id/b2"
android:layout_marginTop="160dp"
android:layout_marginLeft="220dp"/>
</RelativeLayout>
</FrameLayout>
this is my main.xml
<RelativeLayout 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:id="#+id/main"
tools:context="com.example.pawan.recyclerview.Main2Activity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main1"
android:layout_marginTop="200dp">
</RelativeLayout>
this is my main.java
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
RelativeLayout rl=findViewById(R.id.main1);
// RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// params.addRule(RelativeLayout.BELOW,R.id.main1);
for (int i=0;i<4;i++) {
View child = getLayoutInflater().inflate(R.layout.repeat, null);
rl.addView(child);
}
final EditText textView2=findViewById(R.id.tv2);
textView2.setEnabled(false);
Button button1=findViewById(R.id.b1);
Button button2=findViewById(R.id.b2);
Button button3=findViewById(R.id.b3);
final TimePicker tp=findViewById(R.id.timePicker);
tp.setEnabled(false);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView2.setEnabled(false);
tp.setEnabled(false);
Toast.makeText(getApplicationContext(),textView2.getText().toString()+" time is:" +tp.getCurrentHour() + ":" + tp.getCurrentMinute(),Toast.LENGTH_SHORT).show();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(Main2Activity.this, Main3Activity.class);
startActivity(intent);
finish();
}
});
}
}
when i do not use the for loop in main.java and direct add child view only once it's working fine. But since i want to add it multiple times and on adding it using for loop multiple times the repeat.xml buttons stops working, thwy does'nt give any feedback i.e they becomes fixed(not clickable) and textview are not editable. Please give a solution for this :)
Your main error is here:
for (int i=0;i<4;i++) {
// add several layouts
}
// findViewById is outside the loop. So, this code will be executed only once and only for one view.
findViewById does not automatically changes all view with that ID. It just seach and return the first view with that ID. If you have more than one view using the same ID only the first view with that ID will be returned (which is your case because you are inflating same layout several times).
Also, you must find for a View not in the whole screen (since you have several views with same ID). You can search "inside" a specific layout or inside a view...
Instead of:
// Search a view inside the whole screen (root layout).
findViewById(R.id.tv2);
You can use:
// Search a view only inside child
child.findViewById(R.id.b1);
You should do something like:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
RelativeLayout rl=findViewById(R.id.main1);
for (int i=0;i<4;i++) {
View child = getLayoutInflater().inflate(R.layout.repeat, null);
rl.addView(child);
// -----------------------------------------------------------------
// Not that I'm looking for a R.id.tv2 but inside child view... and not in root view
// Use child.findViewById(R.id.tv2) instead of findViewById(R.id.tv2)
final EditText textView2 = child.findViewById(R.id.tv2);
textView2.setEnabled(false);
// SAME HERE
Button button1 = child.findViewById(R.id.b1);
Button button2 = child.findViewById(R.id.b2);
Button button3 = child.findViewById(R.id.b3);
final TimePicker tp = child.findViewById(R.id.timePicker);
tp.setEnabled(false);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView2.setEnabled(false);
tp.setEnabled(false);
Toast.makeText(getApplicationContext(),textView2.getText().toString()+" time is:" +tp.getCurrentHour() + ":" + tp.getCurrentMinute(),Toast.LENGTH_SHORT).show();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(Main2Activity.this, Main3Activity.class);
startActivity(intent);
finish();
}
});
}
Note that are better ways to what you want such as ListView, RecyclerView etc... but this is another history.
well i'm looking for a way to flip the button on a linear layout vertical here is a screen shot of the buttons
the xml code is
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:id="#+id/buttons_layout">
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iv_green"
android:layout_weight="1.0"
android:src="#drawable/leaf_green"
android:layout_gravity="fill_vertical"
android:scaleType="fitXY"
android:onClick="HandleClick" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iv_other"
android:layout_weight="1.0"
android:src="#drawable/leaf_other"
android:layout_gravity="fill_vertical"
android:scaleType="fitXY"
android:onClick="HandleClick" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iv_red"
android:layout_weight="1.0"
android:src="#drawable/leaf_red"
android:layout_gravity="fill_vertical"
android:scaleType="fitXY"
android:onClick="HandleClick" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iv_yellow"
android:layout_weight="1.0"
android:src="#drawable/leaf_yellow"
android:layout_gravity="fill_vertical"
android:scaleType="fitXY"
android:onClick="HandleClick" />
</LinearLayout>
what i want to do is that when i push flip button on (which is another button) i want to change the position of the current button i will explain in pics:
okay guys i used the idea that Arnab Told me about in the post below, well i want to randomly flip the image buttons so i did the following script:
public void onClick(View v) {
btnsLayout.removeAllViews();
ArrayList<Integer> a = new ArrayList<Integer>();
for(int i = 0; i < 4; i++)
{
a.add(i);
}
Collections.shuffle(a);
btnsLayout.addView(Other, a.get(0).intValue());
btnsLayout.addView(Green, a.get(1).intValue());
btnsLayout.addView(Red,a.get(2).intValue());
btnsLayout.addView(Yellow,a.get(3).intValue());
}
the application is crashing the crash reason is:
java.lang.IndexOutOfBoundsException: index=2 count=1
what does it means ?
Thank you !
You can do this inside onCLick(View view) of Flip Button:
// Get all the views
LinearLayout btnsLayout = (LinearLayout) findViewById(R.id.buttons_layout);
ImageButton ivGreen = (ImageButton) findViewById(R.id.iv_green);
ImageButton ivOther = (ImageButton) findViewById(R.id.iv_other);
ImageButton ivRed = (ImageButton) findViewById(R.id.iv_red);
ImageButton ivYellow = (ImageButton) findViewById(R.id.iv_yellow);
// Remove views
btnLayout.removeView(ivGreen);
btnLayout.removeView(ivRed);
// ReAdd views in new index[Index 0 = position 1, Index 2 = position 3]
btnLayout.addView(ivRed, 0);
btnLayout.addView(ivGreen, 2);
Similarly :
// Remove views
btnLayout.removeView(ivOther);
btnLayout.removeView(ivYellow);
// ReAdd views in new index[Index 1 = position 2, Index 3 = position 4]
btnLayout.addView(ivYellow, 1);
btnLayout.addView(ivOther, 3);
First, you should rename the ids of the ImageButtons like :
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/first_leaf"
android:layout_weight="1.0"
android:src="#drawable/leaf_green"
android:layout_gravity="fill_vertical"
android:scaleType="fitXY"
android:onClick="HandleClick" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/second_leaf"
android:layout_weight="1.0"
android:src="#drawable/leaf_other"
android:layout_gravity="fill_vertical"
android:scaleType="fitXY"
android:onClick="HandleClick" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/third_leaf"
android:layout_weight="1.0"
.../>
Then find your flip button in your java class and set an OnClickListener
like this:
myFlipButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
}):
Then, in the onClick function, perform the changes:
myFlipButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myFirstLeaf.setImageDrawable(getResources().getDrawable(R.drawable.leaf_red));
mySecondLeaf.setImageDrawable(getResources().getDrawable(R.drawable.leaf_yellow));
...
}
});
Hope it helps.
You can use the imgButton.setBackgroundResource(); method.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
imgButton1.setBackgroundResource(R.drawable.image3);
imgButton3.setBackgroundResource(R.drawable.image1);
}
});
If you want to swap the views you need to use a ViewGroup and you can set the index accordingly.
How do I change the position of a view in a Linear Layout.?
Hello Using Arnab Suggestion and some tuning i found a solution: the problem was that you need to insert the index 1 2 3 4 you can't randomly add the views to the linear layout.
public void addListenerOnButton() {
Button flip = (Button) findViewById(R.id.btn_flip);
final LinearLayout btnsLayout = (LinearLayout) findViewById(R.id.buttons_layout);
final ImageView Other = (ImageView) findViewById(R.id.iv_other);
final ImageView Green = (ImageView) findViewById(R.id.iv_green);
final ImageView Red = (ImageView) findViewById(R.id.iv_red);
final ImageView Yellow = (ImageView) findViewById(R.id.iv_yellow);
flip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btnsLayout.removeAllViews();
ArrayList<Integer> a = new ArrayList<Integer>();
for (int i = 0; i < 4; i++) {
a.add(i);
}
Collections.shuffle(a);
for (int k = 0; k<=3 ; k++){
if (a.get(0).intValue() == k) {
btnsLayout.addView(Other, a.get(0).intValue());
}
if (a.get(1).intValue() == k) {
btnsLayout.addView(Green, a.get(1).intValue());
}
if (a.get(2).intValue() == k) {
btnsLayout.addView(Red, a.get(2).intValue());
}
if (a.get(3).intValue() == k) {
btnsLayout.addView(Yellow, a.get(3).intValue());
}
}
}
I want to create an activity such as mentioned in photo...
as soon as I press the maximize button I want it to become full screen for the activity and part 1 become minimize, and again when I press the restore button I want it to become in a first state: to be able to see part 1 and part 2 ...
I think if we put two layouts it is possible? Isn't it? Please refer me to a resource to help me about this, or show me the code to achieve a solution.
Part one and two should be in their own layout. After, play with the visilibity property of each layout. Specifically to hide any view without it continues to occupy its space, use the value gone for the visibility property.
Ok, here I go. Below you have a complete example of how to hide/show grouped views.
main.xml
<RelativeLayout 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" >
<LinearLayout
android:id="#+id/viewsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextBox One" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="TextBox Two" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="TextBox Three" />
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Hide" />
</RelativeLayout>
Activity
public class MyActivity extends Activity implements OnClickListener {
private boolean viewGroupIsVisible = true;
private View mViewGroup;
private Button mButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewGroup = findViewById(R.id.viewsContainer);
mButton = findViewById(R.id.button);
mButton.setOnClickListener(this);
}
#Override
public void onClick(View button) {
if (viewGroupIsVisible) {
mViewGroup.setVisibility(View.GONE);
mButton.setText("Show");
} else {
mViewGroup.setVisibility(View.VISIBLE);
mButton.setText("Hide");
}
viewGroupIsVisible = !viewGroupIsVisible;
}
I hope this helps ;)
There is a bit simplified solution, than Diego Palomar produced, without using additional variable. I'll take his code to show:
public class MyActivity extends Activity implements OnClickListener {
private View mViewGroup;
private Button mButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewGroup = findViewById(R.id.viewsContainer);
mButton = findViewById(R.id.button);
mButton.setOnClickListener(this);
}
#Override
public void onClick(View button) {
if (mViewGroup.getVisibility() == View.VISIBLE) {
mViewGroup.setVisibility(View.GONE);
mButton.setText("Show");
} else {
mViewGroup.setVisibility(View.VISIBLE);
mButton.setText("Hide");
}
}
I'm using ViewFLipper with Random.nextInt(). to change layouts onClick (Button). Now I have 3 xml layouts. 1_view.xml 2_view.xml 3_view.xml. Starting from 1_view.xml I have a button there. When button clicked I should get a random layout. it works. But the problem is now, sometimes I get the same layout (1_view.xml). When a user clicks a button on (1_view.xml), I want them to go to the layouts I have left (2_view.xml and 3_view.xml).
Codes
Main.xml
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:id="#+id/TVLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="left|top"
android:text="0"
android:textColor="#4CB848"
android:textSize="40sp"
android:textStyle="bold" />
<TextView
android:id="#+id/TVRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/TVLeft"
android:gravity="right"
android:text="0"
android:textColor="#FF0000"
android:textSize="40sp"
android:textStyle="bold" />
</RelativeLayout>
<ViewFlipper
android:id="#+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include
android:id="#+id/1_View"
layout="#layout/1_view" />
<include
android:id="#+id/2_View"
layout="#layout/2_view" />
<include
android:id="#+id/3_View"
layout="#layout/3_view" />
</ViewFlipper>
</LinearLayout>
Main.java
// FlipperView
MyViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
TVRight = (TextView) findViewById(R.id.TVRight);
TVLeft = (TextView) findViewById(R.id.TVLeft);
// 1_view.xml
Q1_btn1 = (Button) findViewById(R.id.btn_1);
Q1_btn2 = (Button) findViewById(R.id.btn_2);
Q1_btn3 = (Button) findViewById(R.id.btn_3);
Q1_btn4 = (Button) findViewById(R.id.btn_4);
Q1_btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Wrongnum++;
WrongResult.setText(Integer.toString(Wrongnum));
}
});
Q1_btn2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Rightnum++;
RightResult.setText(Integer.toString(Rightnum));
Random RandomView = new Random();
MyViewFlipper.setDisplayedChild(RandomView.nextInt(3));
}
});
Q1_btn3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Wrongnum++;
WrongResult.setText(Integer.toString(Wrongnum));
}
});
Q1_btn4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Wrongnum++;
WrongResult.setText(Integer.toString(Wrongnum));
}
});
You can do this in your code:
Random RandomView = new Random();
int nextViewIndex = RandomView.nextInt(3);
while (nextViewIndex == MyViewFlipper.getDisplayedChild()) {
nextViewIndex = RandomView.nextInt(3);
}
MyViewFlipper.setDisplayedChild(nextViewIndex);
Basically just call Random.nextInt() until it doesn't match current viewFlipper's index.
To only show a viewFlipper once randomly:
// Intialize this once
List<Integer> vfIndices = new ArrayList<Integer>();
for (int i = 0; i < MyViewFlipper.getChildCount(); i++) {
vfIndices.add(i);
}
// when button is clicked
if (vfIndices.size() == 0) {
return; // no more view flipper to show!
}
int viewIndex = RandomView.nextInt(vfIndices.size());
MyViewFlipper.setDisplayedChild(vfIndices.get(viewIndex));
vfIndicies.remove(viewIndex);
The idea is use an ArrayList to keep track of the viewFlipper index that has not been shown. When button is clicked, pick an index randomly from this array and set the viewFlipper to. Then remove that index from the ArrayList. Next time the button is clicked, it will show one of the remaining flippers.