ImageView on again click - java

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!!

Related

Unable to enter the if statement | Android Studio

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.

How to check image resource of an imageview programmatically?

I want to check background of my imageview and do sth if it equals to some drawable in my drawable folder. How can I do that? I have try this code but no answer:
layoutRoot.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (view.getBackground() == R.drawable.grid_single_bg_selected) {
//Do sth
}
}
});
When you set the background drawable also do set a tag to the imageview
imageView.setTag(R.drawable.demo);
And then compare
if (((Integer) imageView.getTag()) == R.drawable.demo){
// Do stg
}
If you are setting the imageView programmatically, while setting, also set a tag to this imageView. Now, when you want to find out the drawable, you can do it by retrieving the tag of this imageView.

How to toggle picture src of ImageButton

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));

screenshots don't change when layout changes

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);

Adding a Button on a View

I am going nuts over this.
I did not find any working solution (tried a few from stackoverflow)
Scenario (this is an actual screenshot what is already done):
I have a Activity that has a View as his Attribute.
This view adds another view via View.addView(myView).
I now want to add a Button to myView (to be specific: after MotionEvent.ACTION_UP the button should appear in the right lower corner (this will start the robot to drive the track))
Here is a shortcut of my code:
public class ModeRouting extends View {
public ModeRouting(Context context) {
super(context);
Button asuroStartButton = new Button(context) //does not work
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int actionevent = event.getAction();
if (actionevent == MotionEvent.ACTION_UP
|| actionevent == MotionEvent.ACTION_CANCEL) {
asuroStartButton.visible=true;
view.add(asuroStartButton);
}
return true;
}
}
and my Activity:
//in constructor
contentView = (FrameLayout) findViewById(R.id.content);
onClickListenerFacade(routingMode, route);
//this removes all views from stack and places the new one on the view
private void onClickListenerFacade(View v, final View target) {
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
contentView.removeAllViews();
contentView.setBackgroundColor(0xff000000);
contentView.addView(target);
modeSelectorAnimation();
}
});
}
I tried to create a button in my mainactivity.xml and instantiate in my mainactivity.
I am missing some point in here but i am not sure which.
Since my view is purely dynamic (no layout.xml) i dont think i should use a layout.xml (maybe thats my mind-blockage) but instead set the button attributes dynamically too.
Any hint is appreciated!
You want to extend ViewGroup rather than just a View (LinearLayout, RelativeLayout, FrameLayout, etc) - they handle child views for you.
I think maybe you need to refresh the whole view/activity. Try to do this in the onResume methode, maybe this helps. But as you don't use a layout.xml, I'm not sure if this helps you much..
#Override
protected void onResume(){
super.onResume();
setContentView(R.layout.activity_main);
}

Categories