Android: Switch loses focus after click - android

Hey Hey currently I am developing an application that contains a form, with some edittext fields, some checkboxes and a switch at the end. The layout looks like this:
<RelativeLayout>
<ScrollView>
<LinearLayout>
<LinearLayout>
<TextView></TextView>
<Checkbox></Checkbox>
</LinearLayout>
<LineraLayout>
<TextView></TextView>
<EditText></EditText>
</LinearLayout>
<LinearLayout>
<TextView></TextView>
<Switch></Switch>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
So now, if I click the switch, the switch looses its focus and the scrollview scrolls to the first form element, the checkbox. I already tried to use the descendantFocusability attribute with "blocksDescendants" in the upper LinearLayout of the Switch, but it doesn't work.
Anyone got an idea how to solve this issue?

Okay, I was able to fix the problem by myself. I'm posting my solution, so that it could be helpful for other people who might have the same problem.
In my code there was a specific function, which calls the setVisibility(View.Visible) method for all my form elements (this is used in my project to display possible dependend form elements, that should only show up, when the user clicks on a checkbox or sth else).
If I click on the switch or a checkbox my code calls the setVisibility-Method for all the form elements, also for the Edittext. This requests the focus and so the scrollview scroll this element to the middle/focus of the screen.
I solved the problem by not calling the setVisibility method for objects, that are already visible.
Hopefully this could be helpful for someone else ;-)

Related

GridLayout is overlapped, when the VISIBILITY of some of its elements is set to GONE

I am developing an app on Android with this form :
<ScrollView>
<RelativeLayout>
<TextView/>
<RelativeLayout>
....
</RelativeLayout>
<gridLayout>
7 CheckBoxes
1 ImageButton
</gridLayout>
<LinearLayout>
many TextViews
</LinearLayout>
</RelativeLayout>
</ScrollView>
The ImageButton inside the gridLayout controls when some of the checkBoxes disappear (VISIBILITY->GONE) and when it's pressed (the ImageButton) the LinearLayout, which is just below it (the gridLayout), overlaps the gridLayout before it takes its final form. See the images below :
[1]: https://i.stack.imgur.com/K6aSc.png This is before the disappearance.
[2]: https://i.stack.imgur.com/nHqpj.png This happens during the disappearance.
EDIT: The problem happens because the View.GONEtakes some time to be performed visually, but the gridLayoutunderstands that it happens immediately, so it reserves the "empty" space right away. Only think I can figure out, is to set the View.INVISIBLE instead and resize the gridLayout manually. Is there a better way?
EDIT2 The problem is caused by animateLayoutChanges="true". Due to the animations, it take some time for the checkBoxes to disappear. Is there a way to force the linearLayout to "wait" for the effect of the animation to end?
Any ideas?
You can add a listener to your animation changes of animateLayoutChanges="true" and move the LinearLayout when the animation ends. More information in this thread - Listner
Also, you can try a combination of setAlpha(0) and setEnabled(false) on your checkboxes in which case gridLayout won't adjust the views again.

adjustPan ScrollView above included layout

Hey there I'm dealing with some kind of problem showing the softkeyboard in some android layout the layout ist organized like this:
<RelativeLayout>
<ScrollView>
Some stuff here...
</ScrollView>
<include ... />
<RelativeLayout>
I habe already set adjustPan and I have tried some experimental stuff with focusing. But my content inside the ScrollView is getting hidden by the included layout everytime its getting focus.
It may be just a logical problem but how do I tell e.g.: my EditText which is in the center of some other stuff like TextViews, ViewPager etc. to be above the included layout when gaining focus ?

How to click a view that is behind a scrollview

I have to an EditText behind a ScrollView
I cannot click the EditText anymore. The ScrollView is intercepting it I think. I tried the code below but it doesn't fix it.
Any suggestions?
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:layout_alignParentTop="true" >
Things to try:
The parent view can influence the position of the views. For example
in a FrameLayout all of the views get stacked in the order they are
placed in the layout file (so your issue could be fixed by
rearranging the order of the EditText in relation to the
ScrollView).
You might try to call editText.bringToFront() to force the
EditText in front of the ScrollView.
If you disable the touch-based listeners (OnClickListeners, etc) on
the ScrollView or the container it's in then the action should "pass
through" the ScrollView and allow you to interact with the EditText
behind it. I found this out the hard way when I was trying to make
the opposite scenario work for me (I wanted the top level view to
consume the actions and not fall through to the view behind it).
These solutions worked for me in the past in different scenarios.
In my case I replaced android:layout_height="match_parent" to android:layout_height="wrap_content" and views inside ScrollView become clickable. Hope this helps.
My code:
v<ScrollView
android:layout_width="match_parent"
android:layout_height="WRAP_CONTENT"
android:id="#+id/svTabActions">
<View>...</View>
</ScrollView>
Try placing the editText inside the scrollView.

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. :)

HorizontalScrollView Inside an AlertDialog

I am adding custom views to a custom ArrayAdapter. The xml layout I am using looks like this
<LinearLayout>
<HorizontalScrollView>
<LinearLayout>
<TextView></TextView>
<TextView></TextView>
<TextView></TextView>
<TextView></TextView>
</LinearLayout>
</HorizontalScrollView>
<LinearLayout>
I then set the adapter for my AlertDialog.Builder as my custom ArrayAdapter and also supply the OnClickListener. My dialog shows fine and I am able to scroll the list vertically as well as scroll each view horizontally. However, when I click on any of the views it never fires the OnClickListener.
I have tried setting the top most LinearLayouts descendantFocusability="blocksDescendants" but that has no effect. Is there anyway to do this within the AlertDialog? I have tried even creating a custom Dialog that implements OnGestureListener but none of the gesture events ever get called either.
I'm not sure if I'm able to reproduce the same problem you're describing, though I can reproduce something similar. Perhaps if you were to post a more complete and working code example, it could help to replicate your problem.
At any rate, the solution might be to not register the child components to have onClickListeners, because when they have onClickListeners, the parents then don't receive the click events.

Categories

Resources