I am writing an app that has a ListView of text. In each row there is a combination of text and links. I added an OnItemLongClickListener to the list and made the links clickable with
my_text.setMovementMethod(LinkMovementMethod.getInstance());
The problem is, if I am able to click the link, I cannot get the textView to register the LongClick.
I have tried different methods to make the links clickable such as
android:autoLink="web
But this doesn't make the links clickable.
My question is: Is there a different way that I should set the LongClickListener, or a different way to make the links clickable?
Has anyone else encountered a similar problem?
I should note that right now the OnItemLongClickListener makes the links register the LongClicks but this causes a problem when a textview doesnt have a link or a user tries to longClick somewhere else in the view (besides the link)
Code:
I programatically inflate a relativeLayout containing a list view and add it to a view flipper:
RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
android:id="#+id/my_list"
style="#style/listViewSimple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f9f9f9"
android:cacheColorHint="#00000000"
android:divider="#ccc"
android:dividerHeight="1px"
android:fadingEdge="none"
android:fadingEdgeLength="30dp"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:persistentDrawingCache="none" >
</ListView>
</RelativeLayout>
Adding everything programatically:
RelativeLayout list = (RelativeLayout) LayoutInflater.from(MyActivity.this).inflate(R.layout.my_list_layout, null);
vf.addView(list);
ListView lv = (ListView) list.findViewById(R.id.my_list);
lv.setOnItemLongClickListener(listLongListener);
Making Links clickable(in getView() of adapte):
my_tv.setText(Html.fromHtml(formatted_text)));
my_tv.setLinkTextColor(linkColor);
my_tv.setMovementMethod(LinkMovementMethod.getInstance());
This makes links clickable but seems consume all clicks before they get to the textview. I tried to add a longClickListener that returns false but that didn't seem to have any affect.
My temporary fix is to set a itemLongClickListener and an onItemClickListener to the listItem and just display a dialog with the clickable text in onListItemClick. This works but adds an extra click for users to interact with the text
I would create a custom Adapter, set your ListView to use that Adapter, and then in getView, set a View.OnClickListener on the TextView that has your text and a View.OnLongClickListener on the TextView that has your link. Remove the existing listeners you referred in your question so that they don't conflict.
My temporary fix is to set a onItemLongClickListener and an onItemClickListener to the listItem and just display a dialog with the clickable text in onItemClick. This works but adds an extra click for users to interact with the text.
If a better answer is posted I will change selected answer
Related
I'm trying to make ListView in a widget where there are multiple clickable items in each row. For example, if each row has data containing a link, one button in the row to open the link and another to share the link.
Sample code:
Widget layout
<LinearLayout...>
<TextView../>
<ListView with id="list_view" />
</LinearLayout>
Each list row in the ListView has:
<LinearLayout with id="listRowParent">
<TextView../>
<Button with id="openBtn"/>
<Button with id="shareBtn"/>
</LinearLayout>
I'm able to set onClick of each row using
remoteViews.setPendingIntentTemplate(R.id.list_view,
*<PendingIntent>*); //in onUpdate
and remoteView.setOnClickFillInIntent(R.id.openBtn, *fillIntent*); // in adapter
which works fine, but I can't have setPendingIntentTemplate for a list row button by doing remoteViews.setPendingIntentTemplate(R.id.shareBtn,
);
since nothing happens on clicking the button if I do this.
Question:
Is what I'm attempting possible? If yes, help is greatly appreciated :)
My bad. I should be using
remoteViews.setPendingIntentTemplate(R.id.list_view,
*<PendingIntent>*);
for both the buttons, but I was trying to use the button's ID directly in the second. Got fixed.
My PostFragment.java implements AbsListView.OnItemClickListener, and I get the
onItemClick event without a problem on every item, unless there's a link on the TextView: lblListItem.
The link on the TextView can be clicked without a problem and open a website, but the background of the list cannot be clicked and so the onItemClick is never called.
Example of a link string: http://www.google.com
I have this TextView on my list_item.xml:
<TextView
android:id="#+id/lblListItem"
style="#style/TextView.ListItem"
android:text="Text."
android:layout_below="#+id/imgUser"
android:layout_marginBottom="#dimen/margin_small"
android:autoLink="all"
android:linksClickable="true"
/>
I think that this is relevant, on my adapter I have some items that receive:
view.setOnClickListener()...
How can I make the list item clickable at the same time the link for the website is clickable?
Try adding android:descendantFocusability="blocksDescendants" to the root view in your row layout. I've done some testing with this and it seems to work.
I write a customized item layout for a listview. The layout has many widgets and some will have its own clicklistener.
When I click the row, sometimes the listview' s onListItemClick work, but sometimes not.
After I spent some time searching, I find a way, setting android:descendantFocusability="blocksDescendants" in the layout' s root. It works, but only one textview cannot work,(the clickable, fucusable attr have been tried). In the list' s adapter, I set the textview' s onClickListener, it works. But that' s not nature, does anyone know how to solve it?
btw, is there a way to distinguish diffderent widget' click? Now, no matter what I click(except that textview), the whole row' s background selector got changed.
many thanks!
// my list row layout root
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:descendantFocusability="blocksDescendants"
android:padding="#dimen/medium"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
// the awful textview
<TextView
android:id="#+id/text"
android:textColor="#android:color/secondary_text_light"
android:textColorLink="#android:color/holo_blue_dark"
android:layout_marginTop="#dimen/small"
android:textAppearance="#android:style/TextAppearance.DeviceDefault"
android:layout_below="#id/nick"
android:layout_alignLeft="#id/nick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
update
the textview uses Linkify to provide some links functionalities.
After spenting some time trying, I solved both.
the first. if your customized listview' s layout has a textview which has been set autolink or Linkify in the code, the click event in the textview won' t affect the list' s row. You have to extends your own TextView and override onTouchEvent method. please see it here
the second. just set in the customized listview' s root node: android:descendantFocusability="blocksDescendants", and then, in your widget which will have its own onClickListener, set android:clickable="true", it works for me.
hope it useful for those you encounter the same:-)
I need to show a list of items, the items are read from a database, and it is possible there is no item, in this case, I just want to show a TextView saying "there is no item", I think I could implement this by using relative layout, both list and text are in center of parent, they are displayed alternatively, but is there any way better than this?
Thanks!
Adding to Aleadam , Bill Mote
You may call at any time AdapterView.setEmptyView(View v) on an AdapterView object to attach a view you would like to use as the empty view.
The snippet is as follows:
empty = (TextView)findViewById(R.id.empty1);
list = (ListView)findViewById(R.id.list1);
list.setEmptyView(empty);
Make sure you keep the ListView and TextView inside the same parent.
For detailed description please refer this
If you're using a ListActivity, that is the default behavior. If not, then you can set the ListView visibility to GONE and a TextView visibility to VISIBLE.
Aleadam is right. Here's an example XML in support of his answer:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#drawable/red"
android:gravity="center_vertical|center_horizontal"
android:textStyle="bold"
android:text="#string/error_no_groups"
/>
</LinearLayout>
The first tutorial on the Android developer website explains how to do this:
http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html
(Look under Step 4)
Snippet:
The ListView and TextView can be thought as two alternative views, only one of which will be displayed at once. ListView will be used when there are notes to be shown, while the TextView (which has a default value of "No Notes Yet!" defined as a string resource in res/values/strings.xml) will be displayed if there aren't any notes to display.
The View with the empty id is used automatically when the ListAdapter has no data for the ListView. The ListAdapter knows to look for this name by default. Alternatively, you could change the default empty view by using setEmptyView(View) on the ListView.
I have Created a ListView and set a background image.Everything goes fine except I am unable to click on the right side of the row of ListView.Can anybody help me this.
Below is the xml file...
<ListView
android:layout_width="wrap_content"
android:id="#+id/JSONListView"
android:layout_height="wrap_content"
android:background="#drawable/listbg"
android:focusable="false"
android:cacheColorHint="#00000000"
android:visibility="visible" >
</ListView>
And I am not using any View.
Use the attribute
android:layout_width="match_parent"
and set click listener for items.
I guess you have created your custom adapter. If so, just go to the getView() method of that custom adapter and on the holder instance of that portion of listitem add a onCLickListener.
Provide more info. XML layout, etc.
Im guessing there is a view there or it is not filling the entire width but difficult to say.
Android ListView Activity