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);
Related
I am making an offline daily image quote app.
How to implement random image from Notification after they clicked it? The location of the images are stored in the drawable resource folder. Any input will be much appreciated.
Drawable resource are Integers so you can create a list of ints and run a Random choose on it:
List<Integer> imgList = Arrays.asList(R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4); // add your images names
Random rand = new Random();
int pickedImg = rand.nextInt(imgList.size()); // picking random number between 0 to list size - 1
ImageView imageView = findViewById(R.id.your_image);
imageView.setImageDrawable(ContextCompat.getDrawable(requireContext(), imgList.get(pickedImg)));
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);
}
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.
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.
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.