Hello Guys I am New to Android. I am developing an App but stuck here. I want to set Random Text to TextView and I did it successfully but I am getting same text on all textview I put it. I want if the text set to one textview should not be show to other means its should not be repeat. Any help must be appreciated. I am posting some code here.
int[] array_set_up_text;
Random rand_set_up_text;
rand_set_up_text = new Random();
array_set_up_text = new int[] { R.string.set_up_text1, R.string.set_up_text2, R.string.set_up_text3,
R.string.set_up_text4, R.string.set_up_text5, R.string.set_up_text6, R.string.set_up_text7,
R.string.set_up_text8,R.string.set_up_text9,R.string.set_up_text10,R.string.set_up_text11,R.string.set_up_text12};
rand_text_set_up = rand_set_up_text.nextInt(array_set_up_text.length - 1);
text1.setText(array_set_up_text[rand_text_set_up]);
text2.setText(array_set_up_text[rand_text_set_up]);
text3.setText(array_set_up_text[rand_text_set_up]);
change your code to:
text1.setText(array_set_up_text[rand_set_up_text.nextInt(array_set_up_text.length)]);
text2.setText(array_set_up_text[rand_set_up_text.nextInt(array_set_up_text.length)]);
text3.setText(array_set_up_text[rand_set_up_text.nextInt(array_set_up_text.length)]);
You need to recall the nextInt() method each time:
text1.setText(array_set_up_text[rand_text_set_up]);
rand_set_up_text.nextInt()
text2.setText(array_set_up_text[rand_text_set_up]);
rand_set_up_text.nextInt()
text3.setText(array_set_up_text[rand_text_set_up]);
Your problem is this line
rand_text_set_up = rand_set_up_text.nextInt(array_set_up_text.length - 1);
You get a random position once, then don't update it.
Easiest solution is to do something like below
rand_text_set_up = rand_set_up_text.nextInt(array_set_up_text.length - 1);
text2.setText(array_set_up_text[rand_text_set_up]);
//Get a new random position.
rand_text_set_up = rand_set_up_text.nextInt(array_set_up_text.length - 1);
text3.setText(array_set_up_text[rand_text_set_up]);
//Get a new random position.
rand_text_set_up = rand_set_up_text.nextInt(array_set_up_text.length - 1);
text3.setText(array_set_up_text[rand_text_set_up]);
Related
I am making an app with Xamarin Android. It has 4 turns, and I want a random picture+sound to display in every turn. So far so good, but now I want to make it in a way that it won't show the same picture+sound(the same random number) twice. I have tried some codes from other treads, but unfortunately that did not work.
Following is what I tried from the other treads:
if (ActivityGame.playing == false)
{
List<int> alreadyGuessed = new List<int>();
Random randomSound = new Random();
int theSound = randomSound.Next(1, 5);
while (alreadyGuessed.Contains(theSound))
theSound = randomSound.Next(1, 5);
alreadyGuessed.Add(theSound);
return theSound;
}
You declare the alreadyGuessed list inside the if-branch. So, every time you get in that if branch, the program creates a new, empty list named alreadyGuessed.
You should make alreadyGuessed a member variable of your ActivityGame class, so it will retain the values already used.
I am trying to randomly call methods to make it use a different animation every time to display a textview. I trie using an array but not sure how to implement it. My array looks like this (obviously I know it shouldn't be strings but not sure what data type or if even possible this way).
String [] mAnimations = {"Hinge","RollIn", "Landing",
"BounceIn", "BounceInDown", "BounceInLeft", "BounceInRight",
"BounceInUp", "FadeIn", "FadeInUp", "FadeInDown", "FadeInLeft",
"FadeInRight","FlipInX", "RotateIn", "RotateInDownLeft", "RotateInDownRight",
"RotateInUpLeft", "RotateInUpRight", "SlideInLeft", "SlideInRight",
"SlideInUp", "SlideInDown", "ZoomIn", "ZoomInDown", "ZoomInLeft",
"ZoomInRight", "ZoomInUp"};
Putting any of the correct animations by themselves it works fine (Note BounceInLeft):
YoYo.with(Techniques.BounceInLeft)
.duration(1000)
.playOn(textView);
But what I am trying to do is randomly pick an animation from the array something like this:
Random rand = new Random();
int n = rand.nextInt(mAnimations.length) + 1;
YoYo.with(Techniques.(mAnimations[n]))
.duration(1000)
.playOn(textView);
Thanks for your help
Nicholas
Best Directly Assign method based on Random numbers.Because (mAnimations[n]) value consider as String.
String [] mAnimations = {"Hinge","RollIn", "Landing",
"BounceIn", "BounceInDown", "BounceInLeft", "BounceInRight",
"BounceInUp", "FadeIn", "FadeInUp", "FadeInDown", "FadeInLeft",
"FadeInRight","FlipInX", "RotateIn", "RotateInDownLeft", "RotateInDownRight",
"RotateInUpLeft", "RotateInUpRight", "SlideInLeft", "SlideInRight",
"SlideInUp", "SlideInDown", "ZoomIn", "ZoomInDown", "ZoomInLeft",
"ZoomInRight", "ZoomInUp"};
Random rand = new Random();
int n = rand.nextInt(mAnimations.length);
if(n==0)
{
YoYo.with(Techniques.Hinge).duration(1000).playOn(textView);
}
if(n==1)
{
YoYo.with(Techniques.RollIn).duration(1000).playOn(textView);
}
I try to change the background of my Activity ( onCreate ) randomly. It stucks in the the last step ... show it. Maybe someone has an idea for me.
I created an array within a xml file which contains 5+ drawable's - it looks like this ..
<array name="backgrounds">
<item>#drawable/bg1_320x480</item>
<item>#drawable/bg2_320x480</item>
<item>#drawable/bg3_320x480</item>
<item>#drawable/bg4_320x480</item>
<item>#drawable/bg5_320x480</item>
<item>#drawable/bg6_320x480</item>
</array>
Within my main activity, I get one random element out of the array ...
String[] mTempArray = getResources().getStringArray(R.array.backgrounds);
int iMin = 0;
int iMax = 5;
int randomIndex = iMin + (int) (Math.random() * iMax);
String resPath = mTempArray[randomIndex];
resPath return me (e.g.) res/drawable-hdpi/bg4_320x480.png. From this point on, I found a lot of solutions, but nothing brings me to succes.
What is the last point to set / change / overwrite the background?
A resource ID is just an integer--there's no need to deal with strings. You can use something like this
int[] imageIds = new int[] { R.drawable.bg1, R.drawable.bg2, ... };
, pick a random element, and set it as your background. Not sure if you can encode the array of resource IDs in xml.
I have 4 textview such as t11,t12,t13,t14 and I have also 4 value in array val[4].
I want to store these values randomly in textviews. but I am getting little problem.
I have done following code:
TextView t11,t12,t13,t14;
Random r = new Random();
for (int i = 0; i < val.length; i++) {
int val[4]=r.nextInt(10);
Log.d("horror", "Randm Array of VAL:" +val[i]);
}
In the Log,there are 4 values displayed but how to display them in textviews.
I have coded but it does not work properly.
t1[i+1].setText("" +val[i]);
and
In this case,values are properly displayed, but i want to do code optimization.
t11.setText("" +val[0]);
t12.setText("" +val[1]);
t13.setText("" +val[2]);
t14.setText("" +val[3]);
Thanks in advance.
Every time you loop in for, you create another integer array. Take the definition of val out of the for loop.
you can store their references inside an array , it won't create new objects. so this should do the job
TextView [] textviews = {t11,t12,t13,t14};
for(int i =0;i<textviews.length;++i){
textviews[i].setText(val[i]);
}
For your TextView use something like,
TextView [] tv = {t11,t12,t13,t14};
and use tv for other going stuff... So now, you can getting it work by,
tv[i+1].setText("" +val[i]);
BigInteger q = new BigInteger(8, 10, new Random()); is giving me random numbers as expected every time while running from my Desktop but not in Android.
in Android I am always getting only same output instead of random number. Please help me to get random BigIntegr.
for your info:
int randQ = (int) (Math.random() * 9);
for (int r = 0; r < randQ; r++) {
q = q.nextProbablePrime();
}
is the quick fix that I did and I am not happy with this fix since it is consuming extra time.
your suggestions are highly appreciated
This is a very common problem, independent of language or platform.
You must reuse the instance of Random() to get random numbers each time. The default constructor will seed the pseudo-random number generator with the current time. The current time doesn't change very quickly with respect to your program, so you keep getting the same number until the clock ticks up.
If you don't re-seed, it will give you different numbers each time you ask for another value. You can avoid re-seeding by reusing the instance, and calling:
Random random = new Random(); // reuse this instance...
int value = random.nextInt(); // use these values instead of new Random()
See: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Random.html
EDITED
Instead use:
// creates random object with current instant as seed
Random generator = new Random((new Date()).getTime());
BigInteger q = new BigInteger(8, 10, generator);