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.
Related
May seem a small problem, but I'm a beginner so need your help.
I've a "main activity" and there are 2 buttons called "vision and mission" and "Team".
I've "details activity" which has details like about us, vision and mission, team.
Now if I click vision and mission button in "main activity" it should automatically open "details activity" and scroll to / jump to vision and mission section of the activity. The same should work for Team button, wherein it automatically opens details activity and directly shows about team section without requiring user to scroll down.
This is something like a hyperlink to specific section of a website.
You can start the activity by calling this function. Here every section should have a number.
// 1 is about us, 2 is vision and mission, etc.
public void startDetailActivity(int type){
Intent startIntent = new Intent(this, DetailsActivity.class);
startIntent.putExtra("Type", type);
startActivity(startIntent);
}
In your DetailsActivity you get the Type argument an scroll to the corresponding position.
ScrollView scrollView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
// this is the important part
scrollView = findViewById(R.id.scrollView);
int type = getIntent().getIntExtra("Type", 0);
scrollTo(type);
}
public void scrollTo(int type){
if(type == 0)
return;
if(type == 1){
//First argument is x, second is y. Test around a bit with the x value
scrollView.scrollTo(10, 0); //Use this if you want no animation
scrollView.smoothScrollTo(10, 0);//Use this if you want a scroll animation
} //add more if you want
}
I need to add a new feature in my app and I have to put an image on the side of the layout. Such that the feature gets highlighted.
But, even if I write the code to make the view's visibility gone after one click. It still appears next time, when the app gets opened.
So, can anyone tell me how to do this correctly ??
Thanks in advance.
This code should solve your problem
public class MainActivity extends AppCompatActivity {
private ImageView imgTarget;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedPreferences = getSharedPreferences("app_prefs",MODE_PRIVATE);
boolean imageVisible = sharedPreferences.getBoolean("img_visible",true);
Button button = findViewById(R.id.button);
imgTarget = findViewById(R.id.imgTarget);
if (!imageVisible){
imgTarget.setVisibility(View.GONE);
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(imageVisible){
imgTarget.setVisibility(View.GONE);
sharedPreferences.edit().putBoolean("img_visible",false).apply();
}
}
});
}
}
I hope it helps you.
Is your app connected to a database like sql, firebase or something else, if so you can create a counter variable in your database and control your view accordingly .
Based on your description, i'm guessing you've the gone after 1 click part done already.
Use SharedPreferences to see if the app has already been opened.
If yes, then set the Visibility to View.GONE in onCreate after you find the id. Otherwise, show it. Feel free to ask if there's anything else.
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 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
I want to create simply application where user can review some pictures. In list I have some pictures and I want to show next pictures after click button. So this is the way: user start application and after click on the button, previous picture is replaced by next picture from the list. I want to use ImageView to show pictures. Can anyone help me?
I tried:
ImageView img = (ImageView)findViewById(R.id.image);
img.setImageResource(picturelist.get(pictureLeft));
where picturelist is list with pictures, and pictureLeft is int variable represents my index. To my list I add elements by:
picturelist.add(R.drawable.car);
do something like this:
yourButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
if(pictureLeft < picturelist.size())
{
img.setImageResource(picturelist.get(pictureLeft));
pictureLeft++;
}
}
});