Android autoCompleteTextView changes with screensize? - android

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.

Related

Android EditText Long Press Behaviour

I'm having some problems with the EditText in Android. Usually, when a user long clicks on the EditText or double-taps it, the word the cursor is in is highlighted, and the Contextual Actionbar (CAB) for the EditText pops up (on Android 3.0 and later).
My EditText did indeed do this until recently: the issue is that now long-pressing the EditText results in the selected word being "picked up", i.e. an enlargened "ghost" image of the word is picked up, and you can drag and drop it anywhere else in the text where it is inserted. I have not touched the code for the EditText at all. Anyone running Google Chrome (desktop version) can see the type of behaviour I mean if they highlight any text and drag it with their cursor.
I think the issue may have been caused by my device's upgrade to Android 4.2.2 recently. I have looked all over Google for information, but it has turned up nothing. Also, double-tapping the word also does not bring up the CAB as expected - it flashes for a second then goes away - but I do not think this is a related issue.
I really need this behaviour to stop, as my app cannot function without the CAB. So the question is: how can I get the "normal" behaviour back? All behaviour has been seen on a Nexus 4 running Android 4.2.2. All help is much appreciated; thank you very much!
XML Layout code for the EditText:
<EditText
android:id="#+id/editor_mainText"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_above="#+id/options_bottom"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/options_top"
android:background="#FFE7E7E7"
android:imeOptions="flagNoExtractUi"
android:inputType="textMultiLine|textNoSuggestions|textVisiblePassword"
android:padding="8dp"
android:scrollbars="none"
android:textCursorDrawable="#null"
android:textSize="17sp"
android:typeface="monospace" >
<requestFocus />
</EditText>
Note: My original answer was a bit wrong. I hadn't spotted my careless mistake in the code, but now that I have, here is why it didn't work
I've solved it, but I absolutely cannot explain why this behaviour happened in the first place, and why doing the following made any difference. But anyway, it did.
The EditText had been extended in order to make it "flingable" using a Scroller and VelocityTracker. This involved Overriding onTouchEvent(MotionEvent). But there was a little mistake in the code, where super.onTouchEvent(MotionEvent) would be called twice, as I had accidentally left out one of the break; statements in the switch-case, except during the MotionEvent.ACTION_MOVE event. The app worked fine even with this error all the way up to Android 4.2.1. I hadn't touched the code following the Android 4.2.2 upgrade, but for whatever reason, in the new version of Android, this small error triggered this weird behaviour.
So basically, it was a very basic mistake, and now I have learnt: always make sure to close off a case in a switch-case with a break statement!
I faced almost exact same behavior. I coudnt find any related question on stackoverflow, so will put my answer here. Maby it will be usefull for somebody. So if you ever faced a problem, that after closing copy/paste popup (by clicking anywhere on the screen) you cant select same text again (getting that "ghost" instead) all you have to do is manually disable textIsSelectable attribute and enable it again on view. For that i used this code im my ActivityMain. Poor solution, but its worked for me
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
if (ev?.action == MotionEvent.ACTION_DOWN) {
val v = currentFocus
if (v is TextView || v is TextInputEditText) {
val outRect = Rect()
v.getGlobalVisibleRect(outRect)
if (!outRect.contains(ev.rawX.toInt(), ev.rawY.toInt())) {
val view = when (v) {
is TextView -> v
is TextInputEditText -> v
else -> null
}
view?.setTextIsSelectable(false)
view?.refreshDrawableState()
view?.setTextIsSelectable(true)
}
}
}
return super.dispatchTouchEvent(ev)
}

Layout in background is getting intracted when tap on foreground layout Android

I am designing a Tab based Android application and using Fragment. My first fragment Layout have few Edittexts and on navigating to other fragment which don't have any EditTexts, on tap is getting Virtual keypad to appear. And after few hours of testing, I found its caused by the EditText which is in the previous fragment. How to prevent this ?
Use this in ur activity where you dont want a virtual keyboard
activity=this.getActivity();
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftkey(activity.getCurrentFocus().getWindowToken(), 0);
#Joseph, Try to mention the efforts and implementation you have achieved up till now, this helps to point out the error or a mistake. For your question, try to set the focus of the layout you want to interact with. This may help you to have a requestCall()
Also check this:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Here is how you can set focus in XML. Put this code within the tag of the control you want the focus on when starting the application.
<requestFocus
android:duplicateParentState="true"
android:focusable="true"
android:focusableInTouchMode="true" />

EditText: how to display the number while you are typing it?

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;
}
});

Disable one key on custom keyboard in Android

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");

Show/Hidden Soft Keyboard event in Android

I need to get the Show/Hidden event from the SoftKeyboard in Android.
I did a research, but nothing worked.
I wanted that 'cause we're working with a tablet 5.0'' with low resolution, so when you edit a EditText, the keyboard rise in full screen, and then or you press the "enter or next" key, or you press the back button to hide the keyboard... I need to update some fields with the new value, but I can't use the TextWatcher 'cause have some business logics on what's the right fields to update, and 'cause I just want to update when the user really finish the input.
And the onFocusChanged isn't a option, 'cause we don't want our customers needing to cliking in the next field, and hiding the keyboard to see the new values.
I don't want to override the onTouchEvent too, see if where the user cliked isn't the same field that he is editing.
Sorry for the specific problem and more specific solution that I'm asking for.
And sorry for my bad English :D
Use tree view observer to detect any change in view height and that time you can check whether keyboard state
final View activityRootView = findViewById(R.id.chat_pane_root_view);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if(mgr.isAcceptingText())
{
//keyboard is up
}
}
});
First of all in your declairation of your activity in manifest file declaire like this
<activity android:name="Map" android:windowSoftInputMode="stateHidden">
from this property your keyboard is being hidden,
after this you are declaire the imeOption property in your main.xml file edittext like this
<EditText
android:id="#+id/edttexttitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:hint="#string/tasktitle"
android:imeOptions="actionNext"
android:inputType="text"
android:textSize="#dimen/font_size_medium"
android:textColor="#color/darkgray1" />
this code is for your first edittext if more then one and if only one edittext then your need to change the imeOption of edittext like
android:imeOptions="actionGo"

Categories

Resources