i have a simple game ..theres a snow move down and a penguin in bottom layout ..we must protec the penguin from falling snow
And i want to ask you
how we can destroy the imageview (snow pictures) when we click it?
i set:
lebah2.setVisibility(View.GONE);
but it just hidding ..because i create a timer to check the collision ..when the snow collision with penguin so the live is minus one ..if i put this code, the timer will always decrease live ..
for(int awal = 1 ; awal<=akhir; awal++)
{
ImageView lebah2 = (ImageView) findViewById(awal);
int atas = lebah2.getTop();
int left = lebah2.getLeft();
if(atas >=180)
{
if(left >180 && left <240)
{
data.live--;
TextView nyawa = (TextView) findViewById(R.id.live);
nyawa.setText(String.valueOf(data.live));
lebah2.setVisibility(View.GONE);
}
}
what must i do?
You can remove the view like this:
((LinearLayout)lebah2.getParent()).removeView(lebah2);
you can also just skip the loop if the view is not visible:
if(iv.getVisibility() == View.VISIBLE) {
for(int awal = 1 ; awal<=akhir; awal++) {
//Check if collision
}
}
The view is not removed when you set the visibility as GONE. For exemple If you get all childs for a parent the view will be counted.
Use This:
ImageView lebah2 = (ImageView) findViewById(R.id.lebah2);
((RelativeLayout)lebah2.getParent()).removeView(lebah2);
Related
I have a layout which is something like this:
LinearLayout (linearLayout)
'--TextView (textView1)
'--ImageView (imageView)
'--TextView (textView2)
textView1 changes its text sometimes and it can be long, so it leaves part of textView2 out of the screen. I want to prevent that, so I want to remove imageView from the layout whenever this happens. imageView may or may not be visible at the time when this is computed (maybe it was removed before when textView1 was edited previously).
This is what I have coded:
void changeText(String veryLongString){
textView1.setText(veryLongString);
int [] loc = new int [2];
textView2.getLocationOnScreen(loc);
int bottom = textView2.getMeasuredHeight() + loc[1];
if (imageView.getVisibility() == View.GONE)
bottom += imageView.getHeight();
if (bottom > linearLayout.getMeasuredHeight()){
imageView.setVisibility(View.GONE);
} else {
imageView.setVisibility(View.VISIBLE);
}
}
But for some reason this doesn't work as expected, because it seems as if changes in the position and height of the Views don't take place immediately. When I call getMeasuredHeight() and getLocationOnScreen() I get the values BEFORE the changes I have just made. The result that I get is that if I set a very large text imageView is not removed, but if I then set a short text, it is removed.
If there any other way to face this problem?
Even though I think that this is not the right approach (you can do all kinds of stuff in your XML so you don't have to meddle with Java code), here is a quick example of what you can do from Java (for example, in your onStart() method)
ViewGroup group = (ViewGroup) findViewById(R.id.myLayout);
int groupHeight = group.getHeight();
for (int i = 0; i < group.getChildCount(); i++) {
groupHeight -= group.getChildAt(i).getHeight();
if (groupHeight < 0) {
// they don't fit in the layout
myImageView.setVisibility(View.GONE);
}
}
I'm trying to set up a layout at runtime that alternates from having a button on the left side of the screen on then the right side. I have an arrayList of button strings and it iterates through, creating a button for each and then applying some styling. Almost all the styling works, except that the margins I'm using to push them to the side of the screen aren't alternating correctly. I'm trying to make the margin push either from the left or the right, but it seems like the buttons are staying within only one column.
Here's the code first of all:
LayoutParams noteStyle = new LayoutParams((int) getResources().getDimension(R.dimen.sticky_note_height),
(int) getResources().getDimension(R.dimen.sticky_note_width));
int margin = (int) getResources().getDimension(R.dimen.margin_huge);
layout = (LinearLayout) findViewById(R.id.note_layout);
int i = 0;
for (String note : notes){
Button btnTag;
if (i % 2 == 0){
btnTag = (Button) getLayoutInflater().inflate(R.layout.sticky_note_right, null);
noteStyle.setMargins(margin,0,0,0);
} else {
btnTag = (Button) getLayoutInflater().inflate(R.layout.sticky_note_left, null);
noteStyle.setMargins(0,0,margin,0);
}
btnTag.setLayoutParams(noteStyle);
btnTag.setText(note);
btnTag.setId(i);
layout.addView(btnTag);
((Button) findViewById(i)).setOnClickListener(this);
i++;
}
And here's a screenshot of how it comes out:
For some reason unknown to me, reusing the LayoutParams can cause goofy results. Instantiating them each time they are needed can help resolve this.
This means putting them inside the for loop, in this situation
for (String note : notes) {
LayoutParams noteStyle = new LayoutParams((int) getResources().getDimension(R.dimen.sticky_note_height),
(int) getResources().getDimension(R.dimen.sticky_note_width));
I have 15 ImageView where I assign a 'shape' as background. What I want to achieved is when I click these ImageViews, the shape's stoke property will change from 0dp to 4dp. But when I clicked the ImageView again, it will turn back to 0dp. I also want to limit the number of ImageView that can be clicked. For example, If I have clicked 8 ImageView already (that changes their stroke property from 0dp to 4dp), it won't allow me to change others ImageView stroke property anymore, unless I clicked one ImageView (that will removed it's 2dp stroke and will reset it to 0dp).
This is the code I have so far. Which allows me to click the ImageViews and change their stroke property to 4dp. I am googling/stackoverflow for solutions but can't find one. Hope you can help me.
private int[] colors = {R.color.filter_dark_blue, R.color.filter_rouge, R.color.filter_blue, R.color.filter_burgundy,
R.color.filter_turquoise, R.color.filter_navy, R.color.filter_green, R.color.filter_black,
R.color.filter_yellow, R.color.filter_charcoal, R.color.filter_orange, R.color.filter_grey,
R.color.filter_warm_red, R.color.filter_white, R.color.filter_pink};
private ImageView[] color = new ImageView[colors.length];
for (int i = 0 ; i < color.length ; i++) {
color[i] = (ImageView) findViewById(ivCirclesId[i]);
final GradientDrawable ivCircleOnClick = (GradientDrawable) color[i].getBackground();
color[i].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ivCircleOnClick.setStroke(4, Color.WHITE);
}
});
}
here is snippet,
some quick ideas ...
1- use the setTag(Object) and getTag() to set 0/1 value, as clicked flag
color[i] = (ImageView) findViewById(ivCirclesId[i]);
final GradientDrawable ivCircleOnClick = (GradientDrawable) color[i].getBackground();
//add this
color[i].setTag("0");//not clicked when created.
2- use global variable as counter for total clicked images, to control the limit 8 issue, if its set to 4 then increment the counter ++counter, if reset to 0, decrement the counter --counter, and change setTag()
public void onClick(View v) {
if(v.getTag().toString().equals("1")){
--clicked;
v.setTag("0");
ivCircleOnClick.setStroke(0, Color.WHITE);
}else{
if(clicked >= 8){
//max limit reached, nothing to do... add toast if you want ...
return;
}
++clicked;
v.setTag("1");
ivCircleOnClick.setStroke(4, Color.WHITE);
}
}
I would like to change an image each time the user touches the screen. I would like to have user tap the screen and the image changes each time and at the 5th touch it would loop back to the original image with added text.
I have looked at
How would I set an ImageButton to change only when my finger is touching it
I have tried both IUevents and StateListdrawable. I couldn't figure out how to actually change the image after each touch (anywhere in the screen).
And why can't you use the normal ImageView? Do this:
ArrayList<Integer> picList = new ArrayList<Integer>();
// populate the list with the int value of the pictures in drawable.
picList.add(R.drawable.apple);
picList.add(R.drawable.horse);
//etc
int touches = 0;
int loop = 0;
ImageView picture = (ImageView) findViewById(R.id.your_picture);
picture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (touches < 4)
v.setBackgroundDrawable(picList.get(touches));
touches++;
} else {
touches = 0;
loop = 1;
v.setBackgroundDrawable(picList.get(touches));
}
if (loop = 1){
// Create a TextView and set the text or make it in the xml with Visible="Gone" and make is visible here.
}
});
you need to create the Custom class which extends the ImageView now display this imageview to whole screen.
> First store the path/location of images into an array/list etc.
> In custom class implement the onTouch event.
> create the counter object in this custom class.
> now in onTouch check in down event that check the counter value with array size if counter==array.length()-1 then set counter as 0 otherwise increment in counter object.
> now get the path of the image and set into the imageview as background
I am trying to use a ViewFlipper and make it act like the home screen(The layout will move with your finger). Check out this for an example. I want to do this with a ViewFlipper that only contains two children so the opposite view should be shown on either side of the current view depending on which way the user moves their finger. This code works but only for 1 direction at a time. This is in onTouchEvent.
case MotionEvent.ACTION_MOVE:
leftView.setVisibility(View.VISIBLE);
rightView.setVisibility(View.VISIBLE);
// move the current view to the left or right.
currentView.layout((int) (touchEvent.getX() - oldTouchValue),
currentView.getTop(),
(int) (touchEvent.getX() - oldTouchValue) + 320,
currentView.getBottom());
// place this view just left of the currentView
leftView.layout(currentView.getLeft() - 320, leftView.getTop(),
currentView.getLeft(), leftView.getBottom());
// place this view just right of the currentView
rightView.layout(currentView.getRight(), rightView.getTop(),
currentView.getRight() + 320, rightView.getBottom());
Which ever of the bottom two lines I put last that direction will work correctly but the other will not.
Here is how I set the leftView and rightView:
final View currentView = myFlipper.getCurrentView();
final View leftView, rightView;
if (currentView == meView) {
Log.d("current layout: ", "me");
leftView = youView;
rightView = youView;
} else if (currentView == youView) {
Log.d("current layout: ", "you");
leftView = meView;
rightView = meView;
} else {
leftView = null;
rightView = null;
}
Is it going to be possible to set it up so that the same view is shown on both sides of the current view?
Thanks stealthcopter
That worked here is the new code if anyone is interested.
if (touchEvent.getX() < oldTouchValue){
// place this view just right of the currentView
rightView.layout(currentView.getRight(), rightView.getTop(),
currentView.getRight() + 320, rightView.getBottom());
}else if (touchEvent.getX() > oldTouchValue) {
// place this view just left of the currentView
leftView.layout(currentView.getLeft() - 320, leftView.getTop(),
currentView.getLeft(), leftView.getBottom());
}
I also moved the setVisibility() calls to the MotionEvent.ACTION_DOWN in an attempt to get rid of some flickering of the views. This helped but I still get a bit.
I have possibly not very constructive suggestion, but if you want it to behave like a home screen, why you don't want to look at the src of that, and modify it to your needs ?
To get rid of the flickering, set setVisibility(View.INVISIBLE) in MotionEvent.ACTION_DOWN. The default value is "GONE" on a view. That means that you can not set Layout() on a view until it's either "VISIBLE" or "INVISIBLE". So in three steps:
Set Visibility to INVISIBLE on the View
Set Layout() on the View
Set Visibility to Visible on the View
If I understand your request, this effect should now be implemented using a View Pager
Looks like this: