Displaying Images(numbers) to screen - android

I am working on a project that will display math problems to the screen such as:
10 + 5 =
and then the user will need to guess the answer. I am doing this as more of way for me to learn how android ticks.
I have images from 0 to 9 saved in the drawable folder for each of the dpi setting I need to account. I also have the operators (+,-,*,/,=) also saved.
My questions:
How easy is this to do?
How would I go about doing the above dynamically?
Thanks
--EDIT--
The images and operators are stored as .9.png files in my drawable folder. I have string-array contain my problems that I will randomly pull from and then display them to the screen.
In the past I would do the following:
public View buildProblem()
{
LinearLayout rtnView = new LinearLayout(ctx);
rtnView.setOrientation(LinearLayout.HORIZONTAL);
rtnView.setLayoutParams(new GridLayout.LayoutParams());
// Build LeftSide
Iterator<Integer> itor = buildLeftSide();
ImageView iv;
// loop through of iterator
while(itor.hasNext())
{
iv = new ImageView(ctx);
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
iv.setImageResource(itor.next());
itor.remove();
rtnView.addView(iv);
}
// Space
rtnView.addView(space);
// Operator
rtnView.addView(plusSign);
// Build LeftSide
itor = buildRightSide();
// another loop
while(itor.hasNext())
{
iv = new ImageView(ctx);
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
iv.setImageResource(itor.next());
itor.remove();
rtnView.addView(iv);
}
// Space
rtnView.addView(space);
// Equal Sign
rtnView.addView(equalSign);
// Space
rtnView.addView(space);
// return the view
return rtnView;
}
I was thinking of using ImageViews for the numbers to be displayed to the screen which I have used in the past but not sure if that is the best way to do it.
https://github.com/barakisbrown/MathTest -- Is one attempt at doing the above but I have now started to rewrite it from scratch so which is why I am asking for help.

Related

Is there a way to set the image from the drawable folder with my Json data, The data includes the name of the image in the drawable folder

