How to disable immediate focus in TextInputLayout? - android

I use TextInputLayout (+ search) in my NavigationDrawer. When user toggle this drawer, the cursor focus appears immediately on TextInputLayout field.
Is it possible not to get focus on this field until user will click on it?
Thanks !

in your root view use this attribute:
android:focusableInTouchMode="true"

None of the answers helped in my case. What I did was the following:
editText.post(() -> {
textInputLayout.setHintAnimationEnabled(false);
editText.clearFocus();
textInputLayout.setHintAnimationEnabled(true);
});
This is available starting from design support library v23.

Put this in your onCreate():
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
or add this into your manifest in your activities entry:
android:windowSoftInputMode="stateHidden"

I fixed this problem , just add another EditText and hide it
<EditText
android:layout_width="0dp"
android:layout_height="0dp" />
<android.support.design.widget.TextInputLayout
android:id="#+id/NghiPhep_TextInputLayout_GhiChu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="#style/TextHintLabel"
android:layout_marginTop="5dp"
android:focusableInTouchMode="true"
>
<EditText
android:id="#+id/NghiPhep_txt_GhiChu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorPrimaryDark"
android:hint="Ghi chú"
android:fontFamily="#font/arial_rounded"
android:inputType="textMultiLine"
android:singleLine="false"
android:textSize="18sp"/>
</android.support.design.widget.TextInputLayout>

Related

Keyboard showed when focus is on AutoCompleteTextView

I don't understand what I'm doing wrong. I would like to show only dropdown menu. The result is like the photo below: I can edit on it and keyboard is showed. What I'm missing?
Aspected (without pointer enabled):
Result:
XML layout
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/gender_container"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginVertical="10dp"
android:hint="Gender"
app:layout_constraintTop_toBottomOf="#+id/year_of_bird_container">
<AutoCompleteTextView
android:id="#+id/gender_spinner"
android:layout_width="match_parent"
android:inputType="none"
android:layout_height="match_parent"/>
</com.google.android.material.textfield.TextInputLayout>
From Fragment
private fun setAdapter() {
val genderList = mutableListOf(
Gender.MALE.toString(),
Gender.FEMALE.toString(),
Gender.OTHER.toString(),
Gender.PREFER_NOT_TO_SAY.toString()
)
val adapter = ArrayAdapter(
requireContext(), R.layout.simple_spinner_dropdown_item, genderList
)
binding.genderSpinner.setAdapter(adapter)
}
I used AutoCompleteTextView in my previous project and the usage was same. But to be sure i just created fresh project and added your code, it's working fine too.
Maybe adding android:imeOptions="actionDone" to the previous EditText might solve it, because it can be use the keyboard and when you finish with it, if it's not actionDone keyboard stays for next component.
Other than that, check about your other code parts that effect this, like onFocus or onClick events. If it is not about them i suggest you to create new project and try this again step by step to find what is causing this.
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/gender_container"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginVertical="10dp"
android:hint="Gender"
app:layout_constraintTop_toBottomOf="#+id/year_of_bird_container">
<AutoCompleteTextView
android:id="#+id/gender_spinner"
android:layout_width="match_parent"
android:inputType="none"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:layout_height="match_parent"/>
</com.google.android.material.textfield.TextInputLayout>
You can try this if you don't want to show the Softkeyboad but retain the cursor/caret. Put this in your activity
window.setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
Screenshot showing the cursor/caret active with no SoftKeyboard
I suggest you to use spinner like below. AutoCompleteTextView not a spinner actually.
<androidx.appcompat.widget.AppCompatSpinner
android:id="#+id/spinner"
style="#style/AddressSpinnerTheme"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="5dp"
app:entries="#{viewModel.uiState.value.spinnerItemList}"
app:newValue="#{viewModel.uiState.value.spinnerSelectedItem}"
app:onItemSelected="#{listener}" />

Android EditText disable edit but allow scrolling

I know that is possible to disable editing EditText.
Answers is here, and here.
But, when i disable EditText it also disabled vertical scroll in this text.
Is it possible to disable only editing, but allow scrolling?
editText.setFocusableInTouchMode(false);
editText.clearFocus();
Use this porperties in your EdittText:
android:enabled="true"
android:focusableInTouchMode="false"
android:scrollbars="vertical"
Your EditText will be enable, but not focusable for edit. Then, you will able to scroll (in vertical on this case) and will not able to edit.
I've found that just disabling the key listener, focusable and cursor seems to work:
editText.setKeyListener( null );
editText.setFocusable( false );
editText.setCursorVisible(false);
As long as you keep the edit text enabled you seem to be able to scroll the text still.
If you wrap your EditText in a ScrollView then you should be able to preserve the scroll behavior.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText ... />
</ScrollView>
editText.setKeyListener(null) should help you.
Try to use these two attributes to your editext it will make your editext only scrollable not editable
android:inputType="none"
android:textIsSelectable="true"
Below is the example
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start"
android:padding="20dp"
android:layout_margin="10dp"
android:textSize="#dimen/text_size"
android:inputType="none"
android:textIsSelectable="true"
android:textStyle="normal"
android:id="#+id/toeditext"
android:textColor="#android:color/white"
/>

How to use TextInputLayout in new android design library

