How to have an EditText stuck to the soft keyboard in Android - android

I'm trying to make something like the image below, where the keyboard automatically opens when the activity starts and the EditText and the send button stick to the keyboard.

Use the below code to pop up the soft keyboard automatically when an activity launches
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(youredittext, 0);
Make sure that you have not define android:windowSoftInputMode="stateHidden" in your manifest.xml.
To make an Edittext attach with the footer,use the following code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#f3f3f3"
android:paddingBottom="10.0dip"
android:paddingTop="10.0dip"
android:id="#+id/bottom_bar" >
<EditText
android:id="#+id/et_send_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10.0dip"
android:layout_toLeftOf="#+id/ib_send"
android:hint="Enter Message"
android:singleLine="true" />
<ImageView
android:id="#+id/ib_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/et_send_bar"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/et_send_bar"
android:layout_marginBottom="1px"
android:layout_marginRight="10.0dip"
android:layout_marginTop="1px"
android:background="#drawable/chatsend_bg"
android:paddingBottom="5.0dip"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5.0dip"
android:src="#drawable/ic_send_dark_normal" />
</RelativeLayout>

To show Keyboard at start of Activity you need to use like this:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateVisible" />
From Android Docs:
stateVisible
The soft keyboard is visible when that's normally appropriate (when
the user is navigating forward to the activity's main window).
To hide Keyboard at start of Activity you need to use like this:
In your AndroidManifest.xml:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
From Android Docs:
stateHidden
The soft keyboard is hidden when the user chooses the activity — that
is, when the user affirmatively navigates forward to the activity,
rather than backs into it because of leaving another activity.
This setting will hide soft keyboard when user enters new Activity (even if EditText control gains the focus). Soft keyboard will be shown only when user clicks the edit box control.

I have also found another solution for moving up layout components when Soft keyboard appers.
It can be achieved using adjustResize attribute in AndroidManifest.xml
Main purpose of adjustResize attribute is the activity's main window is always resized to make room for the soft keyboard on screen.
To show Keyboard and moved up EditText at start of Activity you need to use like this:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="adjustResize" />

i think it will works for you:
Add this statement in manifest file to your Activity:
android:windowSoftInputMode="stateHidden"
<activity
android:name="ConversationActivity"
android:label="#string/title_activity_conversations"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" />

Related

Android EditText click navigation wrong

In the pictures below you can see 2 EditText nicely standing next to each-other.
When I click on one EditText, it navigates me to the view (no problem). But when I click on the EditText next to it, the navigation send me a little upwards instead of staying on the same height. Anther problem is when I click in the previous EditText, bugging out the height and hiding the EditText from the view.
Normal
Click first (ok)
Click on the next one (problem)
Click back on the previous one (biggest problem), I end up a little below the view
Code:
<activity
android:name=".activity.ReportsEditActivity"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_reports_edit"
android:parentActivityName=".activity.MainActivity"
android:windowSoftInputMode="adjustResize|stateVisible">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.MainActivity" />
</activity>
Edittext:
<EditText
android:id="#+id/report_template_grid_single_line_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/grid_padding"
android:layout_marginStart="#dimen/grid_padding"
android:inputType="text"
android:maxHeight="#dimen/grid_element_max_height"
android:maxLines="1"
android:minHeight="#dimen/grid_element_min_height"
android:paddingBottom="#dimen/grid_element_top_and_bottom_padding"
android:gravity="top|start"
android:paddingEnd="#dimen/grid_element_top_and_bottom_padding"
android:paddingStart="#dimen/grid_element_top_and_bottom_padding"
android:paddingTop="#dimen/grid_element_top_and_bottom_padding"
android:textColor="#color/darkGray"
android:textSize="#dimen/grid_text_size_small" />
According to to developer document here.
https://developer.android.com/guide/topics/manifest/activity-element
Adjust resize keep activity's main window is always resized to make room for the soft keyboard on screen.
So you might need to use adjustPan something like this
<activity android:windowSoftInputMode="adjustPan"> </activity>
and when user hits back key you can handle the back key event in onBackpressed method to hide the soft keyboard.

Stop Decimal EditText from gaining focus on startup

I have followed this solution on preventing edit text from taking focus on activity startup. And implemented it as such
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/amount"
android:text="#string/zero"
android:gravity="end|center_vertical"
android:nextFocusUp="#id/amount"
android:nextFocusLeft="#id/amount"
android:inputType="numberDecimal"/>
Notice the android:inputType="numberDecimal". If I remove that then upon startup the focus is not taken, however if it is there then focus is taken. Is there any way to get decimal edit text to not take focus upon startup? I am using API > 23 if it is of any concern.
And just as a side note from looking at answers other people provided to similar questions, I want my field to not take focus. I do not want to only hide the soft keyboard but keep the focus.
Add this to your activity tag in manifest
android:windowSoftInputMode="stateHidden|adjustResize"
Code looks like in manifest
<activity
android:name=".ActivityName"
android:windowSoftInputMode="stateHidden|adjustResize" />
This will help hide soft keyboard at startup
And add these tags to your root layout rather than parent layout
android:focusable="true"
android:focusableInTouchMode="true"

