Android: Change image on many imageviews when clicked/active - android

I have noticed many similar answers but unfortunately I'm having trouble using any of them. I am a few different imageviews as buttons:
ImageView filterWorld = (ImageView) kati.findViewById(R.id.filter_world);
filterWorld.setOnClickListener(new ViewListeners(this.getContext()).new OneFragmentFilters(OneFragment.this) ) ;
ImageView filterSports = (ImageView) kati.findViewById(R.id.filter_sports);
filterSports.setOnClickListener(new ViewListeners(this.getContext()).new OneFragmentFilters(OneFragment.this) ) ;
ImageView filterEconomy = (ImageView) kati.findViewById(R.id.filter_economy);
filterEconomy.setOnClickListener(new ViewListeners(this.getContext()).new OneFragmentFilters(OneFragment.this) ) ;
Each of them is using an xml selector as resource, such as:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/world_active" android:state_selected="true">
</item>
<item android:drawable="#drawable/world_active" android:state_focused="true">
</item>
<item android:drawable="#drawable/world_active" android:state_pressed="true">
</item>
<item android:drawable="#drawable/filter_white_world">
</item>
And the layout xml:
<ImageButton
android:id="#+id/filter_world"
android:layout_width="0dp"
android:layout_height="40dp"
android:scaleType="fitCenter"
android:layout_weight="1"
android:background="#drawable/world"
android:paddingTop="4dp"
android:paddingBottom="4dp"
/>
But the problem is that every button remains on "active" state only when it is clicked, and not when spesific content is shown. Also I would like each button to return to its default state when another button is clicked.
Thank you in advance!
Edit:
with your help I have managed to make the button change on every click, but now the content does not change. There seems to be a colfict between the onclick listeners. Here is my code:
final ImageView filterWorld = (ImageView) kati.findViewById(R.id.filter_world);
filterWorld.setOnClickListener(new ViewListeners(this.getContext()).new OneFragmentFilters(OneFragment.this) ) ;
final ImageView filterSports = (ImageView) kati.findViewById(R.id.filter_sports);
filterSports.setOnClickListener(new ViewListeners(this.getContext()).new OneFragmentFilters(OneFragment.this) ) ;
filterWorld.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
filterWorld.setImageResource(R.drawable.world_active);
filterSports.setImageResource(R.drawable.filter_white_sports); }
});
filterSports.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
filterWorld.setImageResource(R.drawable.filter_white_world);
filterSports.setImageResource(R.drawable.sports_active); }
});

You can do this without using selecter..
simply implement the click listner on each imageview.
and change the backround resource on all imageview.
filterWorld.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
filterWorld.setImageResource(R.drawable.world_active);
filterSport.setImageResource(R.drawable.filter_white_world);
filterEconomy.setImageResource(R.drawable.filter_white_world);
}
});
filterSport.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
filterSport.setImageResource(R.drawable.world_active);
filterWorld.setImageResource(R.drawable.filter_white_world);
filterEconomy.setImageResource(R.drawable.filter_white_world);
}
});
filterEconomy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
filterEconomy.setImageResource(R.drawable.world_active);
filterSport.setImageResource(R.drawable.filter_white_world);
filterWorld.setImageResource(R.drawable.filter_white_world);
}
});

When you press a button, it will go to active state and you have to set it to default state when an another button is pressed.
So set buttons other than which is currently pressed to default state by using this
ImageView filterWorld = (ImageView) kati.findViewById(R.id.filter_world);
filterWorld.setOnClickListener(new ViewListeners(this.getContext()).new OneFragmentFilters(OneFragment.this)
filterSports.setSelected(false);
filterSports.setPressed(false);
filterEconomy.setSelected(false);
filterEconomy.setPressed(false);
) ;

Related

How to change color of button when being click,and revert back to default color in next click?