Recently google introduced new Android Design Library in that how to use TextInputLayout field to enable the Floating Hint feature of EditText.
Not much guidance is available here.
This page says
you can now wrap it in a TextInputLayout
But No idea because smart prediction (Ctrl+SPACE) doesn't predicts any attributes to the TextInputLayout. So my questions are:
How do we get hold of the EditText underlying this component?
How can we get data from EditText?
TextInputLayout extends ViewGroup class.
So which means that you have to wrap your EditText in a TextInputLayout.
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hint"
android:id="#+id/editText1" />
</android.support.design.widget.TextInputLayout>
Do wrap the TextInputLayout around TextInputEditText instead of EditText.
Wrapping around EditText does have issue in landscape mode. Refer to
this article for more details.
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hint"
android:id="#+id/editText1" />
</android.support.design.widget.TextInputLayout>
This is defined in design support library under "Floating labels for editing text".
<android.support.design.widget.TextInputLayout
android:id="#+id/name_et_textinputlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_vertical_margin">
<EditText
android:id="#+id/FeedBackerNameET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="hinttext"
android:inputType="textPersonName|textCapWords" />
</android.support.design.widget.TextInputLayout>
To make this works well, you should let you app theme extends from Theme.AppCompat (or its descendant) theme, like extends from Theme.AppCompat.Light.NoActionBar.
<android.support.design.widget.TextInputLayout
android:id="#+id/til_message"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/type_message"
android:maxLines="5"/>
</android.support.design.widget.TextInputLayout>
hint will automatically shift up once you start typing in the edit text and to have those errors which appear below edit text set error on text input layout like this not on edit text
tilMessage.setError("Message field is empty!");
to disable error
tilMessage.setErrorEnabled(false);
The correct way...
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<android.support.design.widget.TextInputEditText
android:id="#+id/card_id_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/form_card_id_text"
android:inputType="number"
android:maxLength="10"/>
</android.support.design.widget.TextInputLayout>
If you use EditText like a child of TextInputLayout, you will see this message in Android Monitor:
I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
With the Material Components Library there is a new TextInputLayout component.
Just use:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
In the official doc you can find all the info.
We can use AppCompactEditText also for this need to add support:appcompat in gradle
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint"
android:maxLength="100"
android:inputType="textNoSuggestions"
android:textColor="#color/colorPrimary"
android:textSize="#dimen/font_size_large" />
</android.support.design.widget.TextInputLayout>
Rule of Thumb : TextInputLayout should wrap TextInputEditText instead of the normal EditText.
Reason?
TextInputEditText is a sub-class of EditText and is designed for use as a child of TextInputLayout.
Furthermore, using an EditText instead would shoot us a warning : EditText added is not a TextInputEditText. Please switch to using that class instead.

Why isn't my softKeyboard coming up automatically in android

Strangely, my soft keyboard is not opening automatically.On SO I have looked they seem to ask on How to hide Keyboard...?
I would like to get the keyboard automatically focues on the Text Box.
Here is what I have on my XML
android:id="#+id/msg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cursorVisible="true"
android:gravity="left"
android:imeOptions="actionDone"
android:inputType="textMultiLine"
android:maxLength="255"
android:paddingLeft="5dp"
android:scrollbars="vertical"
android:textColor="#000000"
android:textColorHint="#000000"
android:textSize="25sp" />
How to fix this?
You stated "Text box".. If you meant "EditText" than one possibility will be to see if there is any other view that is requesting the focus.
You can also try to add view.requestFocus programatically.
I suppose you need add:
android:textCursorDrawable="#null"
to EditText to see the cursor
Simply add this code <requestFocus /> To the EditText you want to be focused and it will open the keyboard for that field:
<EditText
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
Add the following line inside your Activity tag in AndroidManifest.xml,
android:windowSoftInputMode="stateAlwaysVisible"
Developer site says,
"stateAlwaysVisible":The soft keyboard is made visible 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.
reference
https://stackoverflow.com/a/1510005/1665507
http://developer.android.com/guide/topics/manifest/activity-element.html
http://blog.vogella.com/2010/10/25/android-windowsoftinputmode/
why you have to open a keyboard on a code. it will automatically get opened if you are on EditText

completely transparent EditText

i want to transparent an edittext in both Background and textColor sides. so i used this lines in my java code:
tempEditText.setTextColor(Color.TRANSPARENT);
tempEditText.setBackgroundColor(Color.TRANSPARENT);
but unfortunately this tempEditText object shows when i start typing any character on it. i want it to be completely invisible when i typing somthing to it. so i want it to be focused, get some texts, and i want the ability of retrieving strings that typed on it, but all in invisible state of edittext. i test visibility=invisible but in this situation the edittext cant give texts...
<EditText
android:id="#+id/tempEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:textColor="#00000000"
android:singleLine="true" />
any tips?
thank you...
Try:
android:background="#android:color/transparent"
or
android:background="#null"
Edit: show the virtual keyboard:
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(exampleView.getWindowToken(), 0);
Change EditText background android:background="#00000000" or android:background="#null" in your xml file.
For Text Color android:textColor="#00000000" in xml file.
Try this,
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0">
or programmatically you can set alpha to your EditText
editText.setAlpha(0.0f);
Note: The issue is user will not able to see cursor!
I did this in my XML File :
android:textCursorDrawable="#null"
android:textColor="#android:color/transparent"
android:background="#null"
Make the EditText background attribute as #bb000000 for making it transparent.
If you are on Eclipse use the GUI to do it, or do this in your XML file android:background="#bb000000".
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#null"
android:id="#+id/search_input"
app:layout_constraintLeft_toRightOf="#+id/filter"
app:layout_constraintRight_toLeftOf="#+id/search_button"
/>
use this line,then it will obviously work
android:background="#null"

Categories

Resources