How to toggle picture src of ImageButton - java

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

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.

ImageView on again click

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

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

Display different Button/Button Background depending on SharedPreferences Value

Basically i want a Button to change to another one depending on the int Value stored in sharedprefs.
I have stage select in my game, if the user got enough score in level, then he can start the next one and i want to change the Button depending on that.
I'm setting up my Button's with custom Background created in XML Selector file located in drawable folder.
After that how i can call it in Java?
I tried a bit with if statement but cant find the right solution.
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
Button button = (Button) findViewById(R.id.button_id);
if (settings.getBoolean("nextLevel", true) {
button.setText("Level_2");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
else {
//Everything should be the same.
}
Check also the developer docs concerning SharedPreferences in android: http://developer.android.com/guide/topics/data/data-storage.html#pref

Show description when click on image

Sorry for my English!
Need help!
I make some program, who show random image after button click.
How i can make description for each image after user click on it ?
Program show random image, and how i can make description for every image in new window when user click on it ?
Well you can use the setOnClickListener method on your button to do what you want.
final TextView label = (TextView)findViewById(R.id.some_text_label);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
label.setText("Hello, World!");
}
}
Here's a tip.
Use a list or hashmap and create a custom class to hold image and image data. (Changes by a commentor)
Use Math.Random();//Random the id numbers
Stuff to learn:
Research button events and view flipper events
Here's an example:
My idea for using weakhashmap and
for improvements I recommend saving location instead of the bitmap(wastes memory).
private WeakHashMap<int, Image> whp = new WeakHashMap<Int, Image>
private int position = 0;
public void onCreate{
//Do download information or store images from sd card
//Place it in image class
loop amount of images{
image Image = new image(Bitmap_Image, Image_Description);//load image
whp.add(position, Image);//Note the amount of positions the image is field
position++;
}
//To get bitmap use "whp.get(position).image" <- This code returns a bitmap
setOnClickListener//Create event to listen on clicked image
{
int image_random = new Random().nextInt(position);
String data = whp.get(image_random).note_item;//Image Description
}
}
class image{
Bitmap image;//Actual Image
String note_item;//Image description
public void image(Bitmap intake_image, String intake_description){
this.image = intake_image;
this.note_item = intake_description;
}
}

Categories