I have a like button in my RecyclerView,what I want is when user hit the like button for the 1st time,the button background color will change to red color,and when the same user hit the like button,the button will change back to default color which is white.
I checked for few SO question,but still havent get what I want.So far my solution is like below,doesnt produce any error but when clicked the button,nothing happen.
likeButton =(Button) view.findViewById(R.id.likeButton);
//here for user like the post
holder.likeButton.setOnClickListener(new View.OnClickListener() {
boolean clicked = true;
#Override
public void onClick(View v) {
if(!clicked){
holder.likeButton.setBackgroundColor(Color.RED);
clicked = true;
//here i will update the database
}else{
holder.likeButton.setBackgroundColor(Color.WHITE);
clicked = false;
//here i will update the database
}
}
});
I checked this SO answer too,so I modified my code as below,but still nothing happens when the button is clicked.
holder.likeButton.setBackgroundColor(Color.WHITE);
holder.likeButton.setOnClickListener(new View.OnClickListener() {
ValueAnimator buttonColorAnim = null;
#Override
public void onClick(View v) {
if(buttonColorAnim != null){
buttonColorAnim.reverse();
buttonColorAnim = null;
//here i will update the database
}else{
final Button button = (Button) v;//here is the line I dont undestand
buttonColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), Color.RED, Color.WHITE);
buttonColorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animator) {
// set the background color
button.setBackgroundColor((Integer) animator.getAnimatedValue());
}
//here i will update the database
});
buttonColorAnim.start();
}
}
});
Somebody please point out what I'm missing,what I want is change button color programmatically when being click for 1st time,and change back to default for next click(which avoid multiple like from a same user).
You should create a selector file. Create a file in drawable folder like color_change.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/button_pressed"/> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#color/button_focused"/> <!-- focused -->
<item android:drawable="#color/button_default"/> <!-- default -->
</selector>
and declare it in the button like this
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/color_change"
android:text="Click Me" />
Hi try to this hope this can help you...
In XML
<Button
android:id="#+id/btnClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:text="click"/>
In Adapter Class
boolean click = true;
holder.btnClick.setTag(position);
holder.btnClick.setId(position);
holder.btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (click) {
holder.btnClick.setBackgroundColor(Color.RED);
click = false;
} else {
holder.btnClick.setBackgroundColor(Color.WHITE);
click = true;
}
notifyDataSetChanged();
}
});
Instead of clicked or not condition, make and use condition from you update database for like and dislike operation. So, In click-listener get data previously user like or not then change background and update database as per new click.
Try adding this line to your row.xml file in the main layout :
android:descendantFocusability="blocksDescendants"
Have a look at this. Here, I changed the button text color on click. First time, all buttons appear white and after click it toggles between red and white as you expected. --
//LikeAdapter.java
public class LikeAdapter extends RecyclerView.Adapter<LikeAdapter.LikeHolder> {
public LikeAdapter() {
}
#Override
public LikeAdapter.LikeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.like_item,parent,false);
return new LikeHolder(view);
}
#Override
public void onBindViewHolder(final LikeAdapter.LikeHolder holder, int position) {
holder.red_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (holder.red_btn.getVisibility() == View.VISIBLE) {
holder.red_btn.setVisibility(View.GONE);
holder.white_btn.setVisibility(View.VISIBLE);
} else {
holder.red_btn.setVisibility(View.VISIBLE);
holder.white_btn.setVisibility(View.GONE);
}
}
});
holder.white_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (holder.white_btn.getVisibility() == View.VISIBLE) {
holder.red_btn.setVisibility(View.VISIBLE);
holder.white_btn.setVisibility(View.GONE);
} else {
holder.red_btn.setVisibility(View.GONE);
holder.white_btn.setVisibility(View.VISIBLE);
}
}
});
}
#Override
public int getItemCount() {
return 5;
}
public class LikeHolder extends RecyclerView.ViewHolder {
private Button red_btn, white_btn;
public LikeHolder(View itemView) {
super(itemView);
red_btn = (Button) itemView.findViewById(R.id.red_btn);
white_btn = (Button) itemView.findViewById(R.id.white_btn);
red_btn.setBackgroundColor(Color.RED);
white_btn.setBackgroundColor(Color.WHITE);
}
}
}
//like_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<Button
android:id="#+id/red_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Red"/>
<Button
android:id="#+id/white_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="White"/>
</RelativeLayout>

Android Set button color on first click, unset on second click

