How to prevent lifting listview when keyboard goes up - android

This is code MainActivity java and main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:gravity="center"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#75F575">
<LinearLayout
android:clickable="true"
android:layout_gravity="center"
android:orientation="vertical"
android:layout_width="250dp"
android:layout_height="100dp"
android:background="#C69817"
android:id="#+id/secondLayout">
<ListView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/mainListView1"/>
</LinearLayout>
</LinearLayout>
Java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView) findViewById(R.id.mainListView1);
ArrayAdapter<String> a = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1 ,
new String [] {"item1","item2"});
lv.setAdapter(a);
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
When keyboard is shown, listview lifting up.How to prevent this.
Without listview - all normal(layout NOT go up).
Thank you.
UPDATE QUESTION with answer to FOliveira.
Unfortunately i can't remove java code (setSoftInputMode) in my real app. Java code must be and layout with listview must NOT GO UP. Try to remove listview, and you will see the layout not moving absolutely, Why layout with listview is moving? How prevent this according my conditions?

You need to add android:windowSoftInputMode="adjustPan" to your tag in the AndroidManifest.xml file.
The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.
If this option does not fit your needs, you can allways check Android documention about soft input mode

I'm using android:windowSoftInputMode="stateVisible|adjustNothing" , but this only for API higher 10(unfortunately).
Update:
After adding to listview this attributes:
android:focusableInTouchMode="false"
android:isScrollContainer="false"
I'm completely solved my problem.

Related

Android - AdjustResize won't work with external library that extends FrameLayout

I use an external library called DragListView here. This DragListView extends a FrameLayout and it contains a RecyclerView inside. The structure is like:
DragListView extends FrameLayout {
RecyclerView mRecyclerView;
}
The items populated into RecyclerView contains an EditText. With the EditText near the top of the screen (which is not covered if the soft keyboard appears) works just fine. But the one which is covered by the soft keyboard will gain focus then lose focus at once when called myEditText.requestFocus(); then the keyboard covers that EditText.
Here is the layout of my activity.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.ui.activities.ChecklistsActivity">
<include layout="#layout/custom_action_bar"
android:id="#+id/action_bar"/>
<com.woxthebox.draglistview.DragListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:id="#+id/recycler_view_checklists"
/>
</LinearLayout>
android:layout_gravity="top" works for me once with my old structure where the DragListView is a nested child inside a RecyclerView (I set the top gravity for the RecyclerView), but it's not working anymore in this case.
I cannot use AdjustPan because it will push my action bar away (but it works, sadly). Mine app is not in fullscreen (still able to see the status bar, I assume it's not fullscreen, correct me if I'm wrong please). I tried a few things but nothing work.
I called myEditText.setFocusable(true); and myEditText.setFocusableInTouchMode(true); just before myEditText.requestFocus();.
I put android:descendantFocusability="afterDescendants" for the DragListView or the root LinearLayout.
I tried different ways of calling the keyboard to popup like
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
or with a KeyboardUtils which received the Activity for getCurrentFocus();
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
And one more thing just in case. I don't know if there's anything related to my "custom" keyboard with edtSubItemName.setImeActionLabel("Done", KeyEvent.KEYCODE_ENTER); because I want a multiline EditText with "Done" action key. The xml of the EditText item is here:
<EditText
android:id="#+id/edit_text_sub_checklist_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/height_checklist_setting"
android:layout_marginRight="#dimen/height_checklist_setting"
android:inputType="text"
android:imeOptions="actionDone"
android:maxLength="256"
android:textSize="#dimen/text_size_default"
android:visibility="invisible" />
The android:visibility="invisible" is because there's a button to press to make it visible.
I'm getting mad with it for a few days. Was I missing something? Feel free to ask for more information.
Thank you all for your valuable time.
I solved it by setting layoutManager.setStackFromEnd(true); for the layout manager before setting myDragListView.setLayoutManager(layoutManager);
The list then start at the last item, so I just call myDragListView.scrollToPosition(0); after setAdapter to make it back to the top like normal. The interesting thing is that the position of the items is still the same (still from top to bottom) so there's nothing much to do.
Hope this helps someone.

Android fixed background image in scrollview while scrolling

