i'm using this How to animate .gif images in an android? solution (first answer of Shobhit Puri) to implements in my app some Gif. now i want to add some Listener on them (click listner).
every single procedure i tried didn't worked.
the idea is:
the gif is made by 3 image, when you click the first image the gif will change in another one, and so on.
Thanks for your attention.
EDIT:
actualy i'm trying this way
findViewById(R.id.ivAnimation).setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Log.i(TAG, "onClickGif PrincipalActivity");
}
});
on onCreate() of the mainActivity. the Log is well generated but i can't go over this
I'm not sure about gifs, but if you have separate files, you could do something like this.
Warning: Untested :)
final int[] imgs = new int[] {R.drawable.img1, R.drawable.img2, R.drawable.img3};
int position = 0;
final ImageView gifView = (ImageView) findViewById(R.id.ivAnimation);
gifView.setImageResource(imgs[position]);
gifView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Log.i(TAG, "onClickGif PrincipalActivity");
position = (position + 1) % imgs.length; // wrap around to "restart" gif
gifView.setImageResource(imgs[position]);
}
});
Related
If I want to change a image to another by the click of a button and then back to the previous image again by the click of the same button on imageview in android studio how to do that in short and easiest way? As I am new to it I am not familiar with all the functions of imageview.
For example:-
I wrote this code to do what I needed after a lot of failure in finding a easier way.
int i=0;
public void change(View v){
int img[] = {R.drawable.cat1,R.drawable.cat2};
ImageView cat = findViewById(R.id.imageView2);
if(i==0)
{cat.setImageResource(img[1]);
i=1;}
else {cat.setImageResource(img[0]);
i=0;}
}
Before I was trying to do something like this:-
public void change(View v){
ImageView cat = findViewById(R.id.imageView2);
if(cat.getDrawable()==R.drawable.cat2;)
{cat.setImageResource(R.drawable.cat1);}
else
{cat.setImageResource(R.drawable.cat1};
}
But it kept giving error that they have different type and I also tried some other functions named getId() but it didnt work either...
So my main objective is, is there a function through which I can campare the resource of image view directly with the image in drawable folder and how to implement it in if else or some other conditional statement?
The first approach should work, but the i value seems not tightly coupled to the ImageView. So, instead you can set a tag to the ImageView that equals to the current drawable id:
Initial tag:
ImageView cat = findViewById(R.id.imageView2);
cat.setImageResource(R.drawable.cat1);
cat.setTag(R.drawable.cat1);
And click listener callback:
public void change(View v){
ImageView cat = findViewById(R.id.imageView2);
int tag = (int) cat.getTag();
if(tag == R.drawable.cat2){
cat.setImageResource(R.drawable.cat1);
cat.setTag(R.drawable.cat1);
} else {
cat.setImageResource(R.drawable.cat2);
cat.setTag(R.drawable.cat2);
}
}
You could try StateListDrawable, LevelListDrawable, with each state/level, it will change image depend on your state/level
I added some imageviews in xml layout. Now programmatically i need to check which imageView i have added from the xml file and if same image then on click of that image i need to replace new image and visa versa.
Is the way i implemented it good or can I find a better solution for this?
This is my code so far:
holder.bankTickImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (holder.bankTickImg.getDrawable().getConstantState().equals(context.getResources().getDrawable(R.drawable.black_untick))) {
holder.bankTickImg.setImageResource(R.drawable.blue_tick);
}else{
holder.bankTickImg.setImageResource(R.drawable.black_untick);
}
}
});
Imageview Code
<ImageView
android:id="#+id/defaultBankTick"
android:layout_width="#dimen/TwentyFour"
android:layout_height="#dimen/TwentyFour"
android:layout_alignParentRight="true"
android:layout_marginBottom="#dimen/FiftyFive"
android:layout_marginRight="#dimen/Thirty"
android:layout_marginTop="#dimen/FiftyFive"
app:srcCompat="#drawable/black_untick" />
If i got your question.
Why don't you use a integer/boolean variable to save the state. eg.
lets say, you start with
int clicked = 0;
then in on click check if its value it 0 then treat it as fresh and change the value to 1.
and if the value is already 1 then treat it as it clicked 2nd time and change it to 0 .
There are many ways to check imageView which you added
holder.bankTickImg.setImageResource(R.drawable.black_untick); //default image , you can also set in xml code
holder.bankTickImg.setId(0);//you also do with string setTag();
holder.bankTickImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int istick=holder.bankTickImg.getId();
if (istick==1)
{
holder.bankTickImg.setImageResource(R.drawable.blue_tick);
}
else
{
holder.bankTickImg.setImageResource(R.drawable.black_untick);
}
}
});
I want to be able to click on an ImageView in a list and have it popup on click, like in the Tumblr and Path app.
Does anyone know how this can be done?
PS: I've tired using a dialog already.
I know its old question. but ill add this for future reference.
You can use this library to show image as a Popup. https://github.com/chathuralakmal/AndroidImagePopup
final ImagePopup imagePopup = new ImagePopup(this);
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
/** Initiate Popup view **/
imagePopup.initiatePopup(imageView.getDrawable());
}
});
There's a component called PopupWindow, you can find some example of usage here:
http://android-er.blogspot.com.br/2012/03/example-of-using-popupwindow.html
It is slightly similar to a Dialog, but you have more control over it.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1;
final ImageView image;
button1 = (Button) findViewById(R.id.button1);
image = (ImageView) findViewById(R.id.imageView1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
int j=0;
while(j<=4){
int res=getResources().getIdentifier("d002_p00"+j,"drawable",getPackageName());
image.setBackgroundResource(res);
j = j+1;
}
}
});
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
}
}
I have 3 questions:
What is wrong with this code this code only show 1st and last image? how I can fix it?
I don't want to rotate my apps. How I can do that?
How I can mentioned a user in Stack Vverflow? I tried #username but it didn't work.
Answer for question 1: What is wrong with this code this code only show 1st and last image? how I can fix it?
Put the int j outside the onClick and change the while to an if
int j=0;
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(j >= 0 && j <= 4){
int res = getResources().getIdentifier("d002_p00"+j, "drawable", getPackageName());
image.setBackgroundResource(res);
j++;
}
}
});
Answer for question 2: I don't want to rotate my apps. How I can do that?
See this answer of another stackoverflow question.
Answer for question 3: How I can mentioned a user in Stack Vverflow? I tried #username but it didn't work.
I don't know for sure myself, but I think you can't use #user_name to reply a comment when there is only one commenter apart from yourself (at least I couldn't). If you want to credit someone however, I would suggest putting their profile-page in a link like so: #user3546792 (Click their name, copy the link, click the Add Hyperlink-button in SO and paste the copied link. Then change [enter link description here] to [#user_name]
For question 2:
I don't want to rotate my apps. How I can do that?
Your just add this in your android Manifest:
android:orientation="portrait"
I'm new to andorid, and I'm trying to do a simple thing:
when button A is clicked, I want to replace the image displayed
on ImageButton B.
I've tried all sort of things like:
msortByButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Test change image
mdefineLocationButton.setBackgroundResource(0);
mdefineLocationButton.invalidate();
mdefineLocationButton.setBackgroundResource(R.drawable.notdefined);
mdefineLocationButton.invalidate();
mdefineLocationButton.refreshDrawableState();
}
});
But it seems that the new image is painted, but on top of it the old image
is painted also. (I can see the old image, below it I can see edges of the new image).
Any idea how to do this right?
Thanks,
Omer
Use setImageResource (int resId)
(http://developer.android.com/reference/android/widget/ImageView.html#setImageResource(int))
ImageButtons can have a background and an actual image src. My guess is initially you set the image src and now in code you are setting the background. So both show.
#Override
public void onClick(View v) {
mdefineLocationButton.setImageResource(R.drawable.notdefined);
}