please i need help on this.
I have searched here but the answers i have seen are not working for me, the posts being old, the functions are mostly deprecated.
I am trying to set the color of buttons on a single click in order to highlight them and unset the color on a second click. It's like making some choice from a number of buttons, and if I click on a selected button again maybe after changing my mind on my selection, the color should revert to the default. So that i am only left with the selected buttons highlighted.
The buttons are generated with an adapter in gridview and the onclicklistener applies to all of them.
The code i'm using is as shown:
public class ButtonAdapter extends BaseAdapter {
private Context context;
public View getView(int position, View convertView, ViewGroup parent)
{
final Button btn;
if (convertView == null) {
btn = new Button(context);
btn.setLayoutParams(new GridView.LayoutParams(40, 40));
btn.setPadding(2, 2, 2, 2);
}
else {
btn = (Button) convertView;
}
//exus
btn.setText(Integer.toString(gridNumbers[position]));
btn.setTextColor(Color.BLACK);
btn.setId(position);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//btn.setBackgroundColor(Color.GREEN);
//Toast.makeText(getBaseContext(), "Button clicked", Toast.LENGTH_LONG).show();
if (v.getSolidColor()!=Color.GREEN)
{
btn.setBackgroundColor(Color.GREEN);
}
else
{
btn.setBackgroundColor(Color.GRAY);
}
}
});
return btn;
}
}
}
My XML:
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="8"
android:columnWidth="20dp"
android:stretchMode="columnWidth"
android:gravity="center" />
You can use a list of boolean properties instead of doing this.
Set a public boolean list in your class (it should be public and outside of any functions otherwise the onclicklistener will have error)
List<boolean> blist=new Arraylist<boolean>(Size);
//Size is maximum number of buttons
int index;
Then whenever you create a new button add this:
blist.add(index,false);
index++;
in the onclicklistener; find the index of the button from its position and save the index in an integer named pos.
if(blist.get(pos)==false)
{
//not clicked yet
blist.remove(pos);
blist.add(pos,true);
//here write the code u need for this if
}
else
{
blist.remove(pos);
blist.add(pos,false);
//todo: ur code for else
}
I tried this way and it worked for me,if you want to change on click
counter = 1;
//By Default set color
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (counter == 1)
{
// Default color
counter = 2;
}
else
{
//your color
counter = 1;
}
}
});
View reused in GridView. So, you should define state for your buttons in your base adapters.
Take an ArrayList that will hold your selected index and remove it when grid is not selected.
Ex:
ArrayList<Integer> selectedItems;
In Construtor
selectedItems = new ArrayList<Integer>();
In OnClickListener
public void onClick(View v) {
if (selectedItems.contains(new Integer(position))) {
selectedItems.remove(new Integer(position));
notifyDataSetChanged();
} else {
selectedItems.add(new Integer(position));
notifyDataSetChanged();
}
}
In getView():
if (selectedItems.contains(new Integer(position))) {
btn.setBackgroundColor(Color.GREEN);
} else {
btn.setBackgroundColor(Color.GRAY);
}
Use toggle button insted of normal button. Like
<ToggleButton
android:id="#+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/check" //check.xml
android:layout_margin="10dp"
android:textOn=""
android:textOff=""
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_centerVertical="true"/>
and then make an xml file check.xml in drawable folder something like
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/selected_image"
android:state_checked="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/unselected_image"
android:state_checked="false"/>
</selector>
refrence.

change textview background until select another textview in android

I have to develop an android application.
I have using below textview:
<TextView
android:id="#+id/deals"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:background="#drawable/best_deal_bg"
android:text="Deals"
/>
This is background of these textview:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/menu_active" android:state_enabled="true"
android:state_pressed="true" />
</selector>
Here .,If i have to click these textview means that tiime ly shows background.
But i wish to need the output like.have to shows background until select another textview.
How can i do ?? please give me solution for these ???
EDIT:
i have set that background on android textview onclik functionality:
deals = (TextView) findViewById(R.id.deals);
deals.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
deals.setBackground(getResources().getDrawable(R.drawable.best_deal_bg));
Intent intent = new Intent(getParent(),Deals.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("BestDeals",intent);
}
});
Here am getting below error :
*07-11 10:07:50.593: E/AndroidRuntime(816): java.lang.NoSuchMethodError: android.widget.TextView.setBackground
*
EDIT:
deals = (TextView) findViewById(R.id.deals);
deals.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
View lastclickedview=null;
if(v != lastclickedview) {
v.setBackground(getResources().getDrawable(R.drawable.best_deal_bg));
lastclickedview.setBackground(getResources().getDrawable(R.drawable.message_icon));
}
lastclickedview = v;
Intent intent = new Intent(getParent(),Deals.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("BestDeals",intent);
}
});
Here am getting below error :
E/AndroidRuntime(883): java.lang.NoSuchMethodError: android.view.View.setBackground
What's worng in my code.pls give me solution for these ???
Dynamically change it in your code on selection of another TextView use
yourTextView.setBackgroundDrawable(getResources().getDrawable(R.drawable.best_deal_bg));
and if you want remove the background use
yourTextView.setBackground(null);
first change your textview's background color from your onclickListener and create a view object to save last clicked text view, in second time click on the texview check change the backgound of last clicked view to deafault color
textview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(v != lastclickedview) {
v.setBackground("new color");
lastclickedview.setBackground("default");
}
//your Action
lastclickedview = v;
}
});

how to change the image of a button with every click?

