How to focus in a RecyclerView with TalkBack (TTS - Acessibility) - android

I have the following xml (some attributes were omitted for simplicity):
<FrameLayout>
<RecyclerView
android:id="#+id/list"
android:focusable="true"
android:contentDescription="Some content"/>
</FrameLayout>
When I activate TalkBack (TTS) to test accessibility in my app, I want to be able to click in the RecyclerView (in any item on it) so the whole RecyclerView gets focused and its content description gets read.
I've tryed:
removed any android:focusable="true" from the items in the list, alsto there is no TextView in them, so TTS cannot read them.
added android:focusable="true" to the RecyclerView. Even by making the view larger than its children and being able to click directly on it, it was not receiving any focus at all.
was able to "focus" on the RecyclerView by wrapping it with a FrameLayout and adding android:focusable="true" to it, so the FrameLayout was being focused giving the impression the whole list was focused (but this is more of a workaround).

Related

ClearFocus sets focus on the first view in LinearLayout

When clearFocus() on a LinearLayout is called, it sets focus on the first view in the Linearlayout. I am reading the android View document and it says
Note: When not in touch-mode, the framework will try to give focus to the first focusable View from the top after focus is cleared. Hence, if this View is the first from the top that can take focus, then all callbacks related to clearing focus will be invoked after which the framework will give focus to this view.
enter link description here
So, I checked IsInTouchMode on the LinearLayout before calling clearFocus, but IsInTouchMode is true.
I am wondering what is affecting this behaviour.
Use android:descendantFocusability="beforeDescendants" so that focus will be first on parent layout and then on their child/s.
Like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical" />
Let me know if it worked or not.

View invisibility when RecyclerView is empty

I am using a LinearLayout with some views inside. The second last is a RecyclerView, and the last one is an <include> tag linked with a RelativeLayout. I want the last view to be visible permanently, as I want to use it to add items to the RecyclerView.
My problem is that the <include> view below the RecyclerView disappears whenever the adapter is empty. Is there any way to avoid this behaviour?
EDIT
There is a constraint in the way I want the RecyclerView and the "add" View to work together. I would like that it feels as the "add" view is always the last item in the RecyclerView, as it happens in Google Keep lists.
Example Google Keep list example (I cannot add images yet)
You could try to replace your LinearLayout with a RelativeLayout; then you anchor the bottom view to the bottom of the screen, and the RecyclerView would be set above that view. Something like this (just a skeleton):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent>
<-! other views here -->
<include layout="#layout/something"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#bottomView" />
<RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/some_view_above_recyclerview"
android:layout_above="#+id/bottomView" />
</RelativeLayout>

Toggle autoscroll of ListView - when scrolled interactively by user

I have a real app - which you can see on the left in the below screenshot.
And I have prepared a very simple test app at GitHub, which you can see at the right:
The real app has a ListView at the bottom, which is filled with Bluetooth-related log messages. This happens pretty often, few times a second and it is impossible for the user to scroll up and look at a particular event - because the list jumps to the bottom by itself, when new events are added to the list.
My question is: How to disable autoscrolling of the ListView - when user scrolls up and how to enable autoscrolling again, when user scrolls to the bottom?
And here is my very simple test app:
MainActivity.java (filling the ListView with help of AsyncTask)
activity_main.xml (layout with Button and ListView)
As you can see the auto-scrolling is currently enabled by XML attributes in layout:
<ListView
android:id="#+id/myListView"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Okay, I have found a perfect solution for my problem:
<ListView
android:id="#+id/myListView"
android:stackFromBottom="true"
android:transcriptMode="normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
When the transcriptMode is set to normal, then:
The list will automatically scroll to the bottom when a data set
change notification is received and only if the last item is already
visible on screen.
Here is my updated test project (I've also added double tap listener to clear the ListView there)

Sizing a ListView when it's a nested Fragment

I want a ListView to fill the space available to it while still leaving room for a small footer view at the bottom of the screen. I'm trying to use a RelativeLayout to accomplish this and attempted to use the solution discussed at Limit number of rows of listview . The problem I'm running into is I'm using nested Fragments, so my ListView is actually a FrameLayout in my xml then I load a ListFragment into that frame dynamically. Given the nested fragment stipulation, how can I get my FrameLayout to "stackFromBottom" as I would with a ListView? I just need to stop the list from pushing the other View off the bottom of the screen. Thanks for your time all.
Here is the solution I came up with:
<TextView
android:id="#+id/advertisement"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="Ads will appear here"
android:gravity="center"
android:layout_alignParentBottom="true"/>
<FrameLayout
android:id="#+id/news_frag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/carousel_menu"
android:layout_above="#id/advertisement"/>
The trick was to set both layout_above AND layout_below for the FrameLayout, I had only been setting one and that was apparently allowing the layout to push it off of the screen. Also worth noting is they had to be declared in reverse order of how they actually appear on the page, so that the FrameLayout could properly reference the other View.

Android: Data list scrolling along with other components

I'm trying to show a list of data in an android activity. Normally anyone would do that with a simple ListView which I have used many times before. But now I'm having an application with a fixed header and footer, with the middle part (the content) scrolling underneath both the header and the footer. In the middle section I would like to add other components both above and below the list of data, but the entire part must be scrollable. I tried adding components (like a button, textview etc) to a listview but the lay-out builder in Eclipse won't let me do that.
So I started using a ScrollView where you can easily add any component you like. But I am not allowed to add a ListView to a ScrollView, which I can understand as it would create a strange effect (as both are able to scroll).
Next I wanted to use a TableLayout to dynamically add TableRows, but on multiple websites it is said to be slow and 'not the way to do it'. I also couldn't find an elegant way to add the seperator between each item. With a ListView that would all be done very easily.
The following image probably explaines at best the effect I want: http://tinyurl.com/bvkec5d
The table with the 'Table Data' header can possibly have a lot of items and thus can become very large in length. What I don't want is that the table has a fixed size and the items are scrollable within that table. I actually want the table to grow in size and the ScrollView containing the table should therefore be growing as well. I also want the infobox above the table to scroll along (as with any other components which might be added later).
What is the best way to achieve this effect?
You can use a simple vertical LinearLayout (or a RelativeLayout) that contains your static header and footer, and use a ListView between them. You can set header and footer views on the ListView to add the scrollable header and footer content. For simplicity of example here's the LinearLayout way:
<LinearLayout android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<!--static header content... can be any kind of view-->
<TextView
android:layout_width="match_parent"
android:layout_weight="0"
android:layout_height="wrap_content"/>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<!--static footer content... can be any kind of view-->
<TextView
android:layout_width="match_parent"
android:layout_weight="0"
android:layout_height="wrap_content"/>
</LinearLayout>
And in code, you can say:
ListView theList = (ListView)findViewById(R.id.list);
// example of creating header and footer views from inflation or by instantiation in code
View myHeaderView = getLayoutInflater().inflate(R.layout.myHeaderLayout,theList,false);
View myFooterView = new TextView(this, some layout params);
theList.addHeaderView(myHeaderView);
theList.addFooterView(myFooterView);
ListView.addHeaderView and ListView.addFooterView should enable you to add other static views (whose content could be updated dynamically) to the top or bottom of a ListView:
public void addHeaderView (View v)
Since: API Level 1 Add a fixed view to appear at the top of the list.
If addHeaderView is called more than once, the views will appear in
the order they were added. Views added using this call can take focus
if they want.
NOTE: Call this before calling setAdapter. This is so ListView can
wrap the supplied cursor with one that will also account for header
and footer views.

Categories

Resources