I am facing an issue with the space between the views. Using a LinerLayout and dynamically adding views(ImageViews) to the linear layout.
I want to adjust the space between the imageview. I want them to have an overlap - when a new view is added.
Currently a blue back ground is applied to highlight the two linear layouts.
code to add Image view to the existing layout
dealerImages= (LinearLayout) findViewById(R.id.dealerImages);
dealerImages.setBackgroundColor(Color.BLUE);
ImageView view = new ImageView(BlackJack.this);
view.setImageResource(R.drawable.back);
dealerImages.addView(view);
Every time I add a new view - I want to specify the relative position to the old view. I want the new view to start from the center of the last view in the layout.
Please let me know if you need any additional details. Please suggest if I need to use any other layout to make things easier.
Edit - Posting the code here
playerLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.MATCH_PARENT);
dealerLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.MATCH_PARENT);
private void dealTwoCardsEach(){
player.addCard(hit());
ImageView imageView = new ImageView(BlackJack.this);
imageView.setImageResource(getResourceId(player.getFirstCard()));
playerImages.addView(imageView, player.getCards().size()-1);
dealer.addCard(hit());
imageView = new ImageView(BlackJack.this);
imageView.setImageResource(getResourceId(dealer.getFirstCard()));
dealerImages.addView(imageView);
player.addCard(hit());
if(player.getCount() == 21)
player.setBlackJack(true);
imageView = new ImageView(BlackJack.this);
imageView.setImageResource(getResourceId(player.getSecondCard()));
updateMarginForPlayer(); // updating start margin
playerImages.addView(imageView, player.getCards().size()-1, playerLayoutParams);
dealer.addCard(hit());
}
private void updateMarginForPlayer(){
playerLayoutParams.setMarginStart(playerLayoutParams.getMarginStart()+100);
}
Please not i am not setting any margin for the first card for the player. I can see both the cards until here. First Image with start margin of 0 and the second Image with start margin of 100.
private void handleHit(){
Card c1 = hit();
player.addCard(c1);
ImageView imageView = new ImageView(this);
imageView.setImageResource(getResourceId(c1));
updateMarginForPlayer();
playerImages.addView(imageView, playerLayoutParams);
}
On click of the 'Hit' button is when handleHit() gets called. and the new images being added are making all the images from 2nd till the currrent view invisible. I can only see the first and last(latest added).