How can I change the onClick action of a ImageView dynamically? - android

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.

Related

Clickable ImageButton in RecyclerView

I have an ImageButton such as this icon . I want when I click on it, it will be like this and when I click again it will be like picture 1. What should i add to code? These ImageButtons are in RecyclerView.
inside OnBindView().
boolean beforeClicked =false;
holder.imageView.setOnClickLisnter(new OnClickListner(){
if(!beforeClicked){
holder.imageView.setDrawableResource(R.layout.whiteHeart);
beforeClicked = true;
}
else{
holder.imageView.setDrawableResource(R.layout.blackHeart);
beforeClicked = false;
}
});
May have some spelling mistakes. You can rectify that.
Try this:
//Define Global Variable
Boolean isCheck = true;
//On click of icon
isCheck =! isCheck // This will make boolean switch like On/Off
if(isCheck){
//Add your fill icon here
}else{
//Add your un-filled icon here
}
Yes you need to add button inside your recycler view item and add one flag into your model class So, when you click first time change image to second one(filled heart) and set flag as true and when click on second time go for your like image code.
So,your code structure will shows like below
Your model class
public class model {
private boolean isFirstTime = false;
...
public boolean isFirstTime() {
return isFirstTime;
}
public void setFirstTime(boolean firstTime) {
isFirstTime = firstTime;
}
...
}
Add below code into your adapter
holder.yourView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!arrayList.get(position).getIsClick()) {
holder.yourImageView.setImageResource(getResources().getDrawable(R.drawable.secondImage));
arrayList.get(position).setIsClick(true);
} else {
//your like image code
}
notifyDataSetChanged();
}
});
I hope this can help you!
Thank You.

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 check which image is set in imageview 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`

Dynamic Buttons with two functionalities ANDROID

I have dynamic buttons that are created by a variable that can change.I want that these buttons have two functions. I did one option but I don't know how to implement the other option.
the first time I click the button I call a function that do something and the second time that I click the same button I would like to do another action. And I want to repeat this running with all the dynamic buttons created.
My code is:
LinearLayout buttonsLayout = (LinearLayout)findViewById(R.id.linearlayoutUp);
for(int i=0;i<drawView.getNumeroMallas();i++){
Button buttonMalla = new Button(this);
buttonMalla.setText("Malla "+(i+1));
buttonMalla.setId(i+1);
final int index = i;
buttonMalla.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Malla malla = drawView.getMalla(index);
drawView.paintMallaSelected(malla);
}
}
});
buttonsLayout.addView(buttonMalla);
}
}
EDIT - Very important:
I readed your code again, you could use the getTag/setTag to remember the last state of the button (i missed the for part, sorry!)
for(int i=0;i<drawView.getNumeroMallas();i++){
Button buttonMalla = new Button(this);
buttonMalla.setText("Malla "+(i+1));
buttonMalla.setId(i+1);
buttonMaila.setTag(Boolean.FALSE);
Then in setOnClickListener
if (((Boolean)v.getTag()) == Boolean.TRUE)
{
// Do first action
v.setTag(Boolean.FALSE);
}
else
{
// Do second action
v.setTag(Boolean.TRUE);
}
An idea could be to use a variable to know which was the last action.
A boolean variable
private boolean action = false;
If action is false do the first thing and set it to true. If it's true do the second action and set it to false.
It should go out any method (global of the class)
Something like
private boolean action;
buttonMalla.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (action == true)
{
// Do first action
}
else
{
// Do second action
}
action = !action;
Malla malla = drawView.getMalla(index);
drawView.paintMallaSelected(malla);
}
}
});
Anyway if it does something of important you should manage better the button (example button action is not based on a boolean variable but in a specific state.) time ago i builded a "select all" and "unselect all" function in an app and i checked if the user have unselect manually something to let the button act again like a "select".. I hope i gave to you an idea.
Anyway the boolean variable is the most immediate way to do it.

Categories

Resources