Android Studio - Soft keyboard 'Enter' making text disappear - android

When I'm using my app to enter text into an editview, when I click the Enter button on the soft keyboard the text entered is disappearing. Ideally I would like the app to go to the next edittext box when the user clicks the button as it does on a different form, however the XML is the same for the button so unsure as to why there is a difference
To try getting the enter button move to the next edit text I've tried inputting the following into the XML but had no luck so far
android:imeOpions="actionNext";
XML For Control As Requested
<EditText
android:layout_width="0dp"
android:layout_height="40dp"
android:id="#+id/text_name"
android:layout_weight="1"
android:visibility="invisible"
/>
Comment: Removed Line above as didn't work

I created a basic activity a simple layout ( shown below). When I press enter in the edit text, it goes to the next edit text.
I added the code you mentioned
(android:imeOpions="actionNext";)
but I also had to add:
android:singleLine="true"
EDIT: singleLine is deprecated.
You can simply add
android:inputType="text"
to your editText.
That worked for me. I think the reason that the line was disappearing was because it was being shifted up and out of the bounds of the textview. The EditText thought you wanted to create a new line. That's my guess.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.slipperyslope.aperturelabs.nextfield.NextEditTextActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example"/>
<!-- This way works!-->
<EditText
android:id="#+id/first_edittext"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:inputType="text"
/>
<!-- This way works! But it's deprecated-->
<EditText
android:id="#+id/second_edittext"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:singleLine="true"
/>
<!-- dummy edit text just to transition to. -->
<EditText
android:id="#+id/third_edittext"
android:layout_width="100dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
Let me know if this works or if you are still having issues.

Related

Android cannot deselect TextView

I'm making a simple test app to wrap my head around reading text from TextViews in android.
My problem is: I have 2 TextViews, one for a username and one for a password field, and a button. The button calls a method that gets text from both TextViews. However, my username TextView is automatically selected when the app runs and I can type text into it, and I cannot deselect it and select the password field to write into, but I can click the button. Does anybody know what the issue might be?
Android 7.0 used for testing
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/userField"
android:layout_width="#dimen/textView_width"
android:layout_height="#dimen/textView_height"
android:layout_above="#id/passField"
android:layout_marginHorizontal="#dimen/activity_horizontal_margin"
android:layout_marginVertical="#dimen/activity_vertical_margin"
android:background="#color/LightCoral"
android:gravity="center_vertical"
android:hint="#string/user"
android:inputType="text"
android:textAlignment="gravity"
android:textSize="10pt"
android:visibility="visible" />
<TextView
android:id="#+id/passField"
android:layout_width="#dimen/textView_width"
android:layout_height="#dimen/textView_height"
android:layout_centerInParent="true"
android:layout_marginHorizontal="#dimen/activity_horizontal_margin"
android:layout_marginVertical="#dimen/activity_vertical_margin"
android:background="#color/LightCoral"
android:gravity="center_vertical"
android:hint="#string/pass"
android:inputType="textPassword"
android:textAlignment="gravity"
android:textSize="10pt"
android:visibility="visible" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/passField"
android:layout_centerHorizontal="true"
android:width="#dimen/button_width"
android:height="#dimen/button_height"
android:background="#color/FireBrick"
android:onClick="uradi"
android:text="#string/button_tag"
android:textColor="#color/Black" />
</RelativeLayout>
EditText is better widget over TextView for handling user input. Replace your TextView with EditText
You should choose the controls that are suitable for the use of the sense.
The input of the username and password is generally used for EditText

How to retain the hint text in android EditText field?

In my android app, I have a hint text in my EditText view. Once the user starts typing, I want the hint to be displayed on top of the edittext field. The original hint text disappears when user starts typing which is fine. How do I place a text on top of the edittext field ? Is there a way to make the EditText field multiline and use the first line for hint? But the user input should always start from the second line.
Thanks in advance for your help!
Just found that they are called floating inline labels in android.
http://www.google.com/design/spec/components/text-fields.html#text-fields-floating-labels
But looks like API 21 doesnt support this yet.
There new widget provide by android called TextInputLayout it is the standard way. Below is an example of it:
<android.support.design.widget.TextInputLayout
android:id="#+id/first_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/first_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hello"/>
</android.support.design.widget.TextInputLayout>
You can use FloatLabelLayout. It's a custom component that works as described in the google documentation.
Once you create the custom component you can use it in this way:
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<your.package.FloatLabelLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:floatLabelTextAppearance="#style/TextAppearance.YourApp.FloatLabel">
<EditText
android:id="#+id/edit_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/account_username_hint"
android:singleLine="true"
android:inputType="textNoSuggestions"
android:imeOptions="actionNext"
android:nextFocusDown="#+id/edit_password" />
</your.package.FloatLabelLayout>
<your.package.FloatLabelLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:floatLabelTextAppearance="#style/TextAppearance.YourApp.FloatLabel">
<EditText
android:id="#+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/account_password_hint"
android:singleLine="true"
android:inputType="textNoSuggestions"
android:imeOptions="actionDone" />
</your.package.FloatLabelLayout>
</LinearLayout>

