Unfocusable element, focusable on touch - android

I have an editText that I want to fill in automatically with info from other controls in the form, and at the same time allow to change its contents, discouraging the user from doing so though.
So I set this control to not focusable so when you press actionNext it moves on to the next control. However if you click the edit text, I want to allow the user to change its contents.
This is what I did:
mNameEditText.setFocusable(false);
mNameEditText.setFocusableInTouchMode(false);
mNameEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mNameEditText.setFocusableInTouchMode(true);
mNameEditText.requestFocusFromTouch();
mNameEditText.setFocusable(true);
}
});
However this behaves very weirdly, the result is that when you click, you can edit, but suddenly the next control (an AutoCompleteTextView) is not focusable anymore! Actually it looks like the focus remains on the edit text and goes to the autocompletetextview at the same time, like so:
How can I fix this?

Make a sub class of your text edit view
Use it in your view
Set its enabled property to false
Overwrite the touch handlers within your subclass: when the text edit view is touched, enable the view now and focus it. When the focus is lost, disable the text edit view again. Do all that in the subclass.
Style the text edit view properly so that the user has the impression that it's editable

If you want the automatic focus change to skip some views, you can use a combination of the nextFocus* attributes.
Something like :
<EditText
android:id="#+id/txt1"
android:imeOptions="actionNext"
android:nextFocusForward="#+id/txt3"
android:nextFocusDown="#+id/txt3"
... />
<!-- skipped view -->
<EditText
android:id="#+id/txt2"
android:imeOptions="actionNext"
... />
<EditText
android:id="#+id/txt3"
android:imeOptions="actionNext"
android:nextFocusUp="#id/txt1"
... />
nextFocusForward is the one used for actionNext. I believe the other attributes are mostly useful in non touch mode (e.g. with a hardware keyboard)

Related

Strange behaviour of edittext when receiving focus

I have 2 layouts with EditText widget in them. In the first one EditText automatically receives focus on activity's start but shows no software keyboard (this is the desired behaviour). In the second layout, however, EditText is focused and shows soft-keyboard. I examined both layouts and can't find a reason for this behavour, because both widgets have identical properties. Soft-keyboard should only be displayed when I touch the input field. Am I missing something?
<EditText
android:id="#+id/scanLine"
android:layout_width="253.0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="19.0dp"
android:imeOptions="actionDone"
android:singleLine="true"
android:inputType="text"
android:maxLines="1"
android:focusableInTouchMode="true" />
Add the following line to the onCreate() method of your activity:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Hope it helps!

Android inputtype troubles

What i have to do:
I'm implementing some kind of fancy calculator for special measurements. For this, i have a ListView with sever edit texts.
Per row are tow EditTexts where the user shall be able to enter decimal values.
What my problem is:
I have set inputType both in code and xml, but i get weird behavior out of this. After tapping any number, the normal keyboard pops up for like half a second, then switching back to the decimal keyboard.
I guess the problem comes from me calling requestFocus() on the currently active EditText. But in the way my Calculator needs to work, i have to do this, since every change from the user results in a recalculation of my values (therefore, updating the whole list view --> results in loss of focus which shall be prevented)
Important Code Snippets so far:
<EditText
android:id="#+id/TextValue"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_weight="0.45"
android:background="#E6E6E6"
android:inputType="numberDecimal"
android:hint="#string/Ung"
android:textAlignment="viewEnd"
android:layout_gravity="end"
android:paddingRight="5dp"
/>
(Same code for second EditText in row)
RequestFocus is called in the getView method of my ArrayAdapter, since Reloading --> Loss of Focus --> ...
....
if (position == currentTag) {
textValue.requestFocus();
}
....

How do I show the Phone Input softkeyboard when my activity starts?

