Multiple clickable items in ListView inside android widget - android

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.

Related

Android: How to make Grid view with clickable grid items and nested views (buttons,Image) by remote(For Android TV)

How to make Grid view with click able grid items and nested views (buttons,Image) by remote(For Android TV)
I just want to click able image and button in grid view item should add to play my list if I navigate on button by Remote navigation not by touch
Thank you!!!
How to navigate through d pad in grid view sub items if grid view item have two sub items image button and another button
It is no difference between touch screen or remote control for methods like onClickListener or etc.
For remote control, you need to focus on the element first, than you can click the element.
I have not use the gridView on TV before, but I have used the recyclerView and scrollView. I think they have same situation.
I've try the default simpleAdapter to create the gridView, and it's no problem with the nested views that I can focus and click each grid items after I set the onItemClickListener to the gridView.
I guest your problem is that the grid item conflicts with the button in the grid item.
To avoid this situation, you need to set android:descendantFocusability="blocksDescendants" for the container of your custom grid item view. It will block the items statement in that parent layout.
If you want the button change in the different statement, you can give the button this parameter: android:duplicateParentState="true".
That will let the button follow it's parent's statement.
By the way, don't forget to set some changes to different statement for the grid items (background changes or etc), or you will not know which grid item you have focused :P.
Hope this will help you :)
PS. The following is the sample code for the custom grid item layout. Maybe the example can help you to understand what I mentioned easily.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:duplicateParentState="true"/>
</LinearLayout>
2015.01.06 23:11 update:
How to navigate through d pad in grid view sub items if grid view item
have two sub items image button and another button
For this case you should not use OnItemClickListener. Instead, you should make your grid item unfocusable and unclickable, and than set OnClickListener for both two buttons.
You can set the parameters in your customize adapter for the gridView. In getView method you can set each of the grid item view just like convertView.focusable(false) and convertView.clickable(false).
After that, you can set the click listener and give the method you want to do for the buttons in adapter.
Don't care about the D-pad action. Actually, D-pad will work automatically if there are elements can be focused.
The key point for this question is the conflict between grid item and its child elements. You could have just one part to be focused: grid item (the parent view), or the buttons (the child views).

LongClick textView, clickable link

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

android How to add list view in a scrollview

I have a layout requirement like below,
Textview
TextView
ListView
Edit Text
Button
Since listview cannot fit in landscape, I want to have list view onwards (ie. listview, edittext and button) to be a scroll view.
I know listview cannot be used inside a scrollview, but is there a way to do that ?
Any working example will be appreciated.
99% of android developers think we should not use ListView inside a ScrollView because both are scrollbale views and only parent can be scrollable, so it wraps the ListView.
Its 100% correct. But we have to use tricks to avoid this and to achieve our requirements.
I found one trick in web, which is setting the height of ListView based on the list items. Just check the link below, you will get an example code to calculate the height of ListView to fit inside a ScollView.
Android ListView height calculation to fit in ScrollView
The problem with this code is the list view will be filled entire screen if more children are available.
You have to use below template to achieve solution to your requirement.
<ScrollView >
<LinearLayout vertical>
<TextView />
<TextView />
<ListView />
<EditText />
<Button />
</LinearLayout>
</ScrollView>
I saw one video on youtube, Android ListView inside a ScrollView which is showing we can limit the height of listview, can be scrollable and used inside a ScrollView. I don't know how the programmer achieved that.
I am also thinking to produce same result by avoiding above example code. I hope it may help you temporarily. Please let me know if you got solution.
The better solution for this kind of layout is that You should use relative layout and fix ur EditText and Button at the bottom of ur screen like i have in my list view(see the image below) so that you wont need to add ScrollView in ur layout.
Just do this
<ScrollView>
<LinearLayout>
<ListView>
</LinearLayout>
</ScrollView>
Then add your
EditText
Button
Sort of a round about way to do what you want to do without a scroll view.
Write a custom adapter for your ListView
Assume you have an array of n elements that you want to populate the ListView with and then the EditText and the Button. So number of elements will be n+2
In the getView for the position n+1 return a view which has an EditText box instead of the normal list item
For the n+2 position return a Button.
Don't try to wrap around a ListView with a ScrollView, you will need up with lot of issues.
Note: I have not tested this, not even sure if it will work. Do let me know if it works. :)

Android: what is the best way to show a TextView in case if no item in a ListView?

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.

Clicking on the entire row of Android 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

Categories

Resources