EditText is covered by Keyboard

I am having a EditText in the hierarchy of
<CoordinateLayout>
...
<NestedScrollView>
...
<RelativeLayout> <!-- This is inside an included xml layout -->
...
<EditText
android:id="#+id/rateCalculator"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:background="#color/colorPrimary"
android:inputType="number"
android:maxLength="5"
android:gravity="center"
android:textColor="#FFFFFF"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:paddingLeft="60dp" />
</RelativeLayout>
</NestedScrollView>
</CoordinateLayout>
The EditText is being covered by the android soft-keyboard when it is being on focus! So I am unable to see what is being typed while typing!
Update:
This is my manifest code. Doesn't help!
<activity
android:name=".OfferRide"
android:label="#string/title_activity_offer_ride"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan"
android:theme="#style/AppTheme.NoActionBar" />
Here is the full doc: http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
android:windowSoftInputMode=["stateUnspecified",
"stateUnchanged", "stateHidden",
"stateAlwaysHidden", "stateVisible",
"stateAlwaysVisible", "adjustUnspecified",
"adjustResize", "adjustPan"] >
How the main window of the activity interacts with the window
containing the on-screen soft keyboard. The setting for this attribute
affects two things: The state of the soft keyboard — whether it is
hidden or visible — when the activity becomes the focus of user
attention. The adjustment made to the activity's main window — whether
it is resized smaller to make room for the soft keyboard or whether
its contents pan to make the current focus visible when part of the
window is covered by the soft keyboard. The setting must be one of the
values listed in the following table, or a combination of one
"state..." value plus one "adjust..." value. Setting multiple values
in either group — multiple "state..." values, for example — has
undefined results. Individual values are separated by a vertical bar
(|). For example:
<activity android:windowSoftInputMode="stateVisible|adjustResize" . . . >
Values set here (other than "stateUnspecified" and
"adjustUnspecified") override values set in the theme.
Flags:
"stateUnspecified"
"stateUnchanged"
"stateHidden" - The state of the soft keyboard — whether it is hidden or visible — when the activity becomes the focus of user attention.
"stateAlwaysHidden"
"stateVisible"
"stateAlwaysVisible"
"adjustUnspecified"
"adjustResize"
Or sometimes java will do that:
EditText edtView=(EditText)findViewById(R.id.editTextConvertValue);
edtView.setInputType((InputType.TYPE_NULL);
Take a look:
https://stackoverflow.com/a/6122733/4409113
Removing the gravity from the EditText made it! It seems to be a bug.
<EditText
android:id="#+id/rateCalculator"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:background="#color/colorPrimary"
android:inputType="number"
android:maxLength="5"
android:textColor="#FFFFFF"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:paddingLeft="60dp" />
You can try adjustPan, adjustResize options available for windowSoftInputMode in your AndroidManifest as follows:
<activity
...
android:windowSoftInputMode="adjustPan" >
</activity>
You can learn more about it from here
Depending on behavior you want to get, you can use android:windowSoftInputMode="adjustResize" or android:windowSoftInputMode="adjustPan" on your activity declaration in Manifest.

Spinner is cutting off dropdown list when showing keyboard

I have a Spinner as:
<Spinner
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
It's inside a activity displayed as a dialog (I don't know if it matters, but...) <activity android:theme="#android:style/Theme.Holo.Light.Dialog" ... />
It works fine, showing all the five itens.
Until the keyboard I open the keyboard. Now, the first item is cut and cannot be scrolled to it.
I tried to put a listener for click in this spinner and close the keyboard, but I got an error java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
What is the fix for this behavior?
You probably need to change the soft input mode for the activity in your manifest file. Something like this:
<activity android:windowSoftInputMode="adjustResize" ... >

Android : Showing keyboard moves my components up, i want to hide them instead

I have added a LinearLayOut having some buttons My screen is RelativeLayOut it self
Here is the code for that linear layout manager
<LinearLayout
android:orientation="horizontal"
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/Footer"
android:layout_marginBottom="5dp">
Here is the problem:
There is an EditText component on the top and it pops a soft keyboard on the screen , and brings my Footer manager on top of the keyboard and eventually SHATTERS my whole UI.
What is the exact solution?
P.S. I have removed android:gravity="bottom" and android:layout_alignParentBottom="true" one by one but with hard luck i did not get desired result.
Thanks
Add android:windowSoftInputMode="adjustPan" to manifest - to the corresponding activity:
<activity android:name="MyActivity"
...
android:windowSoftInputMode="adjustPan"
...
</activity>
You probably want
<activity
...
android:windowSoftInputMode="adjustNothing">
</activity>
That will prevent any layout changes when the soft keyboard is shown.
There is probably some confusion over this since it's currently missing from the documentation at http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

Categories

Resources