I created a button in the layout . In the Drawable folder I created a XML file named btn01_state. The btn01_state.xml is assigned to the button i created through "android:background=#drawable/btn01_state"
Now, the button has a default image img1.when i click on the button, the image1 changes to img2, and once i release the clicked mouse button, the image2 again changed to img1 again.
what i want to do is,to change the image of the button with evey click.
for an example, initially
btn01 has img01
if btn01 is pressed==> set img of btn01 to img02 and keep img02 till the btn01 is pressed again. Now, btn01 has img02 on it.
When btn01 is pressed, set img01 to btn01.
I hope this clarified more what i want to do.
btn_selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/android_blue"
android:state_pressed="true" />
<item android:drawable="#drawable/ic_launcher"
android:state_focused="true" />
<item android:drawable="#drawable/ic_launcher" />
main.xml
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/btn01"
android:background="#drawable/btn01_state"/>
You can do it easily within the code.
boolean isPressed = false;
button.setOnClickListener(buttonListener);
OnClickListener buttonListener = new OnClickListener() {
#Override
public void onClick(View v) {
if(isPressed)
button.setBackgroundResource(R.drawable.icon1);
else
button.setBackgroundResource(R.drawable.icon2);
isPressed = !isPressed;
}
};
Simple way
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.locationbutton_on));
}
});
Make it in code perhaps. Put a listener on the button and when the button is clicked the background is changed.

Change source image for image view when pressed