I have a situation where I need a scrollview to have a background image that shouldn't scroll along with it's parent when moving. Before any of you suggest me the links for setting background image and this that, I have already tried and it's not working.
The whole story goes like: I have an activity with fragments which have their own backgrounds with some input fields. When focusing over input fields, keyboard appears and background image squeezes. For that I put an image on background of scrollview that fixed my issue of squeezing background but raised another concern that background Image should stay static while scrolling the parent scrollview.
The second solution any of you may suggest is setting background of my activity rather playing with scrollview. That's right, but for that I had to make a style element with background of theme which appears odd while transitioning different fragments plus it adds overhead when I have a lot of code and fragments to move forward and back.
That's the point where I am stuck. I have gone through links below, if you just need to know that I tried it or not.
link1
link2
link3
... and so on
below is the layout I am using for my fragments (it's all being done programmatically)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/top_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/backgroundView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:scaleType="fitXY" />
<LinearLayout
android:id="#+id/parent_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
</ScrollView>
</LinearLayout>
So if you guys have any better solution keeping in mind the situation I have, will be warmly welcomed. Thank you
None of the above works for me with some reason but this things works.
getWindow().setBackgroundDrawableResource(R.mipmap.img_reg_bg);
put it in oncreate method hope it will help.
If I guess right you try to fix your hotfix to get it working propperly. If I'm right you sould go back to scratch and do it without your scrollview.
If you have a look at following Link:
http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
you'll see softkeyboard mode can be adjusted. "adjustPan" should solve your problem.
"adjustPan"
The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.
Add someimgage to your drawable folderes, like drawable-mdpi, drawable-hdpi.
In your LinearLayout "#+id/top_layout" add attribute:
android:background="#drawable/someimage"

Listview demand focus when added to a layout

I want to add a ListView to a LinearLayout. But when the app start it demand focus and open the keyboard. I don't want it to do that.
I have tried both XML and Java code:
contentLayout = (LinearLayout)findViewById(R.id.contentView);
contentList = new ListView(this);
contentList.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
contentLayout.addView(contentList);
XML
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
I think that I ask the question a little wrong.
But I find out that since I have an input field, the system always want to focus in an input. So I set in the Android Manifest that windowSoftInputMode to stateHidden.
Know the keyboard doesn't open automically when the activity start.

How to hide keyboard in a edittext + listview layout

I have a LinearLayout composed by a ListView(which loads from an AsynTask) and an EditText(so I can filter that list). Here is the code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Filtrar por nombre..." >
</EditText>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
The problem is that when the activity starts, the keyboard is shown up and I only want to show it when the EditText is clicked for a search.
I've tried several methods such as writing the following in the manifest
android:windowSoftInputMode="stateHidden"
and including this in the class
InputMethodManager iMM = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
iMM.hideSoftInputFromWindow(editText.getWindowToken(), 0);
The only thing I've achieved is to not show the keyboard but not showing the loaded stuff of the list(just the EditText and the rest of the screen plain black), and once I click on the EditText, they appear both keyboard and the stuff of the listView.
Any posible solution for this?
EDIT:
I've tried the following solutions(thanks to #Sush, #boztalay and #Agata Sworowska), but none did the trick.
-Placed in my layout:
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
-And placed in my onCreate() method:
ListView listView = findViewById(R.id.list);
listView.setFocusableInTouchMode(true);
listView.requestFocus();
Any other posibility?
NEW EDIT:
I've noticed that when it doesn´t focus on the EditText, the listView doesn´t load until I focus back the EditText, llike if it was waiting for it. I've tried to set up the EditText after the code that loads the ListView but the problem persists.
This sounds like an issue with focus. What's happening is when the app is started, the EditText gets focus, and the keyboard pops up. You should try to make it so that the ListView gets focus. This way, the keyboard won't pop up until the user gives the EditText focus when they click on it.
In your Java, you should get an instance of the ListView, then call a couple of functions:
ListView listView = findViewById(R.id.list);
listView.setFocusableInTouchMode(true);
listView.requestFocus();
If you put that in your onCreate(), focus will be given to the ListView, and they keyboard won't come up until the user clicks the EditText.
See this SO question for more details.
You can just add below two attributes to your LinearLayout.
android:focusable="true"
android:focusableInTouchMode="true"
Try adding in Your layout, that contains EditText this:
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
I hope it helps.
http://www.mysamplecode.com/2012/02/android-edittext-hide-soft-keyboard.html
This is a solution I found for the issue here. Do not understand why listivew should do this, but the workaround helps.

How to disable any event on a View in Android?

My question is simple: How to disable any event on a View in Android? (including removing its focussability, like I just want it to be there visually but be inexistant on everything else)
And does it work on a whole view tree? (like if I disable events on the root, all the events will be disabled for its children?).
Now, before you say anything I have tried all the following:
setEnabled
setFocusable
setSelected
setClickable
setActivated
And none of these methods appear to work, seriously.
I have tried them directly on a WebView, as well as on the parent layout on everything but I am still able to interact with it.
Any idea?
Thanks!
EDIT#1
The solution that consists in adding a view on top of the view that needs to be disabled doesn't work. Actually, it's still possible to click on the inner view, I have tried with a simple example:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ff0000">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Click Me!"
/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
/>
</FrameLayout>
Here it's still possible to click on the button.
EDIT#2
The reason why I want to do this is related to the following question that I asked weeks ago.
What I have is a ListViewacting as a navigation bar which is underneath a View that holds the content of my app. The problem with this implementation is that when I try to scroll through the ListView when there is a focusable view in the layer on top of it, well the ListView doesn't scroll and instead it's the top view that takes focus (That's the case when there is a
Webview or an EditText etc.).
So yes as mentioned in one of the answers, I can disable any click events on a WebView by overriding setOnTouchListener but the view remains focussed and I think this is the reason why I am still having the same issue with my navigation bar.
Simply put a view on top of your view. You can toggle it on off by setting view.visibility = gone/visible.
<FrameLayout>
<WebView/>
<FrameLayout This view will be on top/>
</FrameLayout>
Edit: Just stumpled upon this link: https://stackoverflow.com/a/3856199/969325
Basically disables all touch event for the webview. Tryed that?
Edit 2 reedit: Try to set the visibility to gone for the the top view below your listview.

Categories

Resources