Hello guys I want to use a ImageView as a slideshow, that touch you on the right of the screen display the next image and if the touch is on the left show the previous image,
I already tried to implement this function but I get no response from the ImageView, looking logcat I realized that it doesn't dispatch of TouchEvent.
public class Prova4Activity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
ImageView image;
Bitmap trash = loadImageFromUrl("http://www.televideo.rai.it/televideo/pub/tt4web/ Nazionale/16_9_page-100.6.png");
ImageView imgpag;
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.EDGE_RIGHT){
trash.recycle();
trash = null;
trash = loadImageFromUrl("http://www.televideo.rai.it/televideo/pub/tt4web/Nazionale/16_9_page-100.6.png");
image.setImageBitmap(trash);
}else if(event.getAction() == MotionEvent.EDGE_LEFT){
trash.recycle();
trash = null;
trash = loadImageFromUrl("http://www.televideo.rai.it/televideo/pub/tt4web/ Nazionale/16_9_page-100.5.png");
imgpag.setImageBitmap(trash);
}
imgpag.setScaleType(ScaleType.FIT_XY);
return true;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgpag = (ImageView)findViewById (R.id.imgpag);
imgpag.setImageBitmap(trash);
imgpag.setOnTouchListener(this);
}
}
I would not use a gallery because the images are small and do not know in advance the number of images because i load them directly from Inernet based on the actions of the user
This might be not the best way to achieve this but you can try following:
Set two buttons on Screen - One on Right corner and One on the left corner.
(If you are showing ImageView full screen then better to use FrameLayout -
bt make sure that Buttons are on the front)
Set Button properties like
<Button
android:id="#+id/button1"
android:layout_width="70dp"
android:layout_height="40dp" android:background="#null"/>
This will make sure that buttons are invisible
Then just add your logic of prev and next on OnClickListeners of these buttons
Hope this will help you
Related
I am new to android, In my app I just need to add a screen like pattern lock, what exactly I want to do is, no need to create/ set a pattern.Just design a screen with 9 dots, and when user draw some pattern and I need to get those pattern value.For design I used PatternView but how can I get the user drawer pattern? I already google on this, but I found there they setting the pattern lock, I don't want that.I just want is when I draw something on PatternView it just return the value like,suppose like if I draw a pattern like 2365 dots it will return an integer value like 2365.
I already tried haibison.github.io/android-lockpattern but my app is different than this.I just want to draw a pattern and get result, no need to create and confirm screens. No need to save the result in shared preferences. I just want compare the drawn pattern value with my predefined value.
Following is idea on how to go for this:
Start with a table view for your layout and create a 3 x 3 grid of image views (dots).
override onTouchListener on your activity to detect touches on images. refer this answer for details.
private ImageView imageView;
private Rect imageRect;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (imageRect == null) {
imageRect = new Rect();
imageView.getGlobalVisibleRect(imageRect);
}
int x = (int) event.getX();
int y = (int) event.getY();
if (imageRect.contains(x, y)) {
Log.i(TAG, "touch passing over imageView");
}
return true;
}
example is for 1 image view you have to build it for 9.
as and when touch is detected append the index of imageview to a string.
once an image is touched stop detecting touches on it.
perform desired operation when MotionEvent.ACTION_UP is detected
so all I want to do is to touch on a certain part of the screen where an invisible view is hidden.
After that touch the view should fade In.
The Problem: The Listener isn't triggered whe I touch the view.
Things I've tried: view.setImageAlpha(0), view.setAlpha(0), view.setVisibility(View.INVISIBLE)
When I set alpha=1 the listener works fine.
Thanks in advance.
Edit:
Thanks to #Viswanath L for the great solution which he mentioned in his post as second workaround.
I just wanted to give a little code example for that in case someone doesn't know how this works:
What I want: I want to touch the right corner of the screen and at this spot, a view(myView) should fade in.
fadeInListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
System.out.println("TRIGGERED");
//The screen size here is 1080x1920(vertical)
if (event.getRawX() > 800 && event.getRawY() > 1640) {
final AlphaAnimation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setDuration(500);
fadeIn.setFillAfter(true);
myView.startAnimation(fadeIn);
}
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout.setOnTouchListener(fadeInListener);
}
The layout is:
this.layout = (RelativeLayout) findViewById(R.id.layout);
And myView should be placed in the right corner and what's very important:
myView.setVisibility(View.Gone);
Hope this helps.
INVISBLE means you are trying to add a listener to a view which is not there. You can add the listener to a visible view only.
WORKAROUND
1) Try to make a dummy view which is visible but having the same color as background.
2) Try to set the listener for parent and check the position (whether the position does
belongs to INVISIBLE view).
onTouchListener does not trigger for an invisible View. You can make 2 exactly the same views one under another the one below having the map. It will be visible and so it can be touched but it will be obscured by the view above like they do here: http://blahti.wordpress.com/2012/06/26/images-with-clickable-areas/
Try this in your xml:
<View
android:id="#+id/my_inv_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/blue"
/>
<View
android:id="#+id/my_touch_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
And set up the click listener like this:
final View mTouchView = findViewById(R.id.my_touch_view);
final View mInvView = findViewById(R.id.my_inv_view);
mInvView.setVisibility(View.INVISIBLE);
mTouchView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mInvView.setVisibility(View.VISIBLE);
}
});
Hi I have loaded dynamically images in different position using single image view instance.
my problem is if I touch particular image second image also affected 1st I wish to find which image is touched. i want to print which image is touched. I am suffer in this task please help me.
my problem is i have only one imageview to show all images in appropriate location. I received xml file that xml contain everything like image size, image position ...etc I am loading dynamically images because i receive xml file dynamically. if i touch one image i wish to identify which image is clicked in the design that's all
This is my screen shot:
Firtly implements onTouchListener then override the method,
ImageView one = ....
ImageView two = ....
one.SetTag("ITEM ONE");
two.SetTag("ITEM TWO");
one.setOnTouchListener(this);
two.setOnToucListener(this);
#Override
public boolean onTouch(View v, MotionEvent event) {
if(v == one)
{
Log.e("Touched Itemd: ",(String) v.getTag().toString());
}
if(v == two)
{
Log.e("Touched Itemd: ",(String) v.getTag().toString());
}
}
I have an ImageView, ther are 2 Images for a particular imageview, but the problem is when i click it (the clicked image is bigger then the previous one). So the layout gets shifted upwards, i dont want layout to move upwards, i just want image to extend upwards.
// addnews button the 1st screen
final ImageView addnewsback = (ImageView)findViewById(R.id.newslistback);
ImageView addnews = (ImageView)findViewById(R.id.newslistbtn);
addnews.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
addnewsback.setImageResource(R.drawable.bgbig);
vf.setDisplayedChild(0);
}
});
The correct view should be,
I'd appreciate it if someone could help me with my problem.
I am trying to change the image in an ImageView when someone clicks on it. I've put my images in an array and I am using a while loop to cycle once through all of them.
My problem is that while the first image (image8, not in the array) shows in the view all the other (after creating the OnClickListener) do not. Actually nothing happens and I am not sure where the mistake is. Thanks in advance.
This is the problematic code:
final int array[]=new int[5];
array[0]= R.drawable.image6;
array[1]= R.drawable.image4;
array[2]= R.drawable.image9;
array[3]= R.drawable.image4;
array[4]= R.drawable.image5;
ImageView touchView = (ImageView)findViewById(R.id.imageview);
touchView.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View touchView, MotionEvent ev) {
//get coordinates of touch event
int x = (int)ev.getRawX();
int y = (int)ev.getRawY();
---Code missing---
((ImageView)touchView).setImageResource(R.drawable.image8);
touchView.setOnClickListener(new View.OnClickListener() {
int counter = 0;
#Override
//Image change on every click
public void onClick(View touchView) {
while(counter<5){
((ImageView) touchView).setImageResource(array[counter]);
counter++;
});
You can't change images in a sequence like that and expect anything to show on the screen. You should use the click to start a separate thread that will do the image animation. See the description of the android.view.animation package. It sounds like the AnimationDrawable class will give you exactly what you want.
There is a special view for your case. ImageSwitcher is what you need. There is an example from android developers on how to use it. It should be trivial to adapt the example to your needs.