OnItemClickListner is not working android - android

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.

Related

Android - Drag and Drop Between 2 Activities

Let's say:
Activity1 contains some items which will be dragged
Activity2 contains some "boxes" which will be filled with Activity1's item. The boxes already have drag listener.
I want to drag item from Activity1 into Activity2
I tried these method:
I implement onLongClickListener() on Activity1 items and use startDrag() there. Then I open Activity2. I could make the drag shadow appear but the boxes cannot receive the item. In fact, they doesn't respond to any DragEvent.
I implement onLongClickListener() on Activity1 items, but only to pass the data into Activity2. Then I use startDrag() when Activity2 start(specifically in onResume()). Here, the shadow not appear and the boxes doesn't respond to any DragEvent.
Is there any way to make this possible?
its possible using two fragment of view pager instead of 2 activity and you can set logic of drag and drop in onTouch() or dispatchTouch().
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return super.dispatchTouchEvent(ev);
}
using dispatchTouch() in activity of fragment or
viewPager.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
using touch event of view pager you can set drag and drop logic within inside.
I hope this idea can help you.

Cannot open Android Webbrowser using Action_View

I am trying to open the standard android webbrowser by clicking on a textview.
I defined the android:autoLink="web" in the textview and then use an onTouchListener to start a browserintent:
// On Touch Listener
chatText.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View view, MotionEvent event) {
// view.performClick();
view.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN){
view.performClick();
openBrowser(chatText.getText().toString());
}
return false;
}
});
// Start Browser function
public void openBrowser(String url) {
Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (webIntent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(webIntent);
}
}
However, everytime I click a link on my textview I get the error:
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
Although I do have the flags added to my newly started activity, anyone knows what I do wrong here?
I am calling the activity from a class ChatArrayAdapter which extends an ArrayAdapter, however, I pass the appropriate context to the newly started activity
Going over your code, your chat text has some URL in it. To make things easier, Android has Linkify class and the android:autoLink XML attribute. These help to automatically highlight links and perform the standard action when you click on them. All of this is handled out of the box.
Your chatText is probably a TextView. You can use one of the two ways I mentioned like so:
In XML:
In your chatText's XML add the following attribute:
android:autoLink="web"
In code:
Linkify.addLinks(chatText,Linkify.WEB_URLS);

Forward touch event to dynamically attached view, without lifting finger

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.

Ontap event in android

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
}
}
}

How to set animation when I changes content in same activity

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.

Categories

Resources