How to check which image is set in imageview android - android

I have a star image in action bar. When I click on it, then the image should change to ON star. This is working fine. But how to know whether image is at ON state or OFF state.I want something that, if the image is at OFF mode and user taps on ON star, then it should set to ON star image. Initially, the image is set to OFF mode. So for this, I've wrote give line to turn on as user tap on it :
v.setBackgroundResource(android.R.drawable.star_big_on);
Guys pls suggest me for OFF mode if star image is already on. I am not getting any idea.

you can check it easily by setting tag of each image or by comparing there resource
comparision of resource method is shown below:
if (regProfile.getDrawable().getConstantState() ==
getResources().getDrawable(R.drawable.ivpic).getConstantState()){
Toast.makeText(_con, "Image is ivPic", Toast.LENGTH_LONG).show();
} else{
Toast.makeText(_con, "Image isn't ivPic", Toast.LENGTH_LONG).show();
}

If my guess is correct you can achieve the functionality like this.change this code accordingly for image onclick
private Button button;
public static boolean isclick=false;
button.setOnClickListener(seathtlistner);
private View.OnClickListener seathtlistner = new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(isclick){
button.setBackgroundResource(R.drawable.onstarimage);
}else{
button.setBackgroundResource(R.drawable.offstarimage);
}
isclick=!isclick;
}

You can use Tag property.
img_view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Boolean tag_isOn = (Boolean) v.getTag();
tag_isOn = !tag_isOn;
v.setBackgroundResource(tag_isOn ?android.R.drawable.star_big_on:android.R.drawable.star_big_off);
v.setTag(tag_isOn);
}
});

if (img_like.getTag() != null && img_like.getTag().toString().equals("red")) {
img_like.setImageResource(R.drawable.heart);
img_like.setTag("heart");
} else {
img_like.setImageResource(R.drawable.red);
img_like.setTag("red");
}`enter code here`

Related

How to set the background of button programmatically when that is at some of conditions in android?

I am going to change the background of button in android when text value is lower than 1.
btn_minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int temp = convertStringToInt(text_count.getText().toString());
if (temp != 1){
text_count.setText(temp-1);
} else {
btn_minus.setBackground(R.drawable.ic_circle_gray_minus);
}
}
});
btn_minus is the object of Button. and btn_minus.setBackground is not working now.
Using setBackgroundResource() function instead of the setBackground().
Use below code for changing button background color :
btn_minus.setBackgroundResource(R.drawable.ic_circle_gray_minus);

How to set multiple click action in a imageview and plus, minus button increment and decrement in android

Hlw,
Problem1:
I am developing a app where I have a ImageView like favorite button where I click and change this image resource(it is done), but I want to set action when user again click this imageview and the image remain same.
Problem2:
I have two image button "plus" and "minus" I set condition to it that when user click + button middle textview increment by 1, and when it reach 10 then the button will unclickable, also for the minus button, when textviw equal=0 then it not works...
I done it by condition but when minus button reach 0 and after plus button click, it increment but not decrement it remain unclickable...
how can i solve this problem?
Image like this
productWrapper.plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(context, "Plus", Toast.LENGTH_SHORT).show();
try
{
String presentValStr= finalProductWrapper2.selectedQuantity.getText().toString();
int presentIntVal=Integer.parseInt(presentValStr);
presentIntVal++;
if (presentIntVal>=10){
Toast.makeText(context,"You can select max 10 product",Toast.LENGTH_LONG).show();
finalProductWrapper2.plus.setEnabled(false);
}
finalProductWrapper2.selectedQuantity.setText(String.valueOf(presentIntVal));
}
catch(Exception e)
{
e.printStackTrace();
Toast.makeText(context,"Error! please try again",Toast.LENGTH_LONG).show();
}
}
});
final ProductWrapper finalProductWrapper = productWrapper;
productWrapper.heart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finalProductWrapper.heart.setImageResource(R.drawable.heart2);
}
});
Point #1.
You can use boolean flag like below
if(flag == true){
flag = false;
// Change your Image Resource here
}
else {
flag = true;
// Do your other action
}
for Minus button put following condition
if(YOUR_POINTS >0){
// Do your action
}
else {
// Ignore
}

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 can I change the onClick action of a ImageView dynamically?

I have a ImageView that does an action called setAsFavorite(). This action sets a product as favorite, so I change the image of the ImageView. But when the product is favorite, I want to change the onclick event, so when the user presses the imageView, the action called should be unsetAsFavorite() instead of setAsFavorite.
How can I change the onclick event dynamically?
You could use A flag to know if it has been set as favorite then act on that
yourImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(isSetAsFavorite){
unsetAsFavorite();
} else {setAsFavorite();}
}
});
where is isSetAsFavorite is of boolean type.
The answer given by av_lee is sufficient. But there is a better option.
Use if instead,
boolean isFavourite = false;
public void switchFavorite(){//Replace function setAsFavorite() with this
if(isFavourite)
isFavourite = false;
else
isFavourite = true;
}
This way you decrease the number of function needed for the job.
I think there are two operation in your onclick method setAsFavorite() & unsetAsFavorite()
int myFlag =0;
onclick(){
if(myFlag == 0)
setAsFavorite();
else
unsetAsFavorite();
}
void setAsFavorite(){
myFlag = 1;
//do your work...
}
void unsetAsFavorite(){
myFlag = 0;
//do your work...
}
Get a reference to your ImageView:
getView().findViewById(R.id.your_img_view_id)
and then assign a new listener to it like this:
yourImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// your code here
}
});
This way you change an old listener with the new one.

Categories

Resources