I tried earlier and just got more confused so i will try and be more precise. I am making an app in which i have a deck of 7 cards. I want to click on the deck and have one of the 7 cards pop up on the screen. So far I have
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final MediaPlayer mpClick = MediaPlayer.create(this, R.raw.click);
randomM = (EditText) findViewById(R.id.randomM);
//button 1 start
Button bMythos = (Button) findViewById(R.id.mythos);
bMythos.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mpClick.start();
Random r = new Random();
int n=r.nextInt(7) + 1;
randomM.setText(String.valueOf(n));
}
});
//button 1 end
}
}
So far this displays the card deck which i click on and a random number is generated (the text box is nearly for me to know the random number generator is working; will be removed when i figure out the display).
So my question
How can i get the random number to correspond with a random card and get the card displayed? - the cards are labeled mythos1, mythos2, etc so i assumed i could do something with mythos(String.valueOf(n)) but that didn't work (unless i'm doing something else wrong) [if you can't tell i have no idea what i'm doing]
Try this way
int[] cards={R.drawable.card1,R.drawable.card2,R.drawable.card3,R.drawable.card4,R.drawable.card5,R.drawable.card6,R.drawable.card7};
Random r = new Random();
int n=r.nextInt(7);
imageview.setImageResource(cards[n]);
Your question is kind of ambiguous, but with whatever little I understood, I suggest,if you are sure of having only 7 decks, why dont you hardcode them and assign a value to each of them. So that when you call random function, it will check which number is the result, suppose it is 5, then call setDrawableResource(R.drawable.img5) and so on.
You should be keeping the cards in an array or List of some kind. Then, you can refer to them by their number. For example:
ArrayList<Card> deck = new ArraList<Card>();
//Fill the ArrayList. Maybe shuffle it.
selectedCard = ArrayList.get(randomNumber);
Card could simply be String or something, instead. I don't know what sort of object you're using for it.
You shoud be using ImageView instead of MediaPlayer. You should assigned 7 images(R.drawables.mythos1, ...) assigened to 1-7 and set it to ImageView imageView.setDrawableResource(R.drawable.myths1); depending on the random number. Look at example from here Get the ID of a drawable in ImageView
Related
i want to store number in array one by one ...when user press button it will generate number in Text field ..i want to store that number in array
This is quite a simple problem...I am writing this but I will hope you will first try and then ask a question from next time:
For this problem u just need to use the function Math.random() in your onClickListner() of your button. Now place this no in textview by using the function setText().
For eg if ur textview name is tv then use tv.setText(no) where no=Math.random()
Something like that?
findViewById(R.id.ButtonRandomNumber).setOnClickListener(mRandomListener);
TextView randomNumber = (TextView) findViewById(R.id.randomNumber);
private OnClickListener mRandomListener = new OnClickListener() {
public void onClick(View v) {
Random rand = new Random();
array[array.length + 1] = rand;
randomNumber.setText(String.format(rand));
}
};
Make the array a global variable and that should more or less work. Was that what you wanted to know?
I am making this card game app, when the user clicks the card this image will turn into a random other card. Here is an example:
public class MainActivity extends Activity {
int[] cards={R.drawable.aceofspades,R.drawable.aceofhearts,R.drawable.aceofclubs};
static Random r = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void imageClick(View view) {
int n=r.nextInt(cards.length);
{
ImageView image = (ImageView) findViewById(R.id.imageDice1);
image.setImageResource(cards[n]);
}
}
}
I would like to alter the code so that it won't be possible that the same card will show twice, not untill all the cards passed. So in this case when the card is "aceofspades" the next card and the card after that should not be "aceofspades".
The first image that users will see is "R.drawable.cardback".
I would like a code that changes the image back to "cardback" when all cards have been shown.
Any help is appreciated.
You need to put the cards (or just the values 1 to 52) in an array or a list, then shuffle them. The requirement you're describing is exactly what shuffling does.
See this answer to get unique random numbers in Java
You need basically the same structure and process.
The question really is that simple. I have an activity with a button that randomly changes the text of a textview box. How do I add another button that gathers the previous number generated so the previous textview text comes back - for a quotes app.Is there a feature I require? I have searched, but I cannot find a feature that will 'go back' on the random number generator.
Thank you in advance.
You dont need to go back on the generator itself, just remember the previous values. Store it in a variable. Here we are using prev as an array, because we need it to be final so we can access it in the anonymous inner classes (click handlers). But we also need to change it. So we use the prev[0] value.
Here I am assuming you have a button called nextNum which generates the next random number. But that could be anything, it doesnt matter. When you generate a new random number simply update the 0th element of the prev array and you should be fine.
final String[] prev = new String[] { "" }
#Override
public void onCreate(Bundle bundle) {
// ....
Button BackQuote = (Button)findViewById(R.id.back);
final TextView display = (TextView) findViewById(R.id.textView1);
// number generator button
Button nextNum = (Button) findViewById(R.id.random);
nextNum.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// generate a random number
// store it in prev
prev[0] = rndNum.toString();
}
});
BackQuote.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
display.setText(prev[0]);
}
});
}
Unless there is a specific reason you want to avoid this, then this would work perfectly fine. And there is no way to go back on a random generator. By definition it IS random! (Yes, Pseudo-random. Nonetheless)
So if you want to keep an history, you are going to have to implement those yourself. I have yet to see a random generator which has a history feature. I'd be surprised if i do too.
I have just started learning Android and got some misunderstanding. I'm trying to create an application which displays a textView and a button. Every button click generates a new random number which should be displayed in the textView.
But unfortunately my code causes a list of errors. Here it is:
public class FirstAndroidProjectActivity extends Activity {
public OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView) findViewById(R.id.display);
Random r = new Random();
int i = r.nextInt(101);
tv.setText(i);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(listener);
}
}
If I just don't use random and use some string except of i
(for example tv.setText("99");) everything is ok, but it doesn't work with a variable as a parameter of setText().
Where is a mistake?
Hope for your help.
You need to convert your random number to a string before setting the text on your TextView
Try
tv.setText(i +"");
Try:
tv.setText(String.valueOf(i));
Java doesn't auto convert types. The + operator is overloaded to transform the parameters passed to it into a String when one or more of those paramaters is a String. So, when you pass i + "" to setText() you are passing a String, however if you just pass i then the compiler sees you passing an int to a method that expects a String and lets you know that that can't be done.
i is an int, try tv.setText("" + i);
Convert your integer to a String before setting it to the textView. You should also move Random r = new Random(); outside of the method, or else your numbers may not really be random :
Random r = new Random();
#Override
public void onClick(View v) {
TextView tv = (TextView) findViewById(R.id.display);
int i = r.nextInt(101);
tv.setText(Integer.toString(i));
}
From the documentation :
If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers
If you create two Random objects too quickly (for example the user clicks two times on the button very quickly), they will share the same seed (the system clock is used to generate it) and as a result you will get the same number two times.
By creating only one Random instance in a global variable, you avoid this issue.
use
tv.setText(new Integer(i).toString()) ;
I'm new to android developing but right now I'm working on an application that displays Random Facts. Since I don't want it to be in a random order, I would like to have them in a list. I would like to order through them one by one and show them using TextView.
Resources res = getResources();
myString = res.getStringArray(R.array.FactsArray);
That's what I have so far. If I'm right, that just establishes the array so I can be able to use it later. What I had before was rgenerator which chose a random string from the array and displayed it when I clicked a button.
Resources res = getResources();
myString = res.getStringArray(R.array.myArray);
fact = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.text1);
tv.setText(fact);
But Like I said, I would like to just order through them one by one when a button is clicked.
Since you're displaying the strings sequentially, you'll need a counter variable to keep track of where you are in the array. It should be initialized to zero. Each time you click the button it should increment until you reach the end of the array (myString.length - 1).
As far as the actual event handling, that's not hard to do. You just need to create a setOnClickListener() for your button.
int count = 0;
Button b1 = (Button) findViewById(R.id.buttonName);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (count < myString.length) {
tv.setText(myString[count]);
count++;
}
}
});