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.
Related
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.
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.
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;
}
}
});
I want my toggle button's ON text to be large and OFF text to be small. But can't do it.. any suggestions? This is what i was trying
mf=(ToggleButton)findViewById(R.id.mf);
if(mf.isEnabled()==true)
{
mf.setTextSize(13);
}
else
mf.setTextSize(8);
Your code has to be called each time you click on your button. so use :
toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (toggleButton.isChecked()) {
toggleButton.setTextSize(13.0f);
} else {
toggleButton.setTextSize(8.0f);
}
}
});
You can set OnClickListner with a easy method. In your .xml put the option
android:onClick="nameOfMethod"
And then in your code:
public void nameOfMethod(View v){
}
Where v is the togglebutton in this case ( ToggleButton mf = (ToggleButton)v; )
I put the solution here:
mf=(ToggleButton)findViewById(R.id.mf);
if(mf.isChecked())
{
mf.setTextSize(13);
}
else
mf.setTextSize(8);
Use isChecked() instead of isEnabled()
You need to do some debugging.
Firstly:
if(mf.isEnabled()==true)
can be
if (mf.isEnabled())
Does mf.setTextSize(13) on it's own work as expected?
Add some logging:
if (mf.isEnabled())
{
// Add some logging, is this line reached correctly?
mf.setTextSize(13);
}
else
// Add some logging, is this line reached correctly?
mf.setTextSize(8);
Chances are you need to change isEnabled() to isChecked(). isEnabled() means exactly that, whether it's enabled or not. You want to know whether the button has been checked.
I specifically didn't want to use a radiogroup. I know, I know ... a radiogroup is perfect when you need exclusivity like this.
But..given that I'm working with togglebuttons, how can i disable (setChecked(false)) all (3) other toggle buttons when one of them is checked?
Here is my code for the group of toggle buttons. You need to put your
buttons in the layout where you want them and then use ToggleButtonsGroup object to put them together in a single group.
private class ToggleButtonsGroup implements OnCheckedChangeListener {
private ArrayList<ToggleButton> mButtons;
public ToggleButtonsGroup() {
mButtons = new ArrayList<ToggleButton>();
}
public void addButton(ToggleButton btn) {
btn.setOnCheckedChangeListener(this);
mButtons.add(btn);
}
public ToggleButton getSelectedButton() {
for(ToggleButton b : mButtons) {
if(b.isChecked())
return b;
}
return null;
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked) {
uncheckOtherButtons(buttonView.getId());
mDataChanged = true;
} else if (!anyButtonChecked()){
buttonView.setChecked(true);
}
}
private boolean anyButtonChecked() {
for(ToggleButton b : mButtons) {
if(b.isChecked())
return true;
}
return false;
}
private void uncheckOtherButtons(int current_button_id) {
for(ToggleButton b : mButtons) {
if(b.getId() != current_button_id)
b.setChecked(false);
}
}
}
I think you'll need to implement your own radiogroup logic for that. It will need to register itself as the OnCheckedChangeListener for each button it manages.
I wish the API wasn't designed with RadioGroup as a LinearLayout. It would have been much better to separate the radio group management from layout.
I know this has been answered with very good examples, but the way I set mine up is just a tad different so I thought I'd share. It's on a preference list, no ToggleButtonsGroup involved.
#Override
public boolean onPreferenceClick(Preference preference) {
String key = preference.getKey();
if (key.equals("trailPref") || key.equals("obstaclesPref") || key.equals("insanePref") || key.equals("backgroundBlackPref"))
{
if (key.equals("insanePref"))
{
endurancebox.setChecked(false);
prefsEditor.putBoolean("obstaclesPref", false);
prefsEditor.commit();
}
if (key.equals("obstaclesPref"))
{
insanebox.setChecked(false);
prefsEditor.putBoolean("insanePref", false);
prefsEditor.commit();
}
boolean b = ((CheckBoxPreference) preference).isChecked();
prefsEditor.putBoolean(key, b);
prefsEditor.commit();
}
Log.i(DEBUG_TAG, "Boolean value changed. " + key );
returnPrefs(c);
return false;
}
Also, I'm an Android noob and not too experienced in java either, so it may not be uber efficient, but hey, it works! Could be formatted easily to fit your needs I believe. If the key equals insanePref, I want to turn off obstaclesPref and vice versa. (different game modes, only one can be active at the time.) The other two booleans are unrelated. Also, it puts the preferences into my SharedPreference file, instantiated earlier, along with everything else.
Cheers!