text keyboard appear even if I have set inputType to number edittext android

I want to display the number keyboard.
Here is my code to do that. (This is an item of my list).
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/checked_list_item_text"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/checked_list_item_quatity"
android:layout_centerVertical="true"
android:textSize="30sp" />
<EditText
android:id="#+id/checked_list_item_quatity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="7dp"
android:gravity="center_vertical"
android:hint="Quantité"
android:focusableInTouchMode="true"
android:inputType="number"
android:maxLength="6"
android:textSize="30sp" />
</RelativeLayout>
When I click on edittext, the keyboard displays the numbers but switches quickly to text and I have to click a second time to get the number keyboard.
This issue occurs only the first time when I open the activity.
Try changing the input mode in your AndroidManifest.xml
I had the same issue when trying to get focus to an edittext field thats inside a listview. Adding android:windowSoftInputMode="adjustPan" in the activity holding the listview solved the problem for me.
<activity android:name=".MyEditTextInListView"
android:label="#string/app_name"
android:windowSoftInputMode="adjustPan">
What you have should work , but it could be something on the code side. Maybe you can force it programmatically:
EditText checkedInput = (EditText) findViewById(R.id.checked_list_item_quatity);
checkedInput.setInputType(InputType.TYPE_CLASS_NUMBER);

EditText fails to display inputted text the second time on Android 2.2

I have an EditText which is contained in a ListView. Generally this works as it should, as I've set android:descendantFocusability="afterDescendants" to enable the user to focus the EditText in order to input text.
On devices running Android 2.2, there is a problem that goes like this:
User taps edit text, enters text
User dismisses soft keyboard (at this point the entered text disappears)
User taps edit text again, and attempts to enter text - text does not appear (hint text is visible)
If the user then dismisses the keyboard and taps the edit text a third time, the text that was entered previously but did not appear will now be visible
This occurs on two different 2.2 devices, but does not occur on a Galaxy Nexus running 4.0.1, nor does it occur on the emulator.
Here's the XML layout which contains the edit text:
search_bar.xml
<RelativeLayout
android:id="#+id/search_section"
android:layout_width="fill_parent"
android:layout_height="#dimen/search_bar_height" >
<ImageView
android:id="#+id/search_bar_bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/searchbarbg" />
<EditText <!-- This is the EditText in question -->
android:id="#+id/search_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/searchfieldbg"
android:hint="#string/search_view_hint" />
<ImageView
android:id="#+id/scan_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/scan_button" />
</RelativeLayout>
<ImageView
android:id="#+id/search_bar_shadow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/search_section"
android:background="#drawable/shadowtop" />
</RelativeLayout>
Here's the listview which contains the above layout as a row:
browse_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/main_bg" >
<ListView
android:id="#+id/categories_list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:descendantFocusability="afterDescendants"
android:divider="#null"/>
</RelativeLayout>
no need to change adapter just enter the code shown below:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

cannot see button next to search box android

I'm having trouble when showing a button next to the search box. I'm using an auto complete text view and i don't know if this is due to the auto complete text view. How can i fix this. Here's what i got so far:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<AutoCompleteTextView
android:id="#+id/autocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
/>
<Button
android:id="#+id/search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go"
/>
</LinearLayout>
To fix this, just add the tag android:layout_weight=1 to your AutoCompleteTextView.
This happens because android noticed you set the layout_width to fill_parent on your AutoCompleteTextView, and then it has no space for the Button left. Setting the weight to 1 means the textview will be "generous" and give as much space to any other components as they request, which in this case is limited to the width of the Button after is wrapped.
You are filling the parent width with your AutoCompleteTextView (thus pushing the Button out of screen width). Try to change this
<AutoCompleteTextView
android:id="#+id/autocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
/>
into this
<AutoCompleteTextView
android:id="#+id/autocomplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
/>
And your Button should become visible. Here is a nice resource conserning layouts: Android Developer | Declaring Layouts

Categories

Resources