Android - Randomly setting a button in a grid - android

this is an extension to this question: Android - Handling a grid
Basically, I am wondering how to make a grid of 4x4 buttons randomly change their features(text, color, etc.) I don't need help setting the actual change in text or color, I just need a way to go about something like this. I don't know if I should write an array and choose from there, or use the GridView. Just a little start to something is all I need, I'm not asking for lots of code. Thanks in advance, I really appreciate it.

If your 4 ids are sequential as is
btnId1 = 8006;
btnId2 = 8007;
btnId3 = 8008;
btnId4 = 8009;
Random rn = new Random();
int chooseId = btnId1 + rn.nextInt(btnId4 - btnId1 + 1);

You can generate a random number like so:
Random r=new Random();
r.nextInt(4);
this will give you a random number in the range [0,4).
Based on this random number, you change the properties of the appropriate tile/square which you can enumerate yourself from 0 to 3 since you have 4 tiles in total.
From both your questions it seems that you lack some basic programming knowledge of algorithms/approaches to problems. You should try to fill that gap, which will make you progress MUCH MUCH faster in your programming endeavors!

Related

Count until zero

I am beginner and trying to write some calculations with App Inventor 2.
I am trying to write a code to calculate Net present value.
The formula of NPV = - investment + CF/(1+i)power up by years of investment, which means if years of investment are > 1 the second part of formula will repeat until it reached the number of years.
I successfully code the formula for one year that works correct, but have problem with the "repeating" the second part powered by number of years.
I tried to declare years as variable to use it as powering number but think something is wrong with it.
In my opinion I need to split the powering number somewhere to memory and then increase it by 1 until the required number. However have no clue how to do it.
Can anyone help?
Screenshot of the blocks
Following the calculation from the NPB Calculator,
this is converted into blocks the following
Note: for a better clarity and to avoid such long calculation blocks as in your screenshot, I used External Inputs instead of Inline Inputs, which is the default. You can switch that from the context menu after doing a right mouse click onto one of the calculation blocks.
EDIT: screenshot updated for changing cashflows using a list.See also
How to work with Lists by Saj and
How to work with Lists and Lists of lists (pdf) by appinventor.org

Android: image ID in layout directory

I have 100 images in my application of different cities and I want to divide these pictures in different groups, lets say in evening, morning, sunny, raining etc…
We know that when we call an image from layout folder by calling R.layout.image_1, android generates integer number for each image
For example:
R.layout.image_1 (223344), R.layout.image_2 (556677),
R.layout.image_3 (778899),
I can create one table having evening, morning fields and I can assign group of pictures to each of them with integer IDs which are (223344,556677) and I can call evening or morning group and i can display all images related to these group.
My question is: Does Android generate same number every time. Are these numbers are fixed? When ever the application runs.
If its true then upper idea will work for me. If this idea is incorrect then kindly guide me what is the decent approach to handle hundreds of PNGs in application.
Those numbers are not fixed. R will be regenerated and can have completely different numbers if you change something. That is why when comparing ids, you compare by the name instead.
Eg instead of
if (i == 223344)
do
if (i == R.layout.image_1)
Since R.layout.image_1 references the integer id, the name won't change (unless you change the layout xml name.
If you want to get a resource id dynamically (by a string representing the name), you should have a look at this method - Resources#getIdentifier().
First of all we generally put images in the drawable folder.
Does Android generate same number every time?
No.
Are these numbers fixed whenever the application runs?
Yes.
In fact, once your project is built, the ids will remain the same for that same build.
In other words for a certain generated APK file, the ids won't change.
So how can you take advantage of that to group your resources?
You could have a static int array that holds the ids:
public static final int[] IMAGES_MORNING = {R.drawable.morning0, R.drawable.morning1, etc};
public static final int[] IMAGES_EVENING = {R.drawable.evening0, R.drawable.evening1, etc};
Although a more structured method would be to store them in a database on your app's first launch.
Or you could use what A--C suggests:
For example to get all the ids of morning images
for(int i = 0; i < numberOfMorningImages ; i++){
int id = getResources().getIdentifier("morning" + i, "drawable", getPackageName());
// do something with the id
}
No, there's no guarantee that integers will be the same every time, so the solution you've described won't work. Unfortunately, there's no proper way to group drawables inside the res/drawable folder. As a workaround, you can store them inside the assets folder, where you can group them as you like. However, Android won't be able to handle different resolutions this way. The choice is up to you. Hope this helps.

Shuffling cards and assigning them to user and computer in android development

