How can I make sure my EditText always has focus? - java

Whenever an activity starts, I want to make sure my EditText has focus and has the cursor inside. Basically I want to make sure the keyboard is up.

In the EditText tag in your xml layout, add the tag
<requestFocus />
You may also do the programatically by calling the method. editText.requestFocus.

Related

How to set a SpannableString to an EditText in an Android Material TextInputLayout?

I can't find a way to set the text of an EditText in a Material TextInputLayout to a SpannableString and have the styling actually show up on screen. I've tried every combination of EditText inside a TextInputLayout, e.g.:
AppCompatEditText
EditText
TextInputEditText
with every way to create a SpannableString, e.g.:
HtmlCompat.fromHtml(string, HtmlCompat.FROM_HTML_MODE_COMPACT)
SpannableString(string).setSpan(...)
TextUtils.stringOrSpannedString(string)
with every way to set the text of an EditText, e.g.:
edittext.setText(spannedString)
edittext.setText(spannedString, TextView.BufferType.SPANNABLE)
edittext.append(spannedString)
The crazy thing is they all work when the EditText is not inside a Material TextInputLayout, so it must be a problem with that. More information: the SpannableString is respected when you set the hint or placeholder text on the TextInputLayout, just not the text of the EditText inside it. I looked through the TextInputLayout source code, but didn't see anything wrong. Thanks to anyone who can help.
I figured it out right when I was going to give up. You can get the Editable object from the EditText and then apply the Span to it. Here's how:
mEditText.text.setSpan(...)
Surprisingly easy when you know where to look.

Change the look of an EditText prgrammatically

I'm making an app that works as follows:
when you click a button, there will appear an EditText and a Button and a TextView.
I have already done that, but the style is not the same as the style when I create the EditText in xml (it looks different, the colour is not the same).
There will always be a couple of EditTexts, so the user will see the difference very well.
I also prefer the colour of the EditText when I create it in xml.
How can I change the style of an EditText programmatically?
Thanks in advance!
http://i.stack.imgur.com/24dzI.png (picture from the difference)
The issue here is that the background is being set differently between the two fields. You can just use setBackground(...) to have it match the other fields.
If you need to get the proper background drawable, look up one of the existing fields, grab the background from there, and assign it to your dynamic field.
http://developer.android.com/reference/android/widget/EditText.html
You can use EditText.setTypeFace() or EditText.setTextAppearance(), depending on what you're trying to change. The link has all the methods that EditText can perform.

How can I stop further text input in EditText on a button click and keep focus?

I am using a Button to check text entered into an EditText. After the button click event I don't want to allow any more text to be entered.
I previously used setEnabled(false) but the problem with this is the software keyboard is then closed, which means the user has to reopen it which is very inconvenient (the process in a loop is essentially: text entry --> button1 (stop text entry) --> button 2 --> text entry etc...). I tried to avoid this by adding in the code:
setEnabled(false);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
But, I notice this creates a short flash sometimes as the keyboard is quickly minimized and then restored again.
There must be another way to stop additional text alteration without losing the focus of the EditText?
Given the situation you are describing, the only thing I can think of would be to store the value and restore it at the end of your operation (as opposed to disabling the text box). It's not a very correct approach, but it should work...
You could use an InputFilter :
int currentTextSize = editText.getText.toString().length();
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(currentTextSize);
editText.setFilters(FilterArray);
and to remove it :
editText.setFilters(new InputFilter[] {});
You could of course declare those filters as static in your class if they are to be used often... This might not be the best way to achieve this but it's the first I thought of ;)
I managed to implement a solution to this that I was happy with, although its not a particularly elegant solution. For anyone who might stumble across this in the future:
I created the following EditText in my xml which provides an EditText to temporarily receive focus and means the keyboard remains open. Note the width and height are set to 0px so it is a hidden object.
<EditText
android:id="#+id/switcher"
android:layout_width="0px"
android:maxLength="1"
android:layout_height="0px"
android:textSize="10sp"/>
Then in my button click event I added the following code:
EditText attempt= (EditText) findViewById(R.id.attempt);
EditText switcher= (EditText) findViewById(R.id.switcher);
switcher.requestFocus();
attempt.setFocusable(false);
This moves the focus to hidden EditText and then stops the attempt text object from being able to be edited again. It can then be removed on another action with:
attempt.setFocusable(true);
attempt.setFocusableInTouchMode(true);
attempt.requestFocus();
Perhaps switcher.setFocusable should be changed between the two (but in the opposite way)

Android - Hidden but Select-able EditText

I would like to have a EditText in my Android app that you can not see, however you can select and type text into. In other words I would like it so that it can not be seen at all however other than that it behaves completely normally.
Another option is to have an image or button on the screen which the user presses to enter text into the EditText which is hidden behind another EditText.
Thanks for the help, however it is looking more likely that I will have to use the second option. To elaborate on it, for example I have a TextView that says 'Welcome'. I would like the user to be able to click on this text to bring up the keyboard and edit in the EditText field. The reason for having the EditText field hidden behind another is to cover up the cursor while making it seem the user is typing the text which appears on the screen.
If you would like to make the EditText fully invisible (even what the user types in) , but still be able to retrieve the entered data through myEditText.getText().toString(),
you could add:
android:textColor="#android:color/transparent"
android:background="#00000000"
android:cursorVisible="false"
if I'm getting you correctly. Here is how you can accomplish the first option of yours by setting the background to transparent and cursor visibility to false.
To check this you have to click at the center of screen.
e.g. code snippet:
<EditText
android:id="#+id/eT1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#00000000"
android:cursorVisible="false"
android:ems="10" >
</EditText>
Set transparent Background to EditText.
android:background="#00000000"
Sorry but I didn't understand your another question.

How to stop soft keyboard appear by default in android

I am writing an android application with two EditText on the main activity. My problem is that once the main activity starts the soft keyboad automatically appears. I want the soft keyboard to appear only when the user clicks on the EditText component. How can I accomplish this?
In your activity, in your onCreate method, put the following
edittext.setFocused(false);
And then afterwards (in a different method, maybe an onClickListener for your edittext) put
edittext.setFocused(true);
Also you could put the below line in you manifest, in the the activity section, just below the android:name attribute
android:windowSoftInputMode="stateUnchanged"
Try this,
<EditText android:id="#+id/edit_text_id"
..
android:focusable="false" />
if you want to manage the inpt events , take a look on this part :
==> http://developer.android.com/guide/topics/ui/ui-events.html

Categories