I have custom keyboard in my app for Android. It's layout described in xml like this
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"">
<Row>
<Key android:keyLabel="F1" android:keyOutputText="F1"/>
<Key android:keyLabel="F2" android:keyOutputText="F2"/>
<Key android:keyLabel="F3" android:keyOutputText="F3"/>
...
So, i'm insteresting how i can disable, for example 'f1' key ~ make it grey and untouchable.
There are some similar questions here, but all about default soft-KB.
I know I can iterate through keys like this
for (Keyboard.Key key : myKeyboard.getKeys())
but it's look like objects of Keyboard.Key class are useless for any changes in key's look.
UPD: I did not found solution. I implemented keyboard manually - big relative layout, common buttons and custom buttons and everything fine. By the way - custom keyboard at least more beautiful. Just copy resources from droid 4+ - and you'll get nice modern transparent buttons and transparent layout on every platform.
I am currently using android:horizontalGap to place a black gap (with the width of a key) in the place where the disabled key should be placed (in this way, I can hide keys not allowed in each screen, because my keyboard has always the same distribution). It's a poor workaround, though.
The main problem is for the rightmost keys, since android:horizontalGap can only be set for the left side of a key. But using an android:keyWidth="0" attribute in a rightmost fake key and then setting there the proper android:horizontalGap, does the trick. An even poorer workaround...
Any better solution?
Usually the keyboard is only visible if you are editing something, so you can trap the edits via the object. If its an editText box then you can add a listener and then you could disable any response to the edit text. I'm not sure if this is useful to you but at least you can trap any unwanted input.
// add a keylistener to keep track user input
editText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and "enter" is pressed
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
// do some thing or nothing
return true;
}
return false;
}
});
If Key is a Button, then it's simple:
key.setEnabled(false);
Otherwise, just make it not clickable:
key.setClickable(false);
...and change its' appearance (background color, text color, etc.). The simplest way to do this is:
key.setBackgroundColor("0xFFAAAAAA");
Related
I've been trying to adapt my TextInputEditText which shows a numeric keyboard currently (it has android:inputType="numberDecimal" in the xml) to allow input of a / character (by clicking a button in the ui that appends a / to the EditText).
What I want to achieve: A numeric keyboard & allowed to enter / in the EditText
Everything I have tried either doesn't show a numeric keyboard or doesn't allow me to add the / to the EditText.
Things I've tried:
Adding android:digits="0123456789./" to the xml
Result: It's still a numeric input and won't let me .append("/")
Setting android:inputType="text" in the xml and the below in the activity's onCreate()
input.setFilters(new InputFilter[] {(src, start, end, dst, dstart, dend) -> {
if(src.toString().matches("[0123456789./]+")){
return src;
}
return "";
}});
Result: I don't get a numeric keyboard (although I can only enter the characters I need which is good)
android:inputType="text" with android:digits="0123456789./" as this seems to be suggested quite a few times on various SO answers (and I assume must have worked at some point).
Result: a non-numeric keyboard (i.e. regular text input keyboard)
Removing android:inputType and android:digits from the xml and also adding the below to the activity's onCreate() seems to do the trick.
input.setKeyListener(DigitsKeyListener.getInstance("0123456789./"));
However it's not completely clear to me that this should or will always work - see note below from the docs on DigitsKeyListener...
As for all implementations of KeyListener, this class is only concerned with hardware keyboards. Software input methods have no obligation to trigger the methods in this class.
Source: https://developer.android.com/reference/android/text/method/DigitsKeyListener
I have a series of dynamically generated EditTexts in an Android Activity. They are generated based on the below XML:
<?xml version="1.0" encoding="utf-8"?>
<EditText
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:textCursorDrawable="#null"
/>
In some circumstances I want to disable them but let them remain visible. I am doing this by setting their focusable property to false (I have reasons for using focusable instead of enabled):
if (view2 is EditText)
{
((EditText)view2).Focusable = false;
}
When I want to reenable the EditText I set the focusable property to true:
foreach (EditText disEditText in m_disabledEditTexts)
{
disEditText.Focusable= true;
}
However, this does not make the EditText respond to the user touching it. I have spent all morning fiddling with this and I haven't been able to solve it! How can I make the EditText responsive again after setting focusable to false.
If the EditText does not have focusable set to false it works fine.
This is written in C# / Xamarin.
You need to also add the FocusableInTouchMode property so it can be focused again in touch mode. Thus said, you should probably set FocusableInTouchMode to true after you've set Focusable to false.
Focus Handling
The framework will handle routine focus movement in response to user input. This includes changing the focus as views are removed or hidden, or as new views become available. Views indicate their willingness to take focus through the isFocusable() method. To change whether a view can take focus, call setFocusable(boolean). When in touch mode (see notes below) views indicate whether they still would like focus via isFocusableInTouchMode() and can change this via setFocusableInTouchMode(boolean).
Focus movement is based on an algorithm which finds the nearest neighbor in a given direction. In rare cases, the default algorithm may not match the intended behavior of the developer. In these situations, you can provide explicit overrides by using these XML attributes in the layout file:
nextFocusDown
nextFocusLeft
nextFocusRight
nextFocusUp
To get a particular view to take focus, call requestFocus().
Reference: http://developer.android.com/reference/android/view/View.htm
I have three EditText widgets in my app and the XML for one of them is below. They work, mostly, but the problem is that the actual widget is at the bottom of my devices screen. When I tap the widget, and the numeric keypad is displayed, it completely covers the text entry window and I can't see what number I'm typing. Can I modify TextView so that when the Keypad is displayed on my device, it also displays the number that I'm currently typing?
<EditText
android:id="#+id/EditTextPrices" android:layout_width="100dip"
android:layout_height="wrap_content" android:inputType="number|numberDecimal"
android:singleLine="true" android.imeOptions="actionDone"/>
Another problem is that when I finish typing one number, instead of returning me to the main activity screen, it brings up the numeric keypad for the next TextEdit widget. I don't want this to happen. I had thought that setting android.imeOptions="actionDone" would cause the keypad to go away and be done when I finished typing the number, but not so. How can I stop the 'next' window from appearing?
Look into adding android:windowSoftInputMode to your manifest file. "adjustPan" as it's value is probably what you want though I've found that it sometimes fails to account for the space taken just above a softkeyboard by the strip of word-guesses while you're typing things in.
Add android:windowSoftInputMode=”adjustPan” to your manifest for the activity. That will cause the activity to scroll up so that the focused edit text is always on screen.
The other advice given to use adjustPan is good, and another idea is to put your layout in a scroll view.
Regarding your second problem, how are you handling actionDone?
For example this is how I hide the keyboard (or do whatever you want when they press done).
final EditText editTextPrices = (EditText) findViewById(R.id.editTextPrices);
editTextPrices.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView view, int actionID, KeyEvent event) {
if (actionID == EditorInfo.IME_ACTION_DONE) {
InputMethodManager imm = (InputMethodManager) MyClass.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(bugemailaddress.getWindowToken(), 0);
// DO OTHER HANDLING OF INPUT HERE
return true;
}
return false;
}
});
I'm looking for a possibility to customize my keyboard-keys. In the following picture you can see the result i'd like to reach:
keyboard-key result
Optional: When I multitap on a key, the keyPreview should step through the list of characters of this key (nice to have).
Problem:
If I use the label-attribute of a key, the text of the label only appears in the center of a key. But I need to put the A in the top-left-corner, the B to top-middle, etc.). I also tried the keyIcon-Attribute, but then the keyPreview showed only the keyIcon, and not each character, when I multi-tap on the button.
Currently I'm 'designing' my keyboard with <Keyboard ..><Row ..><Key /></Row></Keyboard>-XML, not with a LinearLayout or a similar layout. Maybe I need to switch to a LinearLayout?
Any ideas?
Hello fellow Android developpers,
I have a problem regarding the autocompletetextview. I'm developping on a tablet, and I customized it nicely. When I run it on a tablet, it looks like this:
This is actually what I want - everything visible, small items, blue instead of orange. Cool.
And this is what happens when I run the exact same app on a smaller device, for example a Nexus-S:
The original views disappear, have orange stuff, and I only see one suggested item at a time or two at best. This is not what I intended.
If I run this app not in landscape, but in portrait, all works fine. Insane.
Does anybody know what I can do to keep the custom layout on the smaller screen?
The second image you shown here is the result of auto complete text view or edit text when the device is in landscape mode.
If the device is in portrait mode it will display the suggestions in Vertical list, and for landscape mode it will display in horizontal list and it tries to fill the entire screen with edit text and soft keyboard.
I too faced same problem, Please check the Android Developers website for information about Supporting multiple screens, it may help you.
New Answer
To hide soft keyboard when you press Done button you have to use imeOptions for AutoCompleteTextView. Do these changes to your code and try it.
Changes to XML layout file as below
<AutoCompleteTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:id="#+id/actv" />
In java file
actv = (AutoCompleteTextView)findViewById(R.id.actv);
actv.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true;
}
return false;
}
});
I hope it may help you.