I'm writing a puzzle game to learn developing Android Apps. But now I'm stuck on how to add images dynamically to the ImageViews in the layout file (see below). I'm trying to make a loop where I add a random image to each of the ImageViews. But I can't find any examples on how to do this. My images are named the same as the ImageViews, only in lower case letters.
Or are there some other and better ways to solve this?
My code so far:
// Puzzle of 2x2 tiles
String[] sTiles = {"A0","A1","B0","B1"};
final Random myRandom = new Random();
// Random tiles
String tmp = null;
for (int i = 0; i < sTiles.length; ++i) {
tmp = sTiles[i];
int r = myRandom.nextInt(sTiles.length);
sTiles[i] = sTiles[r];
sTiles[r] = tmp;
}
// Lopp to add images randomly to the screen (layout/main.xml)
//for(i=0; i < sTiles.length; ++i) {
ImageView image = (ImageView) findViewById(R.id.B0);
image.setImageResource(R.drawable.a0);
//}
--------------- layout/main.xml ------------
<TableRow>
<ImageView
android:id="#+id/A0"
android:layout_column="1" />
<ImageView
android:id="#+id/A1"
android:gravity="right" />
</TableRow>
<TableRow>
<ImageView
android:id="#+id/B0" />
<ImageView
android:id="#+id/B1" />
</TableRow>
Thanks, Sigurd!
Try setting up a few int arrays to store your drawable and widget IDs. It's faster than using reflection to find them by name. Something like this should do the trick:
int[] imageViews = {
R.id.A0, R.id.A1,
R.id.B0, R.id.B1,
R.id.C0, R.id.C1 //...
};
int[] images = {
R.drawable.a0, R.drawable.a1,
R.drawable.b0, R.drawable.b1,
R.drawable.c0, R.drawable.c1 //...
};
Random random = new Random(System.currentTimeMillis());
for(int v : imageViews) {
ImageView iv = (ImageView)findViewById(v);
iv.setImageResource(images[random.nextInt(images.length - 1)]);
}
You may want to add some special handling if you want to ensure that all the ImageViews get unique images, but that should work as is.
An solution is read with reflection the values from R.drawable and load it as Bitmap by getting the value of the field in R.
It's something like:
public static void example() throws IllegalArgumentException, IllegalAccessException{
Class<R.drawable> clazz = R.drawable.class;
for(Field field : clazz.getDeclaredFields()){
Bitmap bmp = BitmapFactory.decodeResource(getResources(), (Integer) field.get(null));
//bmp is your image
}
}
You'll have to make some sort of container(Linear Or Relative layout possibly) and add the images to the view. Example:
RelativeLayout cbrl = new RelativeLayout(this);
RelativeLayout.LayoutParams cbp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
cbVal.setLayoutParams(cbp);
for(i=0; i < sTiles.length; ++i) {
ImageView image = (ImageView) findViewById(this);
image.setImageResource(R.drawable.xX);
cbrl.addView(iv);
}
Related
I have an ImageViewArray and I am randomizing their locations, I want to check if the current location already contains an ImageView without keeping a lot of values of x,y for every ImageView in the array.
Here is my code:
ImageView[] imageViewArray = new ImageView[40];
for (int i = 0; i < 40; i++) {
imageViewArray[i] = new ImageView(this);
imageViewArray[i].setTag(i);
imageViewArray[i].setImageResource(R.mipmap.enemy);
rlt.addView(imageViewArray[i]);
imageViewArray[i].setX(rand.nextInt(rlt.getWidth()));
imageViewArray[i].setY(rand.nextInt(rlt.getHeight()));
if(imageViewArray[i].getX()=) // here I want to check if it already contains an ImageView.
}
Possible Solution
Creating IntArray and adding X value to it and also every Y value for it, then compare between them, is it the best solution?
Problem with the solution - nothing happens, the imageview doesn't change the place and the Toast is not executed.
code:
ImageView[] imageViewArray = new ImageView[20];
ArrayList<Float> xarray = new ArrayList<>();
ArrayList<Float> yarray = new ArrayList<>();
for (int i = 0; i < 20; i++) {
imageViewArray[i] = new ImageView(this);
imageViewArray[i].setTag(i);
imageViewArray[i].setImageResource(R.mipmap.enemy);
imageViewArray[i].setX((float)rand.nextInt(1 + layoutwidth));
imageViewArray[i].setY((float)rand.nextInt(1 + layoutheight));
xarray.add(imageViewArray[i].getX());
yarray.add(imageViewArray[i].getY());
rlt.addView(imageViewArray[i]);
Toast.makeText(MainActivity.this,imageViewArray[i].getX() +"blabla",Toast.LENGTH_LONG);
}
EDIT
layoutwidth is zero :
private int layoutwidth, layoutheight, randx, randy;
private RelativeLayout rlt;
....
rlt = (RelativeLayout) findViewById(R.id.layout);
rlt.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
rlt.getViewTreeObserver().removeOnGlobalLayoutListener(this);
layoutwidth = rlt.getWidth();
layoutheight = rlt.getHeight();
}
});
Yes, that is the best solution. You need to load the rectangles from somewhere. You might merge rectangles if one contains the other, but then you would over-complicate your task and in your quest of writing a more performant and a clearer code, you would end up with a slow and complicated code. Write your code with storing pairs of X, Y points where X is the let-top corner position and Y is the right-bottom corner position.
Note, that I have assumed that the pictures are not rotated. If the images might be rotated, then you need a more general solution, using the inequalities defining the rectangles to see where a point set of a rectangle intersects the point set of the other rectangle. If the intersection is empty set, then the "space is not used up".
I want to set Imageview and Textview on same line in LinearLayout but the Imageview is always higher than the Textview.
Here is my code:
String b[] = cacBuocThucHien.split("#");
for (int j = 0; j < b.length; j++) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(30,30);
lp.setMargins(50, 0, 0, 0);
ImageView img = new ImageView(RecipesDetailActivity.this);
TextView tv = new TextView(RecipesDetailActivity.this);
img.setLayoutParams(lp2);
img.setImageResource(R.mipmap.testic);
tv.setLayoutParams(lp);
tv.setText(b[j] + "\n");
layoutHuongDan.addView(img);
layoutHuongDan.addView(tv);
}
This You can use to put image in one line.. and it will work in all kind of layouts. Hope this will help you
<LinerLayout>
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
....... // Put how many TextViews you want
<TextView
android:id="#+id/textn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinerLayout>
You can set visibility gone initially if you wanna show these textviews only after setting text.
Now in your activity make an array of ids of textviews like this...
public static int[] textRows = {R.id.text1, R.id.text2, ........ R.id.textn};
then use for loop for initializing them and setting text and images like this
TextView[] allTexts = new TextView[n];
for (int i = 0; i < n; i++) {
allTexts[i] = (TextView) findViewById(textRows[i]);
allTexts[i].setText("your text");
allTexts[i].setCompoundDrawables(left_image, null, null, null);
}
It will work. Try it out
set textview drawable left no need of extra imageview
android:drawableLeft="#drawable/img"
If you only want to show an icon beside that TextView, in my opinion you should consider using drawable icon feature of TextView
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
But if you would insist to using two views beside each other, using layout gravity would be useful in such circumstances.
lp.gravity= Gravity.CENTER_VERTICAL;
If you want to display only TextView and ImageView.. go for setting CompounDrawable in TextView itself..
But, for having two views side-by-side in LinearLayout programmatically try setting the orientation=horizontal, check out this link.
My activity has 9 image buttons, I would like to change certain properties of multiple image buttons like disable all the image buttons in a certain situation & then enable them back. I think may be i can use loop something like this....
// int [] ids = { R.id.imgBtn1, R.id.imgBtn2......, R.id.imgBtn9 };
// for (int i=0; i<=ids.length; i++){
// ids[i].setEnabled(true);
// }
Thank you all
define one list like follow:
List<ImageView> images = new ArrayList<>();
ImageView iv1 = (ImageView)findViewById(R.id.image1);
ImageView iv2 = (ImageView)findViewById(R.id.image2);
ImageView iv3 = (ImageView)findViewById(R.id.image3);
ImageView iv4 = (ImageView)findViewById(R.id.image4);
ImageView iv5 = (ImageView)findViewById(R.id.image5);
ImageView iv6 = (ImageView)findViewById(R.id.image6);
ImageView iv7 = (ImageView)findViewById(R.id.image7);
//.....
and put all in one list,
images.add(iv1);
images.add(iv2);
// add other view
then work with this list and do what you want like:
for (ImageView iv : images)
{
// your code
}
// Add views from array of ids
ArrayList<View> views = new ArrayList<>();
int [] ids = { R.id.imgBtn1, R.id.imgBtn2, R.id.imgBtn9 };
for (int i=0; i<ids.length; ++i){
views.add(findViewById(ids[i]));
}
// Loop each view and enable it
for (View view : views) {
view.setEnabled(true);
}
I'm trying to add multiple customo views (for now they are simple rectangles) using this code
//Defining the layout
HandLayout = new LinearLayout(this);
HandLayout.setOrientation(LinearLayout.HORIZONTAL);
HandLayout.setBackgroundColor(getResources().getColor(R.color.bg_hand));
HandLayout.setLayoutParams(new LayoutParams(getResources().getInteger(R.integer.dim_hand_width), getResources().getInteger(R.integer.dim_hand_height)));
//Some more code
int dx = getResources().getInteger(R.integer.dim_cardslot_width);
for (int i = 0; i < 6; i++){
Card c = new Card(this,i);
HandLayout.addView(c);
c.setX(i*dx);
}
The problem is that instead of getting 6 rectagles one next to the other I only see the first rectangle.
I think the rectangles are there but are "behind" the first drawn rectangle. How do I tell the view to "move" them dx to the right?
Thanks for any help
EDIT: The problem is with the Card Class code, I think. After a suggestion from one of the user I've tried adding TextViews and they worked. Here is the code for the card class (NOTE the only code not present are the declarations of a bunch of constant ints).
public Card(Context context, int id) {
super(context);
// TODO Auto-generated constructor stub
borders = new Rect(0,0,
getResources().getInteger(R.integer.dim_cardslot_width),
getResources().getInteger(R.integer.dim_cardslot_height));
offw = (getResources().getInteger(R.integer.dim_cardslot_width) - getResources().getInteger(R.integer.dim_card_width))/2;
offh = (getResources().getInteger(R.integer.dim_cardslot_height) - getResources().getInteger(R.integer.dim_card_height))/2;
cborders = new RectF((float)offw, (float)offh,
(float)getResources().getInteger(R.integer.dim_card_width),
(float)getResources().getInteger(R.integer.dim_card_height));
ntext = Typeface.create(Typeface.MONOSPACE,Typeface.NORMAL);
paint = new Paint();
}
protected void onDraw(Canvas canvas){
paint.setColor(getResources().getColor(R.color.bg_card));
canvas.drawRect(borders, paint);
paint.setColor(getResources().getColor(R.color.main_card));
canvas.drawRoundRect(cborders, rad, rad, paint);
paint.setColor(getResources().getColor(R.color.card_text));
paint.setTextSize(14);
canvas.drawText("Card: " + String.valueOf(ID),offw,TopOffset+CharHeight,paint);
}
They are not "behind", they are "next" to each other, the width of every view i think is fill_parent this is why you get only one view, try to put your parent layout inside a scrollView and check the difference
XML solution:
Let's say your card design is represented as follows
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
in your java code:
for (int i = 0; i < 6; i++){
View cardView = LayoutInflater.from(getActivity()).inflate(R.layout.xml_above, null);
TextView tv = cardView.findViewById(R.id.value));
tv.setText("your text");
//if you have click listeners
tv.setOnClickListener(...);
HandLayout.addView(cardView);
}
as simple as it looks
I'm trying to display a frame by frame animation and want to iterate through the drawables so I don't have to type all their names in case the number of frames increases.
However I can't seem to find how to iterate through the drawables. I have looked up a fair bit of java for loop tutorials but they all just printed stuff which (as far as I'm sure) don't have to use here.
Here's the relevant code (the image's names are dude1, dude2, ...):
private void startAnimation(){
animation = new AnimationDrawable();
for (int i = 1; i < 4; i++) {
animation.addFrame(getResources().getDrawable(R.drawable.dude(i)), 100);
}
animation.setOneShot(true);
ImageView imageView = (ImageView) findViewById(R.id.img);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80, 90);
params.alignWithParent = true;
params.addRule(RelativeLayout.CENTER_IN_PARENT);
imageView.setLayoutParams(params);
imageView.setImageDrawable(animation);
imageView.post(new Starter());
}
Thx!
I think the last answer is varely true but it may be:
for (int i=0; i <10;i++){
animation.addFrame(
getResources().getDrawable(getResources().getIdentifier("dude" + i,"drawable",
getPackageName()),100);
}
Try this. getResources() needs a context.
for (int i=0;i<10;i++){
animation.addFrame(getResources().getIdentifier("dude" + i,"drawable", getPackageName()),100);
}
Here, I have assumed 10 frames (i<10).
I have used Simon's answer to iterate through my drawables that are named "c1" through "c54" and placed in an array. Here is the code I just used that works for me.
private void getDeckDrawables() {
for (int i=1; i<53; i++){
intArrDeck[i-1] = getResources().getIdentifier("c"+i,"drawable",getPackageName());
}
}
Previously, I typed them manually which took up too much room for my taste.
private void getDeckDrawables() {
intArrDeck[0]=R.drawable.c1;
intArrDeck[1]=R.drawable.c2;
intArrDeck[2]=R.drawable.c3;
}