I have a scroll view with lots of image buttons. I want to change the image for an image button when it's pressed. The thing is that I want the image to remain until another image button is pressed. That's why I couldn't use a selector. What is the best practice to achieve this?
You want to do this.
ImageButton Demo_button = (ImageButton)findViewById(R.id.firstimage);
// when you click this demo button
Demo_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Demo_button.setImageResource(R.drawable.secondimage);
}
}
Try this. (updated setset to set)
Better solution, use the following xml as source of the image:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_activated="true">
<bitmap android:src="#drawable/image_selected"/>
</item>
<item>
<bitmap android:src="#drawable/image_not_selected"/>
</item>
</selector>
#Override
public void onClick(View v) {
v.setActivated(!v.isActivated());
}
The shortest way to achieve it, using only XML (no Java code):
Save the following as drawable/image_pressable.xml next to the two images:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/image_pressed" android:state_pressed="true" />
<item android:drawable="#drawable/image_regular" />
</selector>
then reference it as:
<ImageView android:src="#drawable/image_pressable" />
Tip:
Using state_pressed instead of state_activated allows the image to be changed when pressing it with the fingers. state_activated could be useful, if you want to dynamically change the status of the ImageView, but not if you just want it to behave different when pressed, without any code involved. Which is exactly what it was asked in the question of this topic.
the OnTouchListener is much better for what you have to do:
myImageButton.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
myImageButton.setImageResource(R.drawable.image_when_pressed);
break;
case MotionEvent.ACTION_UP :
myImageButton.setImageResource(R.drawable.image_when_released);
break;
}
return false;
}
});
try below code :-
boolean flag=false;
ImageButton btn = (ImageButton)findViewById(R.id.btn);
// when you click this demo button
btn .setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (!flag) {
btn.setBackgroundResource(R.drawable.imageonpress);
flag=true;
}
else {
btn.setBackgroundResource(R.drawable.image);
flag=false;
}
}
}
don't forget to create the field "fun"
Try this for changing image.When imageview is pressed
Like_btn.setOnClickListener(new OnClickListener()
{
**private boolean fun = true;**
public void onClick(View v)
{
if(fun)
{
Like_btn.setImageResource(R.drawable.unlike);
fun=false;
}
else
{
fun=true;
Like_btn.setImageResource(R.drawable.like);
Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
}
}
});
What worked for me was:
I created a new drawable xml file, for eg, image_state.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:src="#drawable/image_pressed"/>
</item>
<item>
<bitmap android:src="#drawable/image"/>
</item>
</selector>
And in my layout file, I set the src of the imageView as:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image_state"/>
ImageButton Demo_button = (ImageButton)findViewById(R.id.firstimage);
ImageButton second_button = (ImageButton)findViewById(R.id.secondimage);
// when you click this demo button
Demo_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Demo_button.setImageResource(R.drawable.secondimage);
second_button.setImageResource(R.drawable.firstimage);
}
}
second_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Demo_button.setImageResource(R.drawable.firstimage);
second_button.setImageResource(R.drawable.secondimage);
}
}
I Hope u want to like that
Right???
float alpha_first = 0.2f;
float alpha_second = 1.0f;
AlphaAnimation alphadp = new AlphaAnimation(alpha_second, alpha_first);
switch (v.getId()) {
case R.id.disable_deactivate_pic:
ImageButton disable_button =(ImageButton)findViewById(R.id.disable_deactivate_pic);
if (!flag) {
disable_button.setImageResource(R.drawable.enable_active);
linearLayout_for_picture = (LinearLayout) findViewById(R.id.linearlayout_imageView_pic);
alphadp.setFillAfter(true);
linearLayout_for_picture.startAnimation(alphadp);
flag=true;
}
else {
disable_button.setImageResource(R.drawable.disable_active);
alphadp.setFillAfter(false);
linearLayout_for_picture.startAnimation(alphadp);
flag=false;
}
break;
You can use a StateListDrawable to achieve this. This method also works for ImageButtons. I prefer it to setting additional listeners.
I've also compiled a class of additional helpers: http://alexanderwong.me/post/40799636705/android-change-background-image-drawable-on-press
public static StateListDrawable makeStateDrawable(Drawable drawable, Drawable pressedDrawable, Drawable disabledDrawable) {
boolean set = false;
StateListDrawable stateDrawable = new StateListDrawable();
if (disabledDrawable != null) {
set = true;
stateDrawable.addState(new int[] { -android.R.attr.state_enabled }, disabledDrawable);
}
if (pressedDrawable != null) {
set = true;
stateDrawable.addState(new int[] { android.R.attr.state_pressed }, pressedDrawable);
}
if (drawable != null) {
set = true;
stateDrawable.addState(new int[0], drawable);
}
return set ? stateDrawable : null;
}
state_pressed or state_activated did not work for me.
However, I succeeded with state_enabled
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:drawable="#drawable/checkbox_on" />
<item android:state_enabled="false" android:drawable="#drawable/checkbox_off" />
</selector>
Your xml should declare it as src:
<ImageView
android:id="#+id/img_checkmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/checkbox_selector_filter"/>
Then in code, ensure you enable or disable it based on event/state:
imageCheckBox.isEnabled = true
This was easiest and cleanest for me
if you have an ImageView orImageButton and want to change image of that when its pressed, you can refresh activity for any pressed:
fav.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int isFavor = c.getInt(c.getColumnIndex("isFavor"));
if(isFavor == 1){
db.execSQL("update content set isFavor=0 where ID="+idxx+";");
fav.setImageResource(R.drawable.favnot_ic);
Toast.makeText(context.getApplicationContext(),"item cleaned",Toast.LENGTH_LONG).show();
activity.finish();
activity.overridePendingTransition(0, 0);
context.startActivity(activity.getIntent());
}
else{
db.execSQL("update content set isFavor=1 where ID=" + idxx + ";");
fav.setImageResource(R.drawable.fav_ic);
Toast.makeText(context.getApplicationContext(),"Item added...",Toast.LENGTH_LONG).show();
activity.finish();
activity.overridePendingTransition(0, 0);
context.startActivity(activity.getIntent());
}
}
});
If you save the selection on click and then reload the image scroll gallery (or scroll menu) and then reload and scroll to the selection with a replaced image, then you might be able to do it. As far as I know none of the inbuilt gallery or menu functions have the ability to replace images once they are loaded and displayed.
if u have already a button in ur app that will do the swap between the two pics u have when its clicked then there is a simple Code :D
// to add Images in android we simply " Copy them from their location and past in (res --> drawable)"
// then drag and drop "ImageView" and select the image u want to display
// u can adjust the scale throw "scaleType , layout_Width & layout_Height"
public boolean swap = true;
public void change(View view)
{
ImageView i = (ImageView) findViewById(R.id.img);
if (swap)
{
i.setImageResource(R.drawable.images);
swap=false;
}
else
{
swap=true;
i.setImageResource(R.drawable.couple);
}
}
Although it's too late to answer. But someones may get help from this answer.
It's working like charm -
circularImageView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
circularImageView.setImageResource(R.drawable.profile_edit_photo);
break;
case MotionEvent.ACTION_UP:
circularImageView.setImageResource(R.drawable.photo_male_8);
break;
case MotionEvent.ACTION_MOVE:
Log.i("ImageViewEvent", "Action_Move_Called");
break;
}
return true; // Note: This return value must need to be true if you want to call MotionEvent.ACTION_UP: action.
}
});
Demo_button.setImageResource(R.drawable.secondimage)
//Demo_button is your event holder (the button or imageview or bitmap file which contains the image) and secondimage is your image (drawable) file, without any complications.
This does the trick.

Categories

Resources