Between the Activity tags in my AndroidManifest.xml I have :
android:windowSoftInputMode = "stateVisible"
So when my activity starts the soft keyboard comes up:
Is there not something I can add to make the phone input keyboard come up instead? Or how would I do it? Thanks.
EDIT
For the sake of clarity to this question I'm adding this 'edit'.
First of all, thanks for all the answers below.
However, my objective is to have no Edittext Visible on the screen, at first. The Phone Input soft keyboard comes up as soon as the activity starts and when the user starts
to type then the Edittext will become visible, with the text being typed.
I tried making the Edittext invisible, and then make it visible when user starts typing, but the problem is an invisible edittext can't have the focus,
So, android:inputType="number" was having no effect. And neither was: edittext.requestFocus(); Because the edittext was invisible.
What I ended up doing eventually was something very simple, setting width and height to 0dp in my xml like :
android:id="#+id/etPhoneNumber"
android:layout_width="0dp"
android:layout_height="0dp"
android:inputType="number" />
That way, the edittext can get focus with
edittext.requestFocus();
as soon as the activity is created, and inputType = "number" is recognised.
The next thing I'll do is increase the size of the edittext as soon as a key is hit on the soft keyboard, so users can see it.
I'll put all this in as an answer so it might be helpful to other users. +1 for the help!
Assuming that you have an EditText in the Activity which you show once your app is started, you can add below XML Attribute to your EditText.
android:inputType="phone"
Check developer docs for other input types in case if you need.
You can add
android:inputType="phone"
or
android:inputType="number"
to your View in your XML layout.
You should have some View in your code where you can give input like EditText or AutoCompleteTextView for changing the type of the keypad.
For example -
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="phone"
/>
In manifest file set the property in activity,
android:windowSoftInputMode="stateAlwaysVisible"
and then in your xml where your EditText set the property,
android:inputType="number"
first of all take an EditText in your xml
<EditText
android:id="#+id/etPhoneNumber"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number" />
use this piece of code in the onCreate method of your activity !
EditText etPhoneNumber = (EditText) findViewById(R.id.etPhoneNumber);
etPhoneNumber.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
This works for me very well ! Hope it works for you too !
Let me know if this works ! :)
The objective is to have no Edittext Visible on the screen, at first.
The Phone Input soft keyboard comes up as soon as the activity starts and when the user starts
to type then the Edittext will become visible, with the text being typed.
I tried making the Edittext invisible, and then make it visible when user starts typing, but the problem is an invisible edittext can't have the focus,
So, android:inputType="number" was having no effect. And neither was: edittext.requestFocus(); Because the edittext was invisible.
What I ended up doing eventually was something very simple, setting width and height to 0dp in my xml like :
android:id="#+id/etPhoneNumber"
android:layout_width="0dp"
android:layout_height="0dp"
android:inputType="number" />
That way, the edittext can get focus with
edittext.requestFocus();
as soon as the activity is created, and inputType = "number" is recognised.
The next thing to do now is increase the size of the edittext as soon as a key is hit on the soft keyboard, so users can see it.

Android, How to select editor segment of TimePicker?

As you know time picker has three segments, two buttons on top and bottom of view and an editor in between.
By tap and holding editor, we can select its contents. The problem that I have is I want to select content of editor just by taping (not tap and hold). Is it possible?
Try something like this in layout file
<TimePicker
android:id="#+id/activity_main_timepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dip"
android:layout_marginRight="16dip"
android:focusable="true"
android:focusableInTouchMode="true" />
Unlike most other controls, EditTexts are focusable while the system is in 'touch mode'.
The first click event focuses the control, while the second click event actually fires the
OnClickListener. If you disable touch-mode focus with the android:focusableInTouchMode View
attribute, the OnClickListener should fire as expected.

Style a Button to look like an EditText (but still behave like a Button)

I realize this is sort of a weird thing to be doing, but I have a button that needs to look like an EditText but still behave like a button. My layout XML currently looks like this:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/editTextStyle" />
That gives it the appearance of an EditText, but also messes with the behavior a little bit by preventing the onClick event from being fired unless the button has focus (effectively making it require two clicks). Is there any way to keep the style without changing the button's behavior?
I thought about just making a nine-patch background that looks like an EditText, but with so many different Android versions and skins out there I'd rather use the system style if it's possible.
How about an EditText that behaves like a button?
<EditText android:id="#+id/Edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="true"/>
You can define an OnClickListener for it too. And it won't get focus.
If it absolutely needs to be a button, you can add a focus listener to your button that fires the onclick when the button receives focus.
weirdoButton.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus) {
weirdoButton.performClick();
}
}
});
The downside is that the click will fire when the button receives focus via a trackball or d-pad. Make it un-focus-able in the layout file:
<Button
android:id="#+id/weirdoButton"
android:layout_width="match_parent"
android:layout_height="50dip"
android:focusable="false"
style="?android:attr/editTextStyle" />
The accepted answer does let you style EditText as a button, but I found a discrepancy in it; when you long press on it, it lets you paste text into it which is terrible in terms of UX.
This is what I applied instead to style it as an EditText.
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:focusable="false"
android:id="#+id/btnAddMember"
style="?android:attr/editTextStyle"
android:focusableInTouchMode="false"
android:clickable="true"
android:hint="Button Content"/>

Categories

Resources