Android edittext attributes - java

I have 2 edittext fields in an app.
Depending on which radio button is selected one of these fields is disabled.
The problem is that when the top field is enabled the user types in a double and then there is no option to be "done" with the keyboard because it still considers that the next field needs to be filled in even though it's disabled.
How can I change this?

Include the imeOptions and singleLine attributes in your EditText.
<EditText
...
android:imeOptions="actionDone"
android:singleLine="true" />

Related

Relation of EditText with its Id in Android Studio?

Please Read My Question Carefully
I am learning Activity LifeCycle. I only have one EditText and a TextView in my XML layout With certain id. When I rotate the screen, nothing seems to change because Rotation doesn't affect EditText. But when I remove the id of EditText in XML and rotated the screen, The Rotation Starts affecting and the text in the editText removed due to rotation. I am confused about the relation of EditText with its Id.
I tried to explain the problem below in columns:
Explanation of below Column names
Having Id: Does EditText has id?
Rotation : Phone has Rotated or not
EditText : What happen to EditText After Rotation.
.
Having Id----------------Rotation---------------EditText
Yes Rotated Does not change
No Rotated Yes, Text Removed from editText
MainActivity.java is empty and main_activity.xml code is below:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter here"
android:inputType="textPersonName"
android:textSize="26sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.371" />
<TextView
android:id="#+id/textView2"
android:layout_width="85dp"
android:layout_height="25dp"
android:layout_marginTop="88dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Sorry for using bad English and High-resolution pictures as I have no other ways. Best Regards
One of the most important aspects in Android programming is to handle the configuration changes and most common of them is orientation changes. Although it is not recommended to handle configuration changes yourself, due to the hidden complexity of handling the configuration changes, there are cases where we might need to handle manually.
If you need to save the state, the common approach is to implement onSaveInstanceState to store & restore the values which are passed as Bundle.
Coming back to your question:
Relation of EditText with its Id in Android Studio?
When it comes to saving state of View Android OS automatically calls the View.onSaveInstanceState method on each of the views in your view hierarchy as long as you call through to the super method in onSaveInstanceState. In order to restore the data, it uses android:id attribute key for that particular View’s state.So for your case, when you don't provide the id for EditText, the View's state won't be stored due to missing key.
Hope this clarifies the relationship of id to EditText
When you rotate screen on android all activities and fragments in your application are destroyed and then recreated. Android does this so that your application can reload resources based on the new configuration.
When the system destroys the activity (e.g. when the screen rotates) then although the actual Activity instance is gone, the system remembers that it existed. The system creates a new instance of that activity using a set of saved data that describes the state of the activity when it was destroyed.
when your assign an id to a view it seems that android keeps track of that view. Hence it remembers it's state.
Okay when you rotate the device this is considered as a configuration change, so the system recreates the activity and data is lost.
One way to avoid this is go to the manifest and for your specific activity add this
android:configChanges="orientation|screenSize"
here:
<activity
android:name=".YourActivityName"
android:configChanges="orientation|screenSize"
....
...
This makes the system ignore the rotation of device.
In manifest of your app, add this line to activity tag:
android:configChanges="keyboardHidden|orientation|screenSize"
The reason for this is due to Android basically destroying the activity and creating it again every time you rotate the device. This is mainly to allow for different layouts based on portrait/landscape mode.
When you use id for your component, data will be assigned to its id, but when it hasn't id, it will be recreated and data will be disappeared after rotation.
See these questions and read their good answers, I don't copy and paste them:
1- TextView's text disappearing when device rotated
2- Handle screen rotation without losing data - Android
EditText is a focused view, so in PhoneWindow, it's state will be saved automatically in saveHierarchyState() method. Only EditText with id is going to save text.
Okay, the relation of one specific EditText item to its id is: "one to one" or "one to N". One EditText item in one layout.xml has one id, if you've configured it in the layout.xml. If you've defined more than one layout.xml's and the very same EditText item (having the same id) in more than one layout.xml's, then EditText has a "one to N" relation.

Use NumberPicker as Input Method instead of standard keyboard

I have several EditText fields in my Activity. For one of those fields I like to use a NumberPicker to enter the values (e.g. age, height or whatever). Is there a possibility to display a NumberPicker instead of the keyboard once that specific editText is clicked?
I know that you can change the inputType to "text", "numberPassword" etc. in the layout file but I haven't found a possibility to make that inputType a customView.
You can set the input type of your edit text "None" and set focusable to false.
<EditText
android:inputType="none"
android:id="#+id/editText"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
And in your java file, you can set an onClickListener in your edit text, where you can show NumberpickerView in a dialog or something and from there set the value in edit text. To show number picker as a dialog, you can refer to the stackoverflow link in the comment on your question.

How to hide IME when EditText is focused

I want EditText not to extend IME when it is focused on Android Wear.
I read and implemented in the same way as the following thread:
Prevent EditText from automatically focusing
However, IME appears.
Is it impossible to hide IME for an EditText on Android Wear?
I tried 3 methods:
Added the following code in the onCreate() method:
{getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);}
Added the following code in the activity tag in AndroidManifest.xml
android:windowSoftInputMode="stateAlwaysHidden"
Added the following code in rect_activity_main.xml
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#android:color/transparent"
android:focusable="true" android:focusableInTouchMode="true"/>
None of them worked.
Best regards, gellpro
Check Show the Input Method On Demand. It's noted that the system hides the input method when the user finishes the task in the test field or the user can hide it with a system control. With this, try to specify how your UI should respond. It's mentioned that,
To declare your preferred treatment in an activity, use the android:windowSoftInputMode attribute in your manifest's element with one of the "adjust" values.
Then, to implement hiding IME, try using InputMethodManager with constant set to RESULT_UNCHANGED_HIDDEN. This will flag for the ResultReceiver result code from showSoftInput(View, int, ResultReceiver) and hideSoftInputFromWindow(IBinder, int, ResultReceiver): the state of the soft input window was unchanged and remains hidden.
Hope that helps.

Android Search icon on keyboard WITHOUT moving to next element

This may seem like a dumb question, but I want the magnifying glass on the keyboard, but I don't want the focus to go right to the next element.
I've got an EditText that populates a ListView with possible matches to the user's query. The EditText's XML includes
"android:imeOptions="actionSearch"
which adds the search icon. But it also takes the user to the first element of the ListView which is really annoying.
Is there a way to include the search icon AND not force focus to the ListView?
EDIT:
By request, my EditText xml. Sorry in advance if I mess up the formatting somehow.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editSearchWord"
android:imeOptions="actionSearch|normal"
android:inputType="text"
android:drawableLeft="#android:drawable/ic_menu_search"
android:layout_alignParentStart="true"/>

Android EditText issue

I have an editText field in my app and the keyboard pops up when I focus the text field on lower APIs than 10. For some reson, the keyboard doesnt show when the editText is focused in APIs higher than 10. Also in API higher than 10, in the logcat it says eglSurfaceAttribute not implemented whenever I focus the EditText field. Thanks in advance.
One way to bring up the soft keyboard when EditText is focused is to add <requestFocus/> to the EditText in the XML layout.
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<requestFocus />
</EditText>
Let me know if this helps.
Just put this line on your onCreate()
editText.requestFocus();

Categories

Resources