I am creating a basic top trumps app in android development. I only need 6 top trumps card which have been made by using a database. I already have the code for this and have successfully created the cards.
I am stuck on this bit
When the user selects Play you should randomly assign half of the cards in the database to the human player, and half of the cards to the computer.
And was wondering if anyone had any ideas on how to do this? Thanks everyone.
UPDATE
I have tried doing this
ArrayList<Integer> cards = new ArrayList<Integer>();
for(int i=1;i<=6;i++)
{
this.cards.add(i);
}
Collections.shuffle(this.cards);
To shuffle the cards but I'm unsure on how to assign the computer and user the cards.
I'm not sure what you are aiming for here, but would something like this do (after you have shuffled):
ArrayList<Integer> playerCards = new ArrayList<Integer>(cards.subList(0,2));
ArrayList<Integer> computerCards = new ArrayList<Integer>(cards.subList(3,5));
Of course, you should use variables instead of constants everywhere, but you get the idea.
I am not an android programmer, but once long ago I realized the way to deal shuffled cards is to store the cards in an array or whatever in their natural order rather than a shuffled order -- for regular poker cards, 2-spades to A-spades, then 2-clubs to A-clubs, etc. Then, instead of dealing cards in-order from the shuffled deck, deal the cards by random draw from the unshuffled deck. You card elements need a "used" setting or value so they don't get reused or used twice. Saves LOADS of time doing the shuffle. In your case you on;y need to draw three cards, then the other guy gets the rest.

Finding free space on a PDF page on Android

I need some sort of an algorithm, that would try to find free space on a specified page of a PDF.
The space I'm looking for is a square 100x100 pixel large. I would like to start searching from the bottom right, move further left in a row, and then gradually move up rows, until I either find a suitable space (free-space/white-space), or return error, that there is no free space.
Anyone aware of such a possibility in Android? And if not, how could I implement it?
Edit
I have been doing quite a bit of research on this lately, and am I right in assuming, that finding a free spot is not possible, until the document is rendered? Because if I'm getting it, right, every vector and every object should be put in it's place, just to find the free spaces? Meaning, the places which no vectors intersect?
You definitely need to render the PDF. You can use something like poppler, and then search for white regions. Here is an open source Android app using poppler that you could modify/use as inspiration:
http://code.google.com/p/apdfviewer/
Once you have the rendered PDF, you can use something like the Boyer-Moore string search algorithm to find space. I.e. in 1D you are trying to find the string "......." in "+++...++.....+..........." where . = white and + = not white.
In 2D it will be more complicated, the most efficient thing I can think of is something like this:
Search along the row until you find sufficient space.
Go to the next row and search along it up to the point you were at in the row above.
If you find space before that, repeat step 2, then continue.
If you see what I mean... something like this:
void search(int row, int space_pos, int space_height_so_far)
{
for (int x = find_next_space(row, 0); x < space_pos; x = find_next_space(row, x+1))
{
search(row+1, x, 0);
}
if (is_space(row, space_pos))
{
if (space_height_so_far > ?)
cout << "Space found at " << row << ", " << space_pos << endl;
else
search(row+1, previous_space_pos, space_height_so_far);
}
}
Hmm I can see several things wrong with that code already.. but hopefully you get the idea. I expect that algorithm exists already but I don't know the name for it unfortunately.

Randomly Generating Sentences for Display

I have a few questions concerning the application I'm designing. I have many different routes I could try in order to get what I wanted, but I thought I would get suggestions instead of doing trial and error.
I'm developing a simple app that has one Game screen (activity) and a main menu to start the game. The game screen will have two buttons, "Next" and "Repeat". Every time "Next" is hit, a new sentence in a different language (with the english translation below it) will appear, audio will pronounce the sentence, and hopefully I can get a highlighter to highlight the part of the sentence being spoken. You can guess what the Repeat button does.
My question is, what do you guys think would be the best way to store these sentences so they can be randomly picked out? I thought about making an array of structures or classes with the English definition, audio, and sentence in each structure. Then using a random iterator to pick one out. However, it would take a long time to do this approach and I wanted to get some ideas before I tried it.
Also, I'm not sure how I would print the sentence and definition on the screen.
Thanks!
Using an array of structs/classes seems like that would be the normal way to go.
Not really sure what you mean by a random iterator, but when picking out random sentences from the array of sentences, you might want to avoid repeats until you've gone through all the elements. To do that, you can make a second array of indices, select one at random from those, use the element that corresponds to that index, and remove that number from the array of indices.
In Java, that would look something like
ArrayList<Sentence> sentences;
ArrayList<Integer> indices;
Random rand;
private void populateIndices() {
for(int i = 0; i < sentences.size(); i++)
indices.add(i);
}
public Sentence getNextSentence() {
if(indices.isEmpty())
populateIndices();
int idx = rand.nextInt(indices.size());
int val = indices.get(idx);
indices.remove(idx);
return sentences.get(val);
}
Quite frankly I would load out of copyright books from Project Gutenberg and randomly pull sentences from them. I would then pass the sentences into Google APIs to translate and pronounce the sentences. Relying on external services is at the very heart of what a connected OS like Android is made for. It would be a much more compelling use of the platform than a canned Rosetta Stone like CD solution and your ability to tap into a broader amount of content would be increased exponentially.

Categories

Resources