Im having difficulty to understand the unique behavior I found.
I have a layout with only a ListView, where its item layout is some texts & editText.
Showing such a dialog onscreen leads to an unexpected behavior - keyboard wont pop-up when giving focus to it.
I strangely find that if I add another element to the dialog's layout, the issue is solved.
Dialog layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/listView"
/>
<!-- patch. otherwise, EditText within listview wont pop keyboard. weird, right? -->
<EditText
android:layout_width="wrap_content"
android:visibility="gone"
android:layout_height="wrap_content" />
</LinearLayout>
Item layout:
..
...
....
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="5"
android:maxEms="5"
android:editable="true"
android:numeric="integer"
android:id="#+id/purchaseAmount" />
....
...
..
Can anyone give an explanation to that behavior?
Perhaps the ListView does not have focus. You can ensure this by modifying your xml slightly.
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/listView">
<requestFocus/>
</ListView>
Related
I want to make a chat screen which has an EditText and a Button at the bottom (wrapped in a LinearLayout), and a ScrollView for the messages at the top. The problem is that when the user taps the EditText and the keyboard opens, the ScrollView will go off screen as a result of moving up. I would like to move the Layout (with the EditBox and the Button) above the keyboard and the ScrollView to be resized to fit the screen. The problem is shown on the pictures below:
As you can see, the line that writes "Chat" (left image) has disappeared in the right image.
I use this XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15sp"
android:orientation="vertical"
android:background="#ccdcf7"
tools:context="com.orionz.sockettest.ChatActivity">
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="15"
android:isScrollContainer="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/chatBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Chat\n"
android:textColor="#000000"
android:textIsSelectable="true"
android:textSize="22dp" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="horizontal">
<EditText
android:id="#+id/msgInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:ems="10"
android:hint="Message..."
android:inputType="text" />
<Button
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:background="#drawable/mybutton"
android:padding="5sp"
android:text="Send"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
I have seen many answers such as setting the android:windowSoftInputMode but none has worked. I use Android Studio. Any help would be appreciated.
Try to replace the ScrollView with a ListView like this :
<ListView
android:id="#+id/list_view"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
store all the messages in an ArrayList, and set an adapter to the ListView you made :
ListView listView=(ListView) findViewById(R.id.list_view);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ArrayList);
listView.setAdapter(adapter);
Finally, add a listenner on your Button writing these lines :
EditText editText= (EditText) findViewById(R.id.msgInput);
ArrayList.add(editText.getText().toString());
adapter.notifyDataSetChanged();
Finally, I found what was wrong in my case.
The theme was full screen. In the style of the activity I had this line that caused the problem:
<item name="android:windowFullscreen">true</item>
In the java code, I gave a command to open the keyboard on the start of the activity:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
which was also part of the problem
After adding the adjustResize property to the activity in the manifest, everything worked fine:
android:windowSoftInputMode="adjustResize"
Wrap Scrollview to the whole layout.
i.e place editText and button inside Scrollview and set scrollView Height to android:layout_height="match_parent"
I have a relative layout which contains only the edit text view. But when content of edittext increases, my title bar gets shifted upwards. Most of the solutions I went through addresses the issue of shifting title bar when keyboard pops out. But I didn't found anything in case its content increases. I also used android:isScrollContainer="false" as suggested in this post How to avoid soft keyboard pushing up my layout?, but still the same issue. Is there a way to prevent it? Here is my xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="false"
android:background="#drawable/background" >
<EditText
android:id="#+id/hidden_edit_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="flagNoExtractUi"
android:inputType="textMultiLine"
android:singleLine="false"
android:paddingLeft="10sp"
android:paddingTop="5sp"
android:paddingRight="5sp"
android:scrollbars="vertical"
android:background="#android:color/transparent"
android:textSize="26sp" >
</EditText>
</RelativeLayout>
I managed to solve this issue by putting edittext in scrollview and replacing adjustPan with adjustSize in the manifest. This is the final working layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background">
<EditText
android:id="#+id/hidden_edit_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:imeOptions="flagNoExtractUi"
android:inputType="textMultiLine"
android:paddingLeft="10sp"
android:paddingRight="5sp"
android:paddingTop="5sp"
android:singleLine="false"
android:textSize="25sp" >
</EditText>
</ScrollView>
try to run your code after removing :
android:background="#android:color/transparent"
make your relative layout height and width to wrap_content.......... probably works. since when text increases your complete layout shifts. Try it and please rpy
Usually this can be solved by adding the android:windowSoftInputMode="adjustPan" property to your activity in the AndroidManifest file.
I have an Activity with a simple layout, a kind of 'faux' web browser with an EditText for the URL input, defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="6dp"
android:background="#drawable/activity_title_bar">
<EditText
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:maxLines="1"
android:scrollHorizontally="true"
android:hint="#string/browser_hint_url"
android:text="#string/browser_url">
</EditText>
<ImageButton
android:id="#+id/button_refresh"
android:layout_width="50dp"
android:layout_height="match_parent"
android:src="#drawable/browser_button_refresh">
</ImageButton>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<ImageView
android:layout_width="882sp"
android:layout_height="1532sp"
android:src="#drawable/browser_background">
</ImageView>
</ScrollView>
</LinearLayout>
The problem I am having is that when the Activity is started, be default the EditText is selected and the soft keyboard activated. I'd like for nothing to be selected to start, and thus not have the soft keyboard active, but can't figure out how to do this via XML.
Any suggestions?
Thanks, Paul
android:windowSoftInputMode=[ "stateUnchanged", "stateHidden",
"stateAlwaysHidden", "stateVisible",
"stateAlwaysVisible",
"adjustResize", ] >
Use some of this in your manifest, this will hide the automatic keyboard pop up. However, if you are using your EditText for some input at time you'll need the keyboard. :)
stateHidden will do the work
get a reference to one of your other views, either the ImageButton, or the LinearLayout. Then in your onCreate() method call view.requestFocus(); on the non-EditText view and it will steal focus away and keep the keyboard from being shown.
I am trying to implement the following:
Please check the image here:
The problem is whenever the user taps the compose mail edittext field, the send/save/discard buttons are hidden by the softkeyboard.
Any ideas on how to make those buttons always visible?
Thanks.
[EDIT]
Finally got it solved!Had to use Linearlayout instead of RelativeLayout and assigning proper layout_weight parameter did the trick. Thanks a lot Femi for pointing out that the buttons need to be outside of the ScrollView.
Here is the final working layout xml in case anyone finds it helpful:
<?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"><ScrollView android:layout_alignParentTop="true"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<EditText
android:id="#+id/editTextCompose"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:gravity="top"
android:singleLine="false" android:lines="5"
android:inputType="text"
android:layout_below="#+id/editTextSubject"
>
</EditText>
</RelativeLayout>
</ScrollView><TableLayout android:id="#+id/recipeButtons"
android:background="#B0B0B0" android:padding="3dip"
android:stretchColumns="0,1" android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow android:gravity="center">
<Button android:id="#+id/editOtherAdditionButton"
android:text="Save" />
<Button android:id="#+id/removeOtherAdditionButton"
android:text="Delete" />
</TableRow>
</TableLayout></LinearLayout>
As documented in #mayra's answer to Android soft keyboard covers edittext field you should see http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft for the details to control.
Modifying the Activity's entry in the AndroidManifest to adjustResize should I think do it.
There is another question about this here, but it looks like the best approach is using the windowSoftInputMode
I have a layout that uses an EditText to let users search a database and populate a ListView. The EditText is about 2/3 of the way from the top of the screen (positioned over an ImageView, and followed by some intro text.)
The problem is that the soft keyboard hides the EditText, so the user can't see what he's typing. (I disabled the auto-suggest.)
I've tried LinearLayout, RelativeLayout, paddings, and different alignments/centerings, but I still can't get it to work right. The EditText is either hidden, gets pushed off the top of the screen, or get "squished" and distorted.
Suggestions???
One possible workaround is to move the EditText to the top of the screen. However, this deviates from the graphic design that I was given.
Another possible workaround is for me to make the soft keyboard open in full screen (not sure how, though). This will still hide the EditText, but then I can re-enable the auto-suggestion so the user can see what he's typing... sort of... because he can only see the suggestions for what he's typing.
Here's my latest attempt. See "introFrame".
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout android:id="#+id/titleContainer"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="#string/title_string"
android:textSize="15sp" android:textColor="#FFFFFF"
android:textStyle="bold" android:paddingLeft="5dp"
android:layout_height="fill_parent" android:layout_width="wrap_content" />
</LinearLayout>
<FrameLayout
android:id="#+id/introFrame"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<ImageView
android:src="#drawable/main_search_image"
android:scaleType="center"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="140dp" >
<LinearLayout android:id="#+id/introSearchContainer"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<EditText android:id="#+id/intro_search_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint=" Enter keyword "
android:imeOptions="actionGo"
android:inputType="textFilter"
android:maxLines="1" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button android:id="#+id/intro_search_button"
android:background="#drawable/custom_button_go"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<TextView
android:text="#string/search_intro"
android:textSize="15sp"
android:textColor="#FFFFFF"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</LinearLayout>
</FrameLayout>
<LinearLayout android:id="#+id/listContainer"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/itemlist" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:cacheColorHint="#00000000" />
<TextView android:text="No data found" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center"
android:textSize="16sp" android:textColor="#FFFFFF" android:id="#+id/android:empty" />
</LinearLayout>
</LinearLayout>
What you're looking for is the Activity's windowSoftInputMode attribute. You set this in your AndroidManifest.xml file, and give it a value such as:
adjustResize: "The activity's main window is always resized to make room for the soft keyboard on screen."
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."
adjustResize will probably work for you, as long as you wrap the layout in a ScrollView. It may have negative effects if you have a bitmap image in the background, as it will be resized as well, in which case you may want to use adjustPan instead.
<activity android:windowSoftInputMode="adjustResize" />
or
<activity android:windowSoftInputMode="adjustPan" />
More information is available at the above link.
What has worked for me is to drop my highest-level LinearLayout in a scrollView (the ScrollView can only have one child). This allowed the entire activity/form to scroll up and not clutter the EditText in focus.
First, I set in my activity:
<activity android:windowSoftInputMode="adjustPan" />
Then I did the ScrollView thing I'm talking about:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- STUFF -->
</LinearLayout>
</ScrollView>
just use following in manifest...
<activity
android:windowSoftInputMode="adjustPan">
try giving the edit text a layout_weight (i.e. layout_weight=1) , this may have some other effects on your other layout items that you may have to work through, but this may help it stay visible when soft keyboard pops up