Lock Focus inside a ListView on a TV Layout - android

I'm trying to find a way to lock the user inside a ListView (When using the D-Pad) preventing it from losing focus, allowing the user to navigate only through the ListView until "exit" is explicitly called.
There's any easy way to do this?

Found!
If you need this, it will be enough to set those parameters in xml:
android:id="#+id/myListView"
android:nextFocusDown="#id/myListView"
android:nextFocusForward="#id/myListView"
android:nextFocusLeft="#id/myListView"
android:nextFocusRight="#id/myListView"
android:nextFocusUp="#id/myListView"
Of course myListView is just an example id, and you could use this technique with any kind of View, it locks the focus setting the next focus to itself, in any direction.
The user is then locked inside that view until the view disappears or the focus is programmatically moved to another view.

Related

Preventing the on-Click listener from calling itself on hidden views in Android app

I've design the user interface of my app as a collection of views, not activities or fragments. I manage the showing and hiding of views on this collection of views which are right on top of each other.
I believe I chose the wrong design principle to use for my app. The problem I am having is that when I hide a view in order to show another one; the button that resides on the hidden view fires its on-Click listener.
After reading some of the Android Reference documentation, I see that the view tree will navigate down the hierarchy of views in order to consume the touch event. It is then that the on-Click listener of this button is called.
What I need is something to prevent the button from the hidden view to stop calling itself when the user touches the shown view. I know that calling setVisibility(View.INVISIBLE) stops the on-Click listener from being called.
I was thinking of the ViewTreeObserver class that will allow me to create a listener of view hierarchy that somehow allows me to get a handle of all the hidden views in order to call setVisibility(View.INVISIBLE) on the hidden views.
Any ideas or advice
When you do setVisibility(View.INVISIBLE) for a view, it will still present in the layout. So instead of setVisibility(View.INVISIBLE), try setVisibility(View.GONE).
This will remove the view completely from the layout including the onClickListner attached to it.
When you want that view back, you can use setVisibility(View.VISIBLE) to get it back.

EditText in RecyclerView view is detached when keyboard opened

I am attempting to create a long form with a variable number of EditText elements inside a RecyclerView. The layout consists of a FrameLayout, a RecyclerView for the form sections, then a nested RecyclerView inside of a CardView to hold all of the form elements for that section (see animation below).
Normally when an element is focused the keyboard will appear, the window is resized, and the view is scrolled so the element appears above the keyboard. The issue I am having is when an element is selected at the bottom of the screen and top of a section the window is resized and the RecyclerView detaches the section's view, causing the EditText to not exist when it tries to focus. This manifests itself as the keyboard popping open momentarily then closing as focus is returned to the FrameLayout.
I am using android:windowSoftInputMode="adjustResize" in my activity and do not want to use adjustPan because it does not provide the best user experience.
I've tried adding a click listener to the EditText then scrolling the RecyclerView up so that the view won't get destroyed when the window resizes but this feels hacky and it is hard to detect precisely how far up the RecyclerView should be scrolled. You also get some weird jumps. In order to do this you have to turn off the focusability of the EditText which isn't ideal either because it breaks navigation and accessibility.
Trying to do something like recyclerView.getRecycledViewPool().setMaxRecycledViews(0, SECTION_COUNT); doesn't work either because it doesn't prevent the views from being detached.
How do I keep the view from being detached when the window is resized for the keyboard so the EditText receives focus?
I solve this with by
android.os.Handler().postDelayed({recycle.smoothScrollToPosition(0)},100)

How to add a Layout

Hello I want to add a LAyout to my Activity.
I have a FrameLayout where I add a SurfaceView so far.
Now I want to add another Layout as a menu over it so when I click a Button on my Surface View I set it to be Visible.
This layout should contain one Textview and should be scrollable
1: What is the best LAyoutout thing for this approach? I thought of a Listview where I add Strings dynamically
2: How can I achieve this in Code?
It should be:
Scrollable.
Should be a certain Size: Half of the screen width and half of the screenHeight
So it also should be at the position screenWidth/4 and screenHeight/4
To achieve this I can set these parameters inside my mainThread that is attached to the surfaceview so I have the parameters needed allready in Pixel format that's no Problem.
It should loose the Focus when the User taps outside of the view so my surfaceView gets the Focusagain and set the LAyout to Gone.
So it should go like this:
User taps the button to display the menu
Now the User can scroll through the Menu
When he presses the Backbutton or outside of the View It should close itself
When there is a new Text that should be displayed it should be attached to the layout to be ready to be displayed at the next time the user clicks the button again
Thank you
1: What is the best LAyoutout thing for this approach? I thought of a Listview where I add Textviews dynamically
That sounds good to me, you can go with either ListView or RecyclerView. You are basically not adding TextViews dynamically, you create an adapter that will do handle creating and recycling of views for you.
2: How can I achieve this in Code?
Create a DialogFragment, it's the best candidate for such dialogs with logic. Its lifecycle is handled via FragmentManager, so you won't have issues on screen rotations and so on. It allows you setting whatever layout you need just like any other Fragment. It will be placed in the center for you, so you don't have to handle that manually. Just set the size of the dialog you want and it will look perfect.
It should loose the Focus when the User taps outside of the view so my surfaceView gets the Focusagain and set the LAyout to Gone.
I don't really get this one. When user touches outside, do you need to lose the focus but the dialog should not be closed? If yes, then this is already answered here:
Allow outside touch for DialogFragment
If the dialog should be closed, you don't have to implement anything, it works like that by default.

Android - how to keep an AutoCompleteTextView from keeping focus

I have a pretty complex layout that has multiple types of views and I have one AutoCompleteTextView that keeps focus. I have relativeLayouts which I keep set to GONE and then when I expand them the screen shoots back up to the autoCompleteTextView which is annoying when you are way down the view. How can I keep the AutocompleteTextView from keeping focus without setting it to not being able to take focus because when you do this you cant click on it to write in it. I tried setting it to not take focus and adding an onclick listener where I basically said if not in focus take focus but that didnt work. I would like to be able to have it so I can start the activity with the AutoCompleteTextView having no focus and then when someone clicks on it they can write in it and when they are done it loses focus. Any help on the matter would be much appreciated.
Place this on its parents layout (like linearLayout):
android:focusable="true"
android:focusableInTouchMode="true"
This way LinearLayout will take focus when user join your activity and the keyboard will not show.

Android: Pass selection to Parent

I optimizing an application for use with the trackball. I have a ListView with rows that contain three buttons. Here is my problem: whenever I move the trackball to the child buttons of the row, the focus disappears from the parent container (the row). I want to keep the focus on the entire row, as well as whatever button is currently selected for the given row.
Basically, what I need is an attribute opposite to android:duplicateParentState, something called "android:duplicateChildState." Unfortunately, android:duplicateChildState does not exist.
At the moment I have hacked it in a way such that I set an onFocusChangedListener and force the parent container to also be selected with the ListView.setSelected method. This works, but unfortunately there is a flicker as this setSelected message is posted and run to the UI thread.
Does anybody know of a way that I can keep this focus on the parent, as I traverse the children widgets?

Categories

Resources