i have a blank screen on mainactivity.
when i tap everywhere on the screen it will do an action. such as opening a new activity or making and event. is it possible to make it on an imageview too? what is the listener that listens to the tapping and how can i get a method such as "OnTap"?
I know little about android but sure this can help:
create an ImageView with height and width set to match_parent
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="tap_me"
/>
then in your java code you can do this
public void tap_me(View v){
//do something
}
View.onClickListener should do the work for you.
Update:
Check out similar problem: How to Set onClicklistener Method of ImageView in Android?
You can only detect clicks within your app. Make the imageview to cover the entire screen. Then click on any point will be picked up by the onClickListener.
You could also do something like this:
ImageView imageView = (ImageView) findViewById(R.drawable.YourImage);
imageView.setOnTouchListener(new OnTouchListener) {
#Override
public boolean onTouch(View view, MotionEvent me) {
switch(me.getAction()) {
case(MotionEvent.ACTION_DOWN):
//do stuff here
}
}
}
Related
I have a problem I am struggling with a while now.
I have a Layout with a Button and a container in it.
<FrameLayout ... >
<Button
android:id="#+id/button"
...
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/overlayContainer"/>
</FrameLayout>
My goal is that as I long-press the button, I attach a custom view MyCustomViewto the container and keep the finger pressed.
All the following (ACTION_MOVE, ACTION_UP) events should then ideally be dispatched to and evaluated by MyCustomView.
MyCustomView works like a circular flyout menu: it overlays, dims the background, and shows some options. You then slide your pressed finger to the option, lift it up, and it triggers a result.
mButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// attach custom view to overlayContainer
// simplified code for demonstration
overlayContainer.addView(new MyCustomView());
return true;
}
});
Right now I don't see any option to "steal" the ACTION_DOWN-Event (which is required to start the event flow to a view) from the Button as I'm above it.
Nor does it work to manually generate and dispatch a ACTION_DOWN-Event in MyCustomView as I attach it.
While researching I found this post here, it basically is the same requirement, but for iOS (also does not provide an elegant solution, other that an click capturing overlay view) ): How to preserve touch event after new view is added by long press
Note that I want to avoid some kind of global overlay over the main view, I would like the solution to be as pluggable and portable as possible.
Thanks for any suggestions.
To answer my own question after the hint in the comments:
I solved it using a bare stripped version of TouchDelegate (had to extend it, since it unfortunetaly is no interface - setTouchDelegate only accepts TouchDelegate (sub)classes. Not 100% clean, but works great.
public class CustomTouchDelegate extends TouchDelegate {
private View mDelegateView;
public CustomTouchDelegate(View delegateView) {
super(new Rect(), delegateView);
mDelegateView = delegateView;
}
public boolean onTouchEvent(MotionEvent event) {
return mDelegateView.dispatchTouchEvent(event);
}
}
Then in my onLongClick method:
mButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// attach custom view to overlayContainer, simplified for demonstration
MyCustomView myMenuView = new MyCustomView()
mButton.setTouchDelegate(new CustomTouchDelegate(myMenuView));
// What's left out here is to mButton.setTouchDelegate = null,
// as soon as the temporary Overlay View is removed
overlayContainer.addView(myMenuView);
return true;
}
});
This way, all my ACTION_MOVE events from the Button are delegated to MyCustomView (and may or may not need some translation of the coordinates) - et voilĂ .
Thanks to pskink for the hint.
I have a view animator that has a lot Button on it. I've a OnTouchEvent for the view . codes works fine if I touch on space, but if I touch the button the code doesn't work. how can i fix it?
final View frame = (View)findViewById(R.id.viewAnimator1);
frame.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeRight() {
viewAnimator.showPrevious();
}
public void onSwipeLeft() {
viewAnimator.showNext();
}}
You need to set on click listener for buttons, if all of them makes the same work, you can extend button class and implement on click listener and use it instead of normal button
i am using listview in android with ratingbar as well as check box, here is my problem,if i am setting onitemclick listner to the listview, it is not working?
Please any one can help me..
Thanks in advance.
In your xml file.............
In checkbox..........
android:focusable="false"
Yes, I see the exact same thing. If you remove the checkbox and rating bar, then OnItemClick works, but with these widgets in your view, Android thinks the user wants to interact with them.
So instead you have to handle the user click in the view itself (rather than the listview).
OnTouchListener pressItemListener =new OnTouchListener()
{
#Override
public boolean onTouch(View arg0, MotionEvent arg1)
{
HomeActivity ha = (HomeActivity) getContext();
ha.handleLocationTouchEvent(position, arg1);
return false;
}
}
newView.setOnTouchListener(pressItemListener);
In the above example, HomeActivity is my parent activity. So I handle the user touch event in the custom view and then pass it off to the parent activity where you can do with it what you want. You might also want to handle onLongTouch. Hope this helps.
I one activity I am seting different layouts depends on user choice.
For example I have setContentView(R.layout.main),
after that when user choose something I am setting new like setContentView(R.layout.first) when next time clicks I am setting setContentView(R.layout.second).
I need to change content in same activity. How to set animation ( something like when I really changing between activities ) when I changes content from main to first and from first to second ?
Let us take this with example.
Suppose we are changing view on press of button,
private OnTouchListener touch = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
loadOutViewAnimation();//
loadInViewAnimation();
}
}
}
public void loadOutViewAnimation(){
//considering layout is your root layout
layout.setAnimation(animation);
}
public void loadInViewAnimation(){
setContentView(R.layout.first);
//by using findview by id here you will get root layout.
layout.setAnimation(animation);
}
You can do this by using ViewFlipper.....
See this example http://www.androidpeople.com/android-viewflipper-example
I hope this will help you to solve your problem.
You can used methods from AnimationUtils class makeInAnimation(context, boolean) and makeOutAnimation(context, boolean) to create Animation object. Config it with setStartTime and setDuration methods. Now you can call setAnimation on your view and it will be appearing or/and disappearing with your animation.
I'm trying to display a RatingBar View that is only capable of showing a one star rating, or no stars. This seems so trivial...
My ratingbar is defined like so:
My view implements OnRatingBarChangeListener and OnTouchListener
My OnRatingBarChange handler has no code.
My OnTouch handler looks like so:
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
if(this.mRating.getRating() == 0)
{
this.mRating.setRating(1);
}
else
{
this.mRating.setRating(0);
}
return true;
}
return false;
}
No matter what, the onTouch event does not succeed at setting the rating back to zero. This seems way too trivial. What am I doing wrong?
Thanks!
Consider using ToggleButton and btn_star stock drawable instead. Here's example:
<ToggleButton
android:button="#android:drawable/btn_star"
android:background="#android:color/transparent"
android:textOn=""
android:textOff=""
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
The RatingBar is extended from the SeekBar and the listener actually expects a slide action. Thus the RB can't be set to 0 (or not for this purpose) when there is only one star. The proper widget to use for this type of functionality (no surprise here) is the ToggleButton.
I had the same problem, trying to use a single star RatingBar as a favourite button. But the ToggleButton is exactly what you need for this type of thing.
This is what I implemented for the ToggleButton. (The _favourited bool was specifically used for something in my code, I can't remember now)
favourite_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!_favourited) {
// Do stuff
_favourited = true;
} else {
// Do stuff
_favourited = false;
}
}});