ImageView click problem - Android - android

Can I make ImageView in android make some action when I click on that ImageView ?
For example, when I click on ImageView imgV that some panel be visible and when I click on other ImageView imgUV that he become unvisible .
imgV.setOnClickListener( new OnClickListener() {
public void onClick(View v)
{
int i=tbrSearchNear.VISIBLE;
tbrSearchNear.setVisibility(1-i);
tbrSearchCriterium.setVisibility(i);
}
});
It looks like that doesn't register click at all .

Maybe ImageButton is what you are looking for? http://developer.android.com/reference/android/widget/ImageButton.html

Setting visbility to 1-View.VISIBLE translates to 1-0; if you want to hide it, write something like:
imgV.setOnClickListener( new OnClickListener() {
public void onClick(View v)
{
// toggle visibility
int visibility=tbrSearchNear.getVisbility() == View.VISIBLE ? View.GONE : View.VISIBLE;
tbrSearchCriterium.setVisibility(tbrSearchNear.getVisbility());
tbrSearchNear.setVisibility(visbility);
}
});

Related

Android can not remove button

I am trying to remove a button when the button itself is tapped, I am trying the following:
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
tagsView.removeView(button);
}
};
}
This code is working, but when I add the following line of code:
editText.setText(button.getText());
The code stops working and the button does not get removed. I add it like so:
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
editText.setText(button.getText());
tagsView.removeView(button);
}
};
}
What is the problem here?
use this in your OnClick method
button.setVisibility(view.GONE);
Your code will look like this
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
editText.setText(button.getText());
button.setVisibility(view.GONE);
}
};
}
Or Try this
Button mybtn = (Button)findViewById(R.id.mybtn_id);
mybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mybtn.setVisibility(view.GONE); // or (view.INVISIBLE)
}
});
Depending on what you're trying to achieve, something like deejay proposed would work just fine. If want the button to hide, call button.setVisibility(View.INVISIBLE). However, if you are trying to dismiss it completely from the view hierarchy, call button.setVisibility(View.GONE).
just set button visibility to false
Obviously button.setVisibility(View.GONE) comes to mind but if it doesn't work you should look one level above for the source of the bug. Maybe you don't set OnClickListener you created to the button and hence nothing happens?

Android Studio - Change image when in different state

I want to have a favorite button on my app like in Gmail
So, when the user click on the star, the star became yellow, and when the user clicked it again, it turn back to normal
How can i make this happen with my custom image?
i have two images
when its not favorited (heart-grey.png)
and when its favorited (heart-red.png)
You can use visibilty to do this.You have to define the xml to have both images at the same postion(It can be done using Relative layout).
final ImageView play = (ImageView) findViewById(R.id.play);
final ImageView play2 = (ImageView) findViewById(R.id.play2);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Drawable playImage = play.getDrawable();
if (playImage.isVisible()){
play.setVisibility(View.GONE);
play2.setVisibility(View.VISIBLE);
mediaPlayer.start(); }
}
});
play2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Drawable playImage2 = play2.getDrawable();
if (playImage2.isVisible()){
play2.setVisibility(View.GONE);
play.setVisibility(View.VISIBLE);
mediaPlayer.pause();
}
If your star is an ImageButton you can do something like this :
starSelected.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//add a condition to detect if it is already a favorite or not
if(starSelected.getDrawable() == R.drawable.theimage2) {
starSelected.setImageResource(R.drawable.thenewimage);
}else{
starSelected.setImageResource(R.drawable.thenewimage2);
}
}
});
Hope it helps
In your java code create a boolean flag:
boolean isSelected = false
Set an onClickListener inside your onCreate for the star (ImageView, Button whatever it is). Inside onClick, check for the flag like this:
if (isSelected) {
// change image src to unselected
isSelected = false;
} else {
// change image src to selected
isSelected = true;
}
and also you can save the boolean state with SharedPreferences to make sure you get the correct state every time.

How to disable click listener on image view android

I am working on android application in which i am making click listener on image view. I just want to disable image listener, for example i have a edit button , without clicking edit button image view listener should be disabled.
image = (ImageView) findViewById(R.id.image);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (clickCount == 0) {
Toast.makeText(getApplicationContext(), "Enabled",
Toast.LENGTH_SHORT).show();
fName.setEnabled(true);
lName.setEnabled(true);
mailText.setEnabled(true);
mobileText.setEnabled(true);
mCond.setEnabled(true);
mNotes.setEnabled(true);
medication.setEnabled(true);
alReact.setEnabled(true);
dofo.setEnabled(true);
image.setEnabled(true);
editText.setText("Done");
clickCount = 1;
}
else if (clickCount == 1) {
Toast.makeText(getApplicationContext(), "Done",
Toast.LENGTH_SHORT).show();
fName.setEnabled(false);
lName.setEnabled(false);
mailText.setEnabled(false);
mobileText.setEnabled(false);
meond.setEnabled(false);
mNotes.setEnabled(false);
meation.setEnabled(false);
alReact.setEnabled(false);
doInfo.setEnabled(false);
image.setEnabled(false);
editText.setText("Edit");
updatingUser();
clickCount = 0;
}
}
});
just add it in onCreate Method
image.setEnabled(false);
Use this line into your XML:
android:clickable="false"
If you want To clear the onClick-listenener of the imageView. Simply call
myImageView.setOnClickListener(null);
Hope it helps.
Update: "Advantage" (depends on what you want") of setOnClickListener to null is that it will not change the background of the button like it does at setClickable(false) or setEnabled(false).
Because the button wants to show the user if he is in an enabled(false) or setClickable(false) mode.
If you don't want that, simply use my answere. :) hope it helps and gives a direction of what you want
Try This:
image.setClickable(false);
Use image.setClickable(false) to set imageview clickable disable
try this
image = (ImageView) findViewById(R.id.image);
image.setClickable(false);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
image.setClickable(true);
}
Hope this will help you friend :)

How to make a view visible when a button is clicked and make that view invisible when the button is again clicked?

I have a layout which is invisible when the activity starts.When I click on a button the layout becomes visible.My requirement is when I click the button for the second time, the layout should be invisible.I know this is a silly question but as I am a new to android, I am unable to figure it out.
Try the following code to toggle the visibility of the view:
v.setVisibility(v.getVisibility() == View.INVISIBLE ? View.VISIBLE
: View.INVISIBLE);
You can also implement by using boolean FLAG.
e.g. Declare
boolean visibility_Flag = false;
button..setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(visibility_Flag){
YourView.setVisibility(View.INVISIBLE);
visibility_Flag = false;
} else {
YourView.setVisibility(View.VISIBLE);
visibility_Flag =true;
}
}
});

change button image in android

I want to change the image of a button in my code. I found this can be done in xml:
please check this link
but the image will not stay on after I released the button. I want to click the button
and the button change to a new image. Can this be done?
in onClick method you need to change the Button's image, this way..
public void onClick(View v) {
if(v==buttonName){
buttonName.setBackgroundResource(R.drawable.imageName);
}
}
Assuming an ImageButton...
You can change the background image in an onClick listener, similar to the following:
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//set button image
myButton.setImageBitmap(myBitmapFile)
}
});

Categories

Resources