I am trying to fade two images back and forth:
Image1 with an id of imageone
Image2 with an id of imagetwo
For both the images, I have linked onClick in UI properties to the onClick method in the below Java code.
After the launching the app, when I click on image1, it is supposed to fade out and image2 should fade IN.
After analysis, I determined that the flow is not entering the if statement and is always getting directed to else no matter what.
How can I solve this?
Why is the if statement if (view.getId() == R.id.imageone) not letting the flow inside it even though the entry condition view.getId() == R.id.imageone is true?
public class MainActivity extends AppCompatActivity {
public void onClick(View view) {
ImageView imageone = (ImageView) findViewById(R.id.imageone);
ImageView imagetwo = (ImageView) findViewById(R.id.imagetwo);
if (view.getId() == R.id.imageone) {
imageone.animate().alpha(0f).setDuration(2000);//image1 fade OUT
imagetwo.animate().alpha(1f).setDuration(2000);//image2 fade IN
} else {
imagetwo.animate().alpha(0f).setDuration(2000);//image2 fade OUT
imageone.animate().alpha(1f).setDuration(2000);//image1 fade IN
}
}
}
#Narendra Jadon : You are right ,The problem was with the layout. The image two was on top of image one. And i solved it by setting the image one on top of image 2. Thank you everyone for your help.
Related
The Title says it all. drawline works outside the setOnClick Listener, but not inside, so I can set it to run with a button press.
//Bitmap Object creation
bitmap = Bitmap.createBitmap(600,800,Bitmap.Config.RGB_565);
//Create Canvas object and set to bitmap variable
canvas = new Canvas(bitmap);
canvas.drawColor(Color.BLACK);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(50);
imageView.setImageBitmap(bitmap);
up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
paint.setColor(Color.BLUE);
canvas.drawLine(100,100,100,200,paint);
}
});
Something else I want to look into is drawing multiple lines, and making sure each one lines up to the tip of the last one drawn, similar to this:
This will work for multiple buttons, but I only need it for one button right now. I hope this is sufficient information, please let me know if more is needed
I want to show an image when certain condition is met in the code.
My code is:
if((times-prt)/1000000 >1000) {
prt=times;
pushdata((lightIn > 150 ? 1 : 0));
// Log.d("Light amount: %02f", lightIn+"getArraydata= "+getArraydata() +"getArraydata()"+matchPattern());
if(matchPattern()) {
ImageView imageView = (ImageView) findViewById(R.id.new_logo);
imageView.setImageResource(R.drawable.new_logo);
// finish();
// System.exit(0);
}
}
Pattern is matching but no picture is showing.
Please, help me.
If your image view has visibility set to INVISIBLE or GONE in your layout.xml file, you will also need to call:
imageView.setVisibility(View.VISIBLE);
I want my ImageView to change it's drawable resource as it is pressed. The problem occurs when ImageView is pressed for the second time.
Let me explain, if ImageView is pressed first time, I want it to change from drawable A to drawable B. If ImageView is pressed again I want it to change from drawable B to drawable A.
That pressed again part is not working..
Here's my code:
public void imageViewBiljeskeNaListiCheckMarkMetoda(View view){
imageViewBiljeskeNaListiCheckMark = (ImageView) findViewById(R.id.imageViewBiljeskeNaListiCheckMark);
if (view == imageViewBiljeskeNaListiCheckMark){
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_obavljeno);
} else {
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_nije_obavljeno);
}
}
Remove this from the method....
You need to init the object once in the onCreate...
imageViewBiljeskeNaListiCheckMark = (ImageView) findViewById(R.id.imageViewBiljeskeNaListiCheckMark);
Then add a boolean variable to control the state of the view. .
public void imageViewBiljeskeNaListiCheckMarkMetoda(View view){
flag =!flag;
if (view == imageViewBiljeskeNaListiCheckMark){
if (flag) {imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_obavljeno);
} else {
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_nije_obavljeno);
}
}
I sugggest to use the "tag" of the view, and keep in the tag the information you need (e.g. if the view is pressed or not)
http://developer.android.com/intl/es/reference/android/view/View.html#setTag(java.lang.Object)
Can't you just use a toggle method like this?
private void toggleDrawableOnClick(){
/* now you can check to see if the set drawable is A using its id */
if(visible drawable is A){
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_nije_obavljeno);
}else{
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_obavljeno);
}
}
This should be easier I believe!!
I have an image button and want that when the user clicks on it-it changes its src to a different drawable-but i want the background color i defined in xml to remain the same. Here is the code that i have done so far, but doesnt work, because im changing background and not the source-but general concept i will use:
public void onClick(View view) {
if (bgenabled == true) {
holder.ib.setBackground(res.getDrawable(R.drawable.location_deactive));
bgenabled = false;
} else { holder.ib.setBackground(res.getDrawable(R.drawable.location_active));
bgenabled = false;}
Just call the setImageDrawable to replace the current image you are using
with your ImageButton.
ImageButton button;
...
button.setImageDrawable(getResources().getDrawable(R.drawable.new_image));
I'm using ViewPager as my main navigation in app. I'm using ActionBar, as well. What I want to achieve is that when user clicks search button in ActionBar I want to have blurred background. So my approach is to take a screenshot, blur it, set as ImageView and display as an overlay over the whole view. And it works. But problem is that actually when I first open search it displays proper screenshot. Then I turn overlay off, change page in ViewPager, click search again and ... I can see the previous screenshot- not new one with new page on it. Here are my snippets of code:
show and hide overlay on search click (I'm passing R.id.container view which is parent id of my content view, menuOverlay is my ImageView referenced from layout)
Here's my method that takes screenshot and makes it blurry
// Ad. 1
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
if(menuItem.getItemId() == R.id.action_search) {
menuOverlay.setImageBitmap(Utils.takeSnapshot(findViewById(R.id.container)));
menuOverlay.setVisibility(View.VISIBLE);
}
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
if(menuItem.getItemId() == R.id.action_search) {
menuOverlay.setVisibility(View.GONE);
}
return true;
}
});
// Ad. 2
public static Bitmap takeSnapshot(View v) {
v.setDrawingCacheEnabled(true);
v.buildDrawingCache();
Bitmap bm = v.getDrawingCache();
Bitmap scaled = Bitmap.createScaledBitmap(bm, bm.getWidth()/5, bm.getHeight()/5, false);
return Utils.fastblur(scaled, 5);
}
I;m using fast blur algorithm found somewhere here on StackOverflow which is quite fast.
Where's the problem? Why it happens?
ok, I found the answer. if you want to always have "fresh" screenshot you need to destroy cache after all operations:
v.setDrawingCacheEnabled(false);