So far I have this without crashing.
String logoImage = JO.getString("logo");
if (JO.getString("logo") == "able")
{
channel.ivLogoPic.setImageResource(R.drawable.able);
}
The problem is, It's not changing the image.
I've tried the .setDrawable and that didn't work either.
String logoImage = JO.getString("logo");
if (JO.getString("logo") == "able")
{
//int drawable1 = R.drawable.abc;
channel.ivLogoPic.setDrawable(R.drawable.able);
}
Though [ #sunil sunny ] has already mentioned a helping link, I would also like to add some solution here that could be handy.
Remember, the efficiency of this solution, in cases like yours, will decrease with increasing number of drawable resources. Meaning, do this if there are not many drawables to use.
What you can do is first get the drawables as global variables in the recyclerview adapter:
int drawable1 = R.drawable.drawable1, drawable2 = R.drawable.drawable2 ... etc.
Then inside onBindViewHolder() simply use the resources depending upon what is obtained from the JSON response.
Like:
if(logo == 1)
{
logoImageView.setDrawable(drawable1);
}
One advantage to note of this is you can change drawables easily, and not have to resort to changing names in both resource file as well as the API. Meaning, decouples the two.
PS: The codes written here are for reference only.
i figured it out in my backgroundTask.Java file I have
while (count<jsonArray.length())
{
JSONObject JO = jsonArray.getJSONObject(count);
count++;
Channel channel = new Channel(JO.getString("name"),JO.getString("logo"));
Channel logoImage = new Channel(JO.getString("logo"));
if (logoImage.equals("axle"))
{
channel.ivLogoPic.setImageResource(R.drawable.axle);
Then I had to set the change in the Adapter
if(channel.getLogo().equals("axle")){
holder.ivLogoPic.setImageResource(R.drawable.axle);
}

Android load images randomly and uniquely

I have tried the following code. It loads images quite randomly but some images appear to be the same. Can my code be modified to have all the images uniquely and randomly loaded?
int[] imageViews = {
R.id.ImageView2, R.id.ImageView2,
R.id.ImageView3, R.id.ImageView4,
R.id.ImageView5, R.id.ImageView6,
R.id.ImageView7, R.id.ImageView8,
R.id.ImageView9
};
int[] images = {
R.drawable.m1, R.drawable.m2,
R.drawable.m3, R.drawable.m4,
R.drawable.m5, R.drawable.m6,
R.drawable.m7, R.drawable.m8,
R.drawable.m9
};
Random random = new Random(System.currentTimeMillis());
for(int v : imageViews) {
ImageView iv = (ImageView)findViewById(v);
iv.setImageResource(images[random.nextInt(images.length - 1)]);
check about this solution , this was worked for me..Random images without repetition
Edit: Remove the images from the array as you're displaying them. Recalculate your random bounds to use a shorter array bound.

Changing image view background dynamically

I have a a set of 10 imageviews in my layout. I have given them sequential id's also as
android:id="#+id/pb1"
android:id="#+id/pb2"
Now I want to change background dynamically.
int totalPoi = listOfPOI.size();
int currentPoi = (j/totalPoi)*10;
for (i=1;i<=currentPoi;i++) {
imageview.setBackgroundResource(R.drawable.progressgreen);
}
Now inside the for loop I want to set the image view background dynamically. i,e if the currentpoi value is 3, background of 3 image views should be changed. What ever the times the for loop iterates that many image view's background should be changed. Hope the question is clear now.
Note : I have only 1 image progressgreen that need to be set to 10 image views
Finally I did this in the following way,
I placed all the id's in the array as
int[] imageViews = {R.id.pb1, R.id.pb2,R.id.pb3,R.id.pb4,R.id.pb5,R.id.pb6,R.id.pb7,R.id.pb8,R.id.pb9,R.id.pb10};
Now:
int pindex = 0;
for (pindex; pindex <currentPoi; pindex++) {
ImageView img = (ImageView) findViewById(imageViews[pindex]) ;
img.setImageResource(R.drawable.progressgreen);
}
Now, I am able to change the images dynamically.
#goto10. Thanks for your help. I will debug your point to see what went wrong in my side
Create an ImageView array:
ImageView views[] = new ImageView[10];
views[0] = (ImageView)findViewById(R.id.pb1);
...
views[9] = (ImageView)findViewById(R.id.pb10);
Now iterate the loop to set the background of images like this:
for (i=1;i<=currentPoi;i++)
{
views[i-1].setBackgroundResource(R.drawable.progressgreen);
}
you can do this by setting the name of drawables something like:
img_1, img_2, img_3...
for (i=1;i<=currentPoi;i++)
{
ImageView imageview=(ImageView) findViewById(getResources().getIdentifier("imgView_"+i, "id", getPackageName()));
imageview.setImageResource(getResources().getIdentifier("img_"+i, "drawable", getPackageName()));
}
Try this code.....
Create image Array..
private Integer[] mThumbIds = { R.drawable.bg_img_1, R.drawable.bg_img_2,
R.drawable.bg_img_3, R.drawable.bg_img_4, R.drawable.bg_img_5 };
And than modify your code
int totalPoi = listOfPOI.size();
int currentPoi = (j/totalPoi)*10;
for (i=1;i<=currentPoi;i++) {
imageview.setBackgroundResource(mThumbIds[i]);}
You could make an array of your ImageViews and then change them in your for loop.
ImageView views[] = new ImageView[10];
views[0] = (ImageView)findViewById(R.id.imageView0);
...
views[9] = (ImageView)findViewById(R.id.imageView9);
and then change your for loop to:
for (i=1;i<=currentPoi;i++) {
views[currentPoi].setBackgroundResource(R.drawable.progressgreen);
}
Arrays start at index 0, so make sure there's not an off-by-one error in here.
You'll need to give your ImageViews sequential ids, such as "#+id/pb1" and "#+id/pb2", etc.. Then you can get each of them in the loop like this:
for (i=1;i<=currentPoi;i++) {
// Find the image view based on it's name. We know it's pbx where 'x' is a number
// so we concatenate "pb" with the value of i in our loop to get the name
// of the identifier we're looking for. getResources.getIdentifier() is able to use
// this string value to find the ID of the imageView
int imageViewId = getResources().getIdentifier("pb" + i, "id", "com.your.package.name");
// Use the ID retrieved in the previous line to look up the ImageView object
ImageView imageView = (ImageView) findViewById(imageViewId);
// Set the background for the ImageView
imageView.setBackgroundResource(R.drawable.progressgreen);
}
Replace com.your.package.name with your application's package.

How can I use images as numbers/letters in Android?

I am developing an app where I need to show a count. This count is going to show for example a "4", then a "4 + 1", then finally a "5". I have .png images that I want to use as background to the numbers, so that I can easily show anything up to "99" if I need to.
So basically, I would like to assign a drawable background to a number. Is a custom font the way to go? Here typeface and view group with assigned strings is mentioned. Anyone tried this?
Actually you just need an array of size 10:
Resources res = getResources();
Drawable numbers = new Drawable[10]{
new Drawable(res, R.drawable.zero),
new Drawable(res, R.drawable.one),
new Drawable(res, R.drawable.two),
...
new Drawable(res, R.drawable.nine)
};
and to get them just use:
int i = 5;
Drawable image = numbers[i];
If the number has 2 digits then you can put in 2 pictures etc.
You can also make a very big array, but that would honestly be a waste of lines.
Also the space required when you only use 10 pictures is significantly smaller
HashMap<char, Drawable> charMap = new HashMap(Character, Drawable>();
Resources res = getResources();
charMap.put('0', new Drawable(res, R.drawable.img_0));
charMap.put('1', new Drawable(res, R.drawable.img_1));
// the rest of the characters you want to use
When you want to retrieve the correct image for a character:
char c = '0';
Drawable charImage = charMap.get(c);

android dynamical binding

I want to work dynamically therefore I want to bind text views dynamically I think an example would explain me the best
assuming I want to bind 7 image views i can do it like this :
Country = (EditText)findViewById(R.id.CountryEditText);
City = (EditText)findViewById(R.id.CityEditText);
LivinigCreture = (EditText)findViewById(R.id.LivingCretureE);
Nature =(EditText)findViewById(R.id.NatureEditText);
Inanimate = (EditText)findViewById(R.id.InanimateEditText);
KnowenPersonality = (EditText)findViewById(R.id.KnowenPersonalityEditText);
Occupation = (EditText)findViewById(R.id.OccupationEditText);
but lets change 7 with NUMOFFILEDS as a final where i want to do the previous ?
myImages = new ImageView [7];
for (int i = 0; i<7;i++,????)
myImages[i] = (ImageView)findViewById(R.id.initialImageView01);
notice : in my R file the R.id.initialImageView01 - R.id.initialImageView07 are not generate in a cont gap between them therefore I don't know how to make this architecture possible .
and if there's a way can someone show me an example how to work dynmiclly (like using jsp on android combined way or something ?)
id its possiable to do so constant times is it possible to build an the same xml constant num of times like jsp does
thank u pep:)
You can store the IDs themselves in an array at the beginning of your Activity; that way you'll only need to write them once and you can index them afterwards.
Something like:
int[] initialImageViewIds = {
R.id.CountryEditText,
R.id.CityEditText,
R.id.LivingCretureE,
R.id.NatureEditText,
R.id.InanimateEditText,
R.id.KnowenPersonalityEditText,
R.id.OccupationEditText
};
Then you can access them with:
myImages = new ImageView [7];
for (int i = 0; i<7;i++) {
myImages[i] = (ImageView)findViewById(initialImageViewIds[i]);
}
If that's not enough and you really want to get the IDs dynamically, I suppose you can use reflection on the R.id class, possibly with something like R.id.getClass().getFields() and iterate on the fields to check if their names interest you. Check reference for the Class class, too